-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffmpeg.ts
69 lines (63 loc) · 1.75 KB
/
ffmpeg.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
import { FRAME_RATE, FRAME_NAME, FRAME_DIR, VIDEO_PATH } from "./const";
import ffmpeg from "fluent-ffmpeg";
// TODO: simplify by just ret seconds. realized after i didn't need to construct string. those damn interview problems threw me off.
export const numToStamp = (frameNum: number): string => {
frameNum.toFixed(2);
let time = {
h: 0,
m: 0,
s: 0,
};
// 1 hours = 24f/s * 60s/m * 60m/h = 86400 f
while (frameNum > 86400) {
time.h++;
frameNum -= 86400;
}
while (frameNum > 1440) {
time.m++;
frameNum -= 1440;
}
while (frameNum > 24) {
time.s++;
frameNum -= 24;
}
frameNum /= 24;
time.s += frameNum;
return `${String(time.h).padStart(2, "0")}:${String(time.m).padStart(
2,
"0"
)}:${String(time.s).padStart(2, "0")}`;
};
export const getFrame = async (frameNum: number): Promise<string> => {
//convert frame num to timestamp
const frameStamp = numToStamp(frameNum);
let retPath: string = "";
let ret = new Promise((resolve, reject) => {
ffmpeg(VIDEO_PATH)
.on("filenames", (filenames: string[]) => {
retPath = filenames[0].split("''")[0];
console.log(`made file: ${retPath}`);
})
.on("progress", (progress) => {
console.log(`[ffmpeg] ${JSON.stringify(progress)}`);
})
.on("error", (err) => {
console.log(`[ffmpeg] error: ${err.message}`);
reject(err);
})
.on("end", () => {
resolve(`${FRAME_DIR}/${retPath}`);
return "[ffmpeg] finished";
})
.fps(FRAME_RATE)
.screenshots({
timestamps: [frameStamp],
filename: FRAME_NAME,
folder: FRAME_DIR,
});
// .output(`${FRAME_DIR}/${retPath}`);
}).then((res) => {
return `${res}`;
});
return ret;
};