-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
128 lines (108 loc) · 3.44 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
120
121
122
123
124
125
126
127
128
const ffmpeg = require("fluent-ffmpeg");
const fs = require("fs");
const { argv, exit } = require("process");
const os = require("os");
if (os.platform() === "win32") {
ffmpeg.setFfmpegPath("C:\\ffmpeg\\bin\\ffmpeg.exe");
ffmpeg.setFfprobePath("C:\\ffmpeg\\bin\\ffprobe.exe");
} else {
ffmpeg.setFfmpegPath("/usr/bin/ffmpeg");
ffmpeg.setFfprobePath("/usr/bin/ffprobe");
}
const getDirectories = (path) => {
return fs.readdirSync(path).filter((file) => {
return fs.statSync(path + "/" + file).isDirectory();
});
};
const keypress = async () => {
process.stdin.setRawMode(true);
return new Promise((resolve) =>
process.stdin.once("data", (data) => {
if (data.toString() === "q") exit();
process.stdin.setRawMode(false);
resolve();
})
);
};
const processDirectory = async (dir) => {
return new Promise(async (resolve, reject) => {
const audioDirectories = getDirectories(mainDir + "/" + dir).filter(
(subDir) => subDir.includes("audio")
);
if (!audioDirectories.length) reject("Error : no audio stream found");
for (const audioDir of audioDirectories) {
await new Promise(async (resolveFile) => {
const fullAudioDir = mainDir + "/" + dir + "/" + audioDir;
const audioFiles = fs
.readdirSync(fullAudioDir)
.filter(
(file) =>
file.includes(".aac") | file.includes(".ts") &&
file.match(/[0-9]{6}/g) &&
!file.includes("temp")
);
let filesProcessced = 0;
for (const audioFile of audioFiles) {
let audNumber = audioFile.match(/[0-9]{6}/g)[0];
const targetFile = fullAudioDir + "/" + "temp-" + audNumber + ".aac";
await new Promise((resolveProcess) => {
ffmpeg(fullAudioDir + "/" + audioFile)
.addOption(["-c aac", "-ac 2"])
.output(targetFile)
.on("error", function (err) {
console.log("An error occurred: " + err.message);
})
.on("end", function () {
console.log(
"Files proccessed " +
audioFile +
" (" +
filesProcessced++ +
"/" +
(audioFiles.length - 1) +
")"
);
fs.rmSync(fullAudioDir + "/" + audioFile);
fs.renameSync(
targetFile,
fullAudioDir + "/" + audioFile.replace(".ts", ".aac")
);
resolveProcess();
if (filesProcessced - 1 === audioFiles.length - 1)
resolveFile();
})
.run();
});
}
const m3u8File =
fullAudioDir +
"/" +
fs
.readdirSync(fullAudioDir)
.filter((file) => file.includes(".m3u8"))[0];
fs.writeFileSync(
m3u8File,
fs.readFileSync(m3u8File).toString().replace(/\.ts/g, ".aac")
);
});
}
resolve();
});
};
const mainDir = argv[2];
const fixedDir = argv[3];
(async () => {
const directories = fixedDir ? [fixedDir] : getDirectories(mainDir);
for (const dir of directories) {
console.log(
"processing : " + dir + " press any key to continue or q to exit"
);
await keypress();
try {
await processDirectory(dir);
} catch (err) {
console.log(err);
}
}
exit();
})();