-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
57 lines (48 loc) · 1.5 KB
/
utils.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
import colors from "colors/safe";
import { spawn } from "child_process";
export function randomNumber(min: number, max: number) {
return Math.floor(Math.random() * (max - min) + min);
}
export function log(text: string) {
console.log(
colors.bold(colors.green("Chipron | ")) +
colors.bold(colors.white(text))
);
}
export function wait(seconds: number) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}
export function getVideoDuration(video: string): Promise<number> {
return new Promise((resolve) => {
//trying to spawn ffprobe and get video duration (in sec)
const proc = spawn("ffprobe", ["-show_format", `./temp/${video}.mp4`]);
proc.stdout.on("data", async (data: Buffer) => {
const msg = data.toString();
if (!msg.includes("duration=")) return;
const videoDuration = msg.match(/duration=(.*)/);
resolve(parseInt(videoDuration[1]));
});
});
}
export function generateThumbnail(
video: string,
seconds: number,
i: number = 1
): Promise<void> {
return new Promise((resolve) => {
const proc = spawn("ffmpeg", [
"-y",
"-hide_banner",
"-i",
`./temp/${video}.mp4`,
"-ss",
seconds.toString(),
"-s",
"250x164",
"-vframes",
"1",
`./temp/${video}_${i}.jpg`,
]);
proc.on("close", () => resolve());
});
}