-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
219 lines (199 loc) · 6.43 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const path = require("path");
const fs = require("fs");
const { execSync, exec } = require("child_process");
const { performance } = require("perf_hooks");
const picomatch = require("picomatch");
const reCurrentTime = /out_time_ms=(.*?)\s/;
class CTFB {
constructor(bookPath, { title, pattern, output, bitrate }) {
this.bookPath = bookPath;
this.metadata = [];
this.metaPath = path.join(bookPath, "./metadata.txt");
this.fileList = [];
this.fileListPath = path.join(bookPath, "./filelist.txt");
this.title = title;
this.pattern = picomatch(pattern);
this.output = output;
this.bitrate = bitrate;
this.bookLength = 0;
}
async process(isInteractive, isYoutube) {
await this.scan(isYoutube);
/* Exit if no files found. */
if (!this.fileList.length) {
console.log("No files found.");
process.exit(0);
}
console.log(`${this.fileList.length} input files found.`);
console.log(
`Total duration: ${this.constructor.toSexagesimal(this.bookLength)}`
);
/* Save metadata to text files. */
fs.writeFileSync(this.metaPath, this.metadata.join("\n"));
fs.writeFileSync(this.fileListPath, this.fileList.join("\n"));
if (!isInteractive) {
/* Start combining if in non-interactive mode. */
await this.combine();
} else {
console.log(
`Press [m] to open the generated metadata file.\nPress any other button to start creating the book.`
);
if (process.stdin.isTTY) process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.setEncoding("utf8");
process.stdin.on("data", async (data) => {
console.log(data);
if (data === "m") {
process.stdout.write("Opening the metadata file...");
exec(`${this.constructor.getCommandLine()} ${this.metaPath}`);
} else {
console.log("Generating the book...");
await this.combine();
}
});
}
}
async scan(isYoutube) {
const dir = await fs.promises.opendir(this.bookPath, {});
const files = [];
for await (const file of dir) {
const filename = file.name;
if (this.pattern(filename)) {
const filePath = path.join(dir.path, file.name);
const data = execSync(
`ffprobe -i "${filePath}" -print_format json -show_chapters -show_entries format -loglevel error`
).toString();
const json = JSON.parse(data);
const chapters = !isYoutube
? CTFB.getFileChapters(json.chapters)
: CTFB.getYoutubeChapters(dir.path, file.name);
files.push({
name: filename,
path: filePath.replace(/'/g, "'\\''"),
duration: parseFloat(json.format.duration),
chapters,
});
}
}
files.sort((a, b) => a.name.localeCompare(b.name));
this.metadata = [";FFMETADATA", `title=${this.title}`];
let id = 0;
for (const file of files) {
id = CTFB.setChapters(
this.metadata,
id,
file.name,
file.chapters,
this.bookLength
);
this.bookLength += file.duration;
this.fileList.push(`file '${file.path}'`);
}
}
async combine() {
const start = performance.now();
const totalTime = this.bookLength * 1e6;
const ffmpeg = exec(
`ffmpeg -y -hide_banner -loglevel 16 -progress - -f concat -safe 0 -i "${
this.fileListPath
}" -i "${this.metaPath}" -map_metadata 1 ${
this.bitrate ? `-b:a ${this.bitrate}` : `-c:a copy`
} "${this.output}"`
);
ffmpeg.stdout.on("data", (data) => {
const match = reCurrentTime.exec(data);
if (match) this.constructor.printProgress(match[1], totalTime, start);
});
ffmpeg.on("exit", () => {
const end = performance.now();
console.log(
`\nTime Elapsed: ${this.constructor.toSexagesimal(
(end - start) / 1000
)}`
);
// delete temporary files
fs.unlinkSync(this.metaPath);
fs.unlinkSync(this.fileListPath);
process.exit(0);
});
}
static toDecimal(number) {
return number.toString().padStart(2, "0");
}
static toSexagesimal(time) {
const milliseconds = (time * 1000) | 0;
const hours = this.toDecimal((milliseconds / 36e5) | 0);
let remainder = milliseconds % 36e5;
const minutes = this.toDecimal((remainder / 6e4) | 0);
remainder = remainder % 6e4;
const seconds = this.toDecimal((remainder / 1e3) | 0);
remainder = remainder % 1e3;
return `${hours}:${minutes}:${seconds}.${remainder}`;
}
static getFileName(filename) {
const extension = path.extname(filename);
return filename.slice(0, filename.length - extension.length);
}
static printProgress(currentPosition, totalTime, startTime) {
const fraction = parseInt(currentPosition, 10) / totalTime;
const currentTime = performance.now();
const progress = (fraction * 100).toFixed(3) + "%";
const timeLeft = this.toSexagesimal(
((currentTime - startTime) * (1 / fraction - 1)) / 1e3
);
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Progress: ${progress} Time left: ${timeLeft}`);
}
static getCommandLine() {
switch (process.platform) {
case "darwin":
return "open";
case "win32":
return "start";
default:
return "xdg-open";
}
}
static getFileChapters(chapters) {
for (const chapter of chapters) {
chapter.title = (chapter.tags && chapter.tags.title) || "";
}
return chapters;
}
static getYoutubeChapters(dir, name) {
const infoFilePath = path.join(
dir,
path.basename(name, path.extname(name)) + ".info.json"
);
try {
const file = fs.readFileSync(infoFilePath, { encoding: "utf8" });
const json = JSON.parse(file);
return json.chapters || [];
} catch (e) {
return [];
}
}
static setChapters(metadata, id, filename, chapters, bookLength) {
if (!chapters.length) {
chapters.push({
start_time: 0,
title: CTFB.getFileName(filename),
});
}
for (const chapter of chapters) {
const chapterId = CTFB.toDecimal(id);
const chapterName = chapter.title || id;
const chapterStartTime = CTFB.toSexagesimal(
bookLength + parseFloat(chapter["start_time"])
);
metadata.push(
`CHAPTER${chapterId}=${chapterStartTime}`,
`CHAPTER${chapterId}NAME=${chapterName}`
);
id += 1;
}
return id;
}
}
module.exports = CTFB;