-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-videos.js
82 lines (72 loc) · 1.98 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
70
71
72
73
74
75
76
77
78
79
80
81
82
import { spawn } from "child_process";
import fs from "fs";
import path from "path";
import scanLibrary from "./scanLibrary.js"; // Assuming this is an ESM export
// 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);
return 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) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`FFmpeg process exited with code ${code}`));
}
});
});
}
function processVideos() {
if (videos.length === 0) {
console.log("All videos processed.");
return;
}
const video = videos.pop();
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();
})
.catch((error) => {
console.error(`Error processing video ${video.fileName}:`, error);
processVideos();
});
}
}
processVideos();