-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscode.js
83 lines (74 loc) · 2.17 KB
/
transcode.js
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
const fs = require("fs");
const { createFFmpeg, fetchFile } = require("@ffmpeg/ffmpeg");
const ffmpeg = createFFmpeg({ log: true });
const transcodeVideo = async (filename, filepath, filetype, res) => {
try {
if (!ffmpeg.isLoaded()) await ffmpeg.load();
ffmpeg.FS(
"writeFile",
`test.${filename.slice(-3)}`,
await fetchFile(filepath)
);
if (["wav", "m4a", "mp3"].includes(filetype))
await ffmpeg.run(
"-i",
`test.${filename.slice(-3)}`,
"-vn",
"-acodec",
"copy",
`out.${filetype}`
);
else {
await ffmpeg.run(
"-i",
`test.${filename.slice(-3)}`,
`out.${filetype}`
);
}
const newPath = `./transcoded/${Date.now()}-Transcoded----${filename}.${filetype}`;
await fs.promises.writeFile(
newPath,
ffmpeg.FS("readFile", `out.${filetype}`)
);
await res.download(newPath);
} catch (err) {
console.log("Error:", err);
throw err;
}
};
const trimVideo = async (filename, filepath, start, end, res) => {
try {
if (!ffmpeg.isLoaded()) await ffmpeg.load();
ffmpeg.FS(
"writeFile",
`test.${filename.slice(-3)}`,
await fetchFile(filepath)
);
await ffmpeg.run(
"-i",
`test.${filename.slice(-3)}`,
"-ss",
`${start}`,
"-to",
`${end}`,
"-c:v",
"copy",
"-c:a",
"copy",
`out.${filename.slice(-3)}`
);
const newPath = `./transcoded/${Date.now()}-Trimmed----${filename}.${filename.slice(
-3
)}`;
await fs.promises.writeFile(
newPath,
ffmpeg.FS("readFile", `out.${filename.slice(-3)}`)
);
await res.download(newPath);
} catch (err) {
console.log("Error:", err);
throw err;
}
};
module.exports.transcodeVideo = transcodeVideo;
module.exports.trimVideo = trimVideo;