-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
119 lines (101 loc) · 2.84 KB
/
convert.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const { spawn } = require("child_process");
const fs = require("fs-extra");
const argv = require("yargs").argv;
const path = argv.path;
function getFiles(path) {
return fs.readdirSync(`${path}`);
}
function getTranscodeFiles(files) {
const transCodeFiles = [];
for (const file of files) {
const splits = file.split(".");
const fileExt = splits[splits.length - 1];
if (fileExt === "mkv") {
transCodeFiles.push({
filepath: file,
path: `${path}/${file}`,
ext: fileExt,
filename: splits.splice(0, splits.length -1).join(".")
});
}
}
return transCodeFiles;
}
async function getAudioCodecs(transCodeFile) {
return new Promise(function (resolve, reject) {
let json = "";
const ffprobe = spawn("ffprobe", [
"-hide_banner",
"-v",
"error",
"-print_format",
"json",
"-show_streams",
`${transCodeFile}`,
]);
ffprobe.stdout.on("data", (data) => {
json += data.toString("utf8");
});
ffprobe.stderr.on("data", (data) => {
reject(`Program exited with error ${data}`);
});
ffprobe.on("close", (code) => {
const parsedJson = JSON.parse(json);
const codecs = [];
for (const entry of parsedJson.streams) {
if (entry.codec_type === "audio") {
codecs.push(entry.codec_name);
}
}
resolve(codecs);
});
});
}
async function convertDTS(transCodeFile) {
return new Promise(function (resolve, reject) {
const outputPath = `${path}/${transCodeFile.filename}_ac3.${transCodeFile.ext}`;
const dtsConvert = spawn("ffmpeg", [
"-i",
`${transCodeFile.path}`,
"-c:v",
"copy",
"-c:a",
"ac3",
"-b:a",
"640k",
"-c:s",
"copy",
"-hide_banner",
"-stats",
"-progress",
"pipe:1",
`${outputPath}`,
]);
dtsConvert.stdout.on("data", (data) => {
const linesSplit = data.toString("utf8").split("\n");
for (const line of linesSplit) {
const lineSplit = line.split("=")
if (lineSplit[0] === 'total_size') console.log(`Size: ${parseInt(lineSplit[1], 10) / 1024 / 1024} MB`)
}
});
// dtsConvert.stderr.on("data", (data) => {
// reject(`Program exited with error ${data}`);
// });
dtsConvert.on("close", (code) => {
resolve(`Transcoded successfully`);
});
});
}
(async function main() {
const files = getFiles(path);
const transCodeFiles = getTranscodeFiles(files);
for (const transCodeFile of transCodeFiles) {
console.log(`Checking ${transCodeFile.filepath}`);
const audioCodec = await getAudioCodecs(transCodeFile.path);
console.log(`Found codecs: ${audioCodec}`);
// Convert
if (audioCodec.includes("dts") && !audioCodec.includes("ac3")) {
console.log(await convertDTS(transCodeFile));
}
}
})();