cache download metadata to speedup resume phase.

This commit is contained in:
2022-08-28 18:13:17 +02:00
parent 80478cfa67
commit 5d51309ef3

View File

@@ -20,6 +20,7 @@ class Process {
private settings: ISettings; private settings: ISettings;
private stdout: Readable; private stdout: Readable;
private pid: number; private pid: number;
private metadata?: IDownloadInfo;
private exePath = join(__dirname, 'yt-dlp'); private exePath = join(__dirname, 'yt-dlp');
constructor(url: string, params: Array<string>, settings: any) { constructor(url: string, params: Array<string>, settings: any) {
@@ -28,6 +29,7 @@ class Process {
this.settings = settings this.settings = settings
this.stdout = undefined; this.stdout = undefined;
this.pid = undefined; this.pid = undefined;
this.metadata = undefined;
} }
/** /**
@@ -58,46 +60,51 @@ class Process {
* @returns Promise to the lock * @returns Promise to the lock
*/ */
public getInfo(): Promise<IDownloadInfo> { public getInfo(): Promise<IDownloadInfo> {
let stdoutChunks = []; if (!this.metadata) {
const ytdlpInfo = spawn(this.exePath, ['-j', this.url]); let stdoutChunks = [];
const ytdlpInfo = spawn(this.exePath, ['-j', this.url]);
ytdlpInfo.stdout.on('data', (data) => { ytdlpInfo.stdout.on('data', (data) => {
stdoutChunks.push(data); stdoutChunks.push(data);
});
return new Promise((resolve, reject) => {
ytdlpInfo.on('exit', () => {
try {
const buffer = Buffer.concat(stdoutChunks);
const json = JSON.parse(buffer.toString());
resolve({
formats: json.formats.map((format: IDownloadInfoSection) => {
return {
format_id: format.format_id ?? '',
format_note: format.format_note ?? '',
fps: format.fps ?? '',
resolution: format.resolution ?? '',
vcodec: format.vcodec ?? '',
acodec: format.acodec ?? '',
}
}).filter((format: IDownloadInfoSection) => format.format_note !== 'storyboard'),
best: {
format_id: json.format_id ?? '',
format_note: json.format_note ?? '',
fps: json.fps ?? '',
resolution: json.resolution ?? '',
vcodec: json.vcodec ?? '',
acodec: json.acodec ?? '',
},
thumbnail: json.thumbnail,
title: json.title,
});
} catch (e) {
reject('failed fetching formats, downloading best available');
}
}); });
})
return new Promise((resolve, reject) => {
ytdlpInfo.on('exit', () => {
try {
const buffer = Buffer.concat(stdoutChunks);
const json = JSON.parse(buffer.toString());
const info = {
formats: json.formats.map((format: IDownloadInfoSection) => {
return {
format_id: format.format_id ?? '',
format_note: format.format_note ?? '',
fps: format.fps ?? '',
resolution: format.resolution ?? '',
vcodec: format.vcodec ?? '',
acodec: format.acodec ?? '',
}
}).filter((format: IDownloadInfoSection) => format.format_note !== 'storyboard'),
best: {
format_id: json.format_id ?? '',
format_note: json.format_note ?? '',
fps: json.fps ?? '',
resolution: json.resolution ?? '',
vcodec: json.vcodec ?? '',
acodec: json.acodec ?? '',
},
thumbnail: json.thumbnail,
title: json.title,
}
resolve(info);
this.metadata = info;
} catch (e) {
reject('failed fetching formats, downloading best available');
}
});
})
}
return new Promise((resolve) => { resolve(this.metadata!) });
} }
/** /**