-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Downloader.ts
234 lines (202 loc) · 8.6 KB
/
Downloader.ts
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { MultiProgressBars, UpdateOptions } from "multi-progress-bars";
import Video from "./lib/Video.js";
import { settings, args } from "./lib/helpers.js";
import { promisify } from "util";
const sleep = promisify(setTimeout);
type promiseFunction = (f: Promise<void>) => void;
const reset = "\u001b[0m";
const cy = (str: string | number) => `\u001b[36;1m${str}\u001b[0m`;
const gr = (str: string | number) => `\u001b[32;1m${str}\u001b[0m`;
const ye = (str: string | number) => `\u001b[33;1m${str}\u001b[0m`;
const bl = (str: string | number) => `\u001b[34;1m${str}\u001b[0m`;
type DownloadProgress = { total: number; transferred: number; percent: number };
type Task = { video: Video; res: promiseFunction; formattedTitle: string };
const MaxRetries = 5;
// Ew, I really need to refactor this monster of a class
export default class Downloader {
private mpb?: MultiProgressBars;
private taskQueue: Task[] = [];
private barsQueued = 0;
private videosProcessing = 0;
private videosProcessed = 0;
private summaryStats: { [key: string]: { totalMB: number; downloadedMB: number; downloadSpeed: number } } = {};
private runQueue = false;
start(): void {
if (this.mpb === undefined && args.headless !== true) this.mpb = new MultiProgressBars({ initMessage: "", anchor: "top" });
if (this.runQueue === false) {
this.runQueue = true;
this.tickQueue();
}
}
private tickQueue(): void {
if (this.runQueue === false) return;
while (this.taskQueue.length !== 0 && this.videosProcessing < 2) {
this.videosProcessing++;
this.barsQueued--;
const task = this.taskQueue.pop();
if (task !== undefined) {
const processingVideoPromise = this.processVideo(task);
task.res(processingVideoPromise);
processingVideoPromise.then(() => this.videosProcessing-- && this.videosProcessed++);
}
}
for (let i = 0; i < 10; i++) {
if (this.taskQueue[i] === undefined) break;
if (args.headless !== true) {
this.mpb?.addTask(this.taskQueue[i].formattedTitle, {
type: "percentage",
barTransformFn: (str) => `${this.taskQueue[i].video.channel.consoleColor || ""}${str}`,
message: "Queued",
});
}
}
setTimeout(() => this.tickQueue(), 50);
}
stop(): void {
this.runQueue = false;
}
processVideos(videos: Video[]): Array<Promise<void>> {
if (videos.length === 0) return [];
console.log(`> Processing ${videos.length} videos...`);
this.summaryStats = {};
this.videosProcessed = 0;
const processingPromises = videos
.reverse()
.map((video) => new Promise<void>((res) => this.taskQueue.push({ video, res, formattedTitle: this.formatTitle(video) })));
// Handler for when all downloads are done.
Promise.all(processingPromises).then(this.updateSummaryBar.bind(this));
return processingPromises;
}
private updateSummaryBar(): void {
if (this.summaryStats === undefined || args.headless === true) return;
const { totalMB, downloadedMB, downloadSpeed } = Object.values(this.summaryStats).reduce(
(summary, stats) => {
for (const key in stats) {
summary[key as keyof typeof stats] += stats[key as keyof typeof stats];
}
return summary;
},
{ totalMB: 0, downloadedMB: 0, downloadSpeed: 0 }
);
// (videos remaining * avg time to download a video)
const totalVideos = this.taskQueue.length + this.videosProcessed + this.videosProcessing;
const processed = `Processed: ${ye(this.videosProcessed)}/${ye(totalVideos)}`;
const downloaded = `Total Downloaded: ${cy(downloadedMB.toFixed(2))}/${cy(totalMB.toFixed(2) + "MB")}`;
const speed = `Download Speed: ${gr(((downloadSpeed / 1024000) * 8).toFixed(2) + "Mb/s")}`;
this.mpb?.setFooter({
message: `${processed} ${downloaded} ${speed}`,
pattern: "",
});
}
/**
* Log the progress bar for a specific video.
* @param formattedTitle Title of bar to update.
* @param barUpdate Update object to update the bar with.
* @param displayNow If the update should be immediately sent to console (Only applied if running in headless mode)
*/
private log(formattedTitle: string, barUpdate: UpdateOptions, displayNow = false): void {
if (args.headless === true) {
if (displayNow === true && barUpdate.message !== undefined) console.log(`${formattedTitle} - ${barUpdate.message}`);
} else this.mpb?.updateTask(formattedTitle, barUpdate);
}
private formatTitle(video: Video) {
let formattedTitle: string;
if (args.headless === true) formattedTitle = `${video.channel.title} - ${video.title}`;
else if (video.channel.consoleColor !== undefined) {
formattedTitle = `${video.channel.consoleColor}${video.channel.title}${reset} - ${video.title}`.slice(
0,
32 + video.channel.consoleColor.length + reset.length
);
} else formattedTitle = `${video.channel.title} - ${video.title}`.slice(0, 32);
if (this.summaryStats !== undefined) while (formattedTitle in this.summaryStats) formattedTitle = `.${formattedTitle}`.slice(0, 32);
return formattedTitle;
}
private async processVideo(task: Task, retries = 0): Promise<void> {
const { video, formattedTitle } = task;
if (args.headless === true) console.log(`${formattedTitle} - Downloading...`);
else {
this.mpb?.addTask(formattedTitle, {
type: "percentage",
barTransformFn: (str) => `${video.channel.consoleColor || ""}${str}`,
message: "Download Starting...",
});
}
try {
// If the video is already downloaded then just mux its metadata
if (!(await video.isDownloaded())) {
const startTime = Date.now();
const downloadRequests = await video.download(settings.floatplane.videoResolution);
const totalBytes: number[] = [];
const downloadedBytes: number[] = [];
const percentage: number[] = [];
await Promise.all(
downloadRequests.map(async (request, i) => {
request.on("downloadProgress", (downloadProgress: DownloadProgress) => {
this.onDownloadProgress(startTime, totalBytes, i, downloadProgress, downloadedBytes, percentage, formattedTitle);
});
await new Promise((res, rej) => {
request.on("end", res);
request.on("error", rej);
});
})
);
this.summaryStats[formattedTitle].downloadSpeed = 0;
}
if (!(await video.isMuxed())) {
this.log(formattedTitle, {
percentage: 0.99,
message: "Muxing ffmpeg metadata...",
});
await video.muxffmpegMetadata();
}
if (settings.postProcessingCommand !== "") {
this.log(formattedTitle, { message: `Running post download command "${settings.postProcessingCommand}"...` });
await video.postProcessingCommand().catch((err) => console.log(`An error occurred while executing the postProcessingCommand!\n${err.message}\n`));
}
if (args.headless === true) {
this.log(formattedTitle, { message: `Downloaded!` });
} else this.mpb?.done(formattedTitle);
this.updateSummaryBar();
} catch (error) {
let info;
if (!(error instanceof Error)) info = new Error(`Something weird happened, whatever was thrown was not a error! ${error}`);
else info = error;
// Handle errors when downloading nicely
if (retries < MaxRetries) {
this.log(formattedTitle, { message: `\u001b[31m\u001b[1mERR\u001b[0m: ${info.message} - Retrying in ${retries}s [${retries}/${MaxRetries}]` }, true);
// Wait between retries
await sleep(1000 * retries);
await this.processVideo(task, ++retries);
} else this.log(formattedTitle, { message: `\u001b[31m\u001b[1mERR\u001b[0m: ${info.message} Max Retries! [${retries}/${MaxRetries}]` }, true);
}
}
private onDownloadProgress(
startTime: number,
totalBytes: number[],
i: number,
downloadProgress: DownloadProgress,
downloadedBytes: number[],
percentage: number[],
formattedTitle: string
) {
const timeElapsed = (Date.now() - startTime) / 1000;
totalBytes[i] = downloadProgress.total;
downloadedBytes[i] = downloadProgress.transferred;
percentage[i] = downloadProgress.percent;
// Sum the stats for multi part video downloads
const total = totalBytes.reduce((sum, b) => sum + b, 0);
const transferred = downloadedBytes.reduce((sum, b) => sum + b, 0);
const totalMB = total / 1024000;
const downloadedMB = transferred / 1024000;
const downloadSpeed = transferred / timeElapsed;
const downloadETA = total / downloadSpeed - timeElapsed; // Round to 4 decimals
this.log(formattedTitle, {
percentage: percentage.reduce((sum, b) => sum + b, 0) / percentage.length,
message: `${reset}${cy(downloadedMB.toFixed(2))}/${cy(totalMB.toFixed(2) + "MB")} ${gr(((downloadSpeed / 1024000) * 8).toFixed(2) + "Mb/s")} ETA: ${bl(
Math.floor(downloadETA / 60) + "m " + (Math.floor(downloadETA) % 60) + "s"
)}`,
});
this.summaryStats[formattedTitle] = { totalMB, downloadedMB, downloadSpeed };
this.updateSummaryBar();
}
}