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,6 +60,7 @@ class Process {
* @returns Promise to the lock * @returns Promise to the lock
*/ */
public getInfo(): Promise<IDownloadInfo> { public getInfo(): Promise<IDownloadInfo> {
if (!this.metadata) {
let stdoutChunks = []; let stdoutChunks = [];
const ytdlpInfo = spawn(this.exePath, ['-j', this.url]); const ytdlpInfo = spawn(this.exePath, ['-j', this.url]);
@@ -70,7 +73,7 @@ class Process {
try { try {
const buffer = Buffer.concat(stdoutChunks); const buffer = Buffer.concat(stdoutChunks);
const json = JSON.parse(buffer.toString()); const json = JSON.parse(buffer.toString());
resolve({ const info = {
formats: json.formats.map((format: IDownloadInfoSection) => { formats: json.formats.map((format: IDownloadInfoSection) => {
return { return {
format_id: format.format_id ?? '', format_id: format.format_id ?? '',
@@ -91,7 +94,9 @@ class Process {
}, },
thumbnail: json.thumbnail, thumbnail: json.thumbnail,
title: json.title, title: json.title,
}); }
resolve(info);
this.metadata = info;
} catch (e) { } catch (e) {
reject('failed fetching formats, downloading best available'); reject('failed fetching formats, downloading best available');
@@ -99,6 +104,8 @@ class Process {
}); });
}) })
} }
return new Promise((resolve) => { resolve(this.metadata!) });
}
/** /**
* function that kills the current process * function that kills the current process