-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (53 loc) · 2.12 KB
/
script.js
File metadata and controls
63 lines (53 loc) · 2.12 KB
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
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const fs = require("fs");
const path = require("path");
const filesToConvert = [
// Files to convert here...
//"demo.mkv"
];
function searchFilesInDir(inputDir = "./input") {
try {
// Add file to filesToConvert array
const files = fs.readdirSync(inputDir);
filesToConvert.push(...files);
} catch(err) {
//handle error in search
console.error(`Error reading directory: ${err}`);
}
}
function clearFilesToConvert() {
filesToConvert.length = 0
}
let successCount = 0; // Counter for successful conversions
let errorCount = 0; // Counter for files failed to be converted
async function convertToH265(inputDir = "./input", outputDir = "./output") {
for(let i = 0; i < filesToConvert.length; i++) {
let inputFile;
let outputFile;
filesToConvert[i].split(".").pop() === "m3u8" ? (
// directory output, add a 0 before if iteration numbers < 10, iteration + 1
inputFile = filesToConvert[i],
outputFile = path.join(outputDir, `${i < 9 ? "0" : ""}${i + 1}.mp4`)
) : (
inputFile = path.join(inputDir, filesToConvert[i]),
outputFile = path.join(outputDir, filesToConvert[i])
)
try {
await exec(
`ffmpeg -i "${inputFile}" -c:v libx265 -c:a copy -x265-params crf=25 "${outputFile}"`, //Quotes are essential to avoid whitespace errors
{ maxBuffer: 10 * 1024 * 1024 } // change default maxBuffer limit to 10 GB
);
console.log(`Video ${outputFile} has been successfully converted.`);
successCount++;
} catch(error) {
console.error(`Error during conversion of ${inputFile}: ${error.message}`);
errorCount++;
}
}
}
clearFilesToConvert()
searchFilesInDir()
convertToH265().then(() => {
console.log(`All videos have been processed! ${successCount} video${successCount < 2 ? '' : 's'} were successfully converted, ${errorCount} video${errorCount < 2 ? '' : 's'} failed to be converted.`);
});