-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-videos.js
69 lines (62 loc) · 1.67 KB
/
convert-videos.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
const spawn = require("child_process").spawn;
const fs = require("fs");
const path = require("path");
const scanLibrary = require("./scanLibrary.js");
// Find all videos with scanLibrary()
const albumDirs = scanLibrary("src/album-assets/");
let videos = [];
albumDirs.forEach((dir) => {
const videoFiles = dir.files.filter((file) => file.fileType === "video");
videos.push(...videoFiles);
});
function getOutputPath(video) {
return `./dist/video/${video.fileBase}.mp4`;
}
// https://blog.founderatwork.com/how-to-batch-process-video-conversions-using-ffmpeg-with-node-js/
// https://gist.github.com/rick4470/0e051cbceae6fd591fd3c02a8ab417cc
function resizeVideo(video, quality) {
const outputPath = getOutputPath(video);
const p = new Promise((resolve, reject) => {
console.log("Converting...", video.fileName);
const ffmpeg = spawn("ffmpeg", [
"-i",
video.filePath,
"-c:v",
"libx265",
"-crf",
"24",
"-vf",
`scale=-2:${quality}`,
"-preset",
"veryslow",
"-tag:v",
"hvc1",
"-movflags",
"faststart",
outputPath,
]);
ffmpeg.stderr.on("data", (data) => {
console.log(`${data}`);
});
ffmpeg.on("close", (code) => {
resolve();
});
});
return p;
}
function processVideos() {
let video = videos.pop();
if (video) {
const outputPath = getOutputPath(video);
if (fs.existsSync(outputPath)) {
console.log(`Video already exists at ${outputPath}`);
processVideos();
} else {
resizeVideo(video, 720).then(() => {
console.log(`Video processed and saved to ${outputPath}`);
processVideos();
});
}
}
}
processVideos();