-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhooks.ts
130 lines (111 loc) · 2.97 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { useState, useEffect, useRef } from "react";
import { fetchFile } from "@ffmpeg/ffmpeg";
import { millisToMinutesAndSeconds } from "utils";
import {
ffmpeg,
storedDuration,
_time,
setWatchTime,
getBitrate,
inputStr,
AUDIO_BITRATE,
outputStr,
setWatchDuration,
} from "utils/ffmpeg";
export const useLoadFfmpeg = () => {
const [ready, setReady] = useState(false);
const load = async () => {
if (!ffmpeg.isLoaded()) await ffmpeg.load();
setReady(true);
};
useEffect(() => {
load();
}, []);
return ready;
};
export const useGetCompressProgress = (converting: boolean) => {
const [progress, setProgress] = useState((0).toFixed(1));
const interval = useRef<NodeJS.Timer>();
const updateProgress = () => {
const progress = (_time / storedDuration) * 100;
setProgress(progress.toFixed(1));
};
useEffect(() => {
if (converting) {
setWatchTime(true);
interval.current = setInterval(updateProgress, 2000);
}
if (!converting) {
setWatchTime(false);
interval.current && clearInterval(interval.current);
}
}, [converting]);
return progress;
};
export const useCompressToWppSize = (video: File | undefined) => {
const [finished, setFinished] = useState(false);
const [converting, setConverting] = useState(false);
const handleStartConversion = () => !converting && compressToWppSize();
const compressToWppSize = async () => {
if (video) {
setConverting(true);
setFinished(false);
// Run the FFMpeg command
const bitrate = await getBitrate();
const startTime = performance.now();
await ffmpeg.run(
"-y",
"-i",
inputStr,
"-c:v",
"libx264",
"-b:v",
`${bitrate}k`,
"-c:a",
"aac",
"-b:a",
`${AUDIO_BITRATE}k`,
"-vsync",
"cfr",
outputStr
);
console.log("FIM DA CONVERSÃO");
console.log(
"tempo ",
millisToMinutesAndSeconds(performance.now() - startTime)
);
// // Create a URL
setFinished(true);
setConverting(false);
}
};
return { finished, converting, handleStartConversion };
};
export const useFfmpegVideo = (ffmpegReady: boolean) => {
const [video, setVideo] = useState<File>();
useEffect(() => {
(async () => {
if (video && ffmpegReady) {
ffmpeg.FS("writeFile", inputStr, await fetchFile(video));
setWatchDuration(true);
}
})();
}, [video, ffmpegReady]);
return { video, setVideo };
};
export const useIsMobile = () => {
const [width, setWidth] = useState<number>(
typeof window !== "undefined" ? window.innerWidth : 1000
);
function handleWindowSizeChange() {
setWidth(window.innerWidth);
}
useEffect(() => {
window.addEventListener("resize", handleWindowSizeChange);
return () => {
window.removeEventListener("resize", handleWindowSizeChange);
};
}, []);
const isMobile = width <= 768;
return isMobile;
};