This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathYoutubeDl.js
52 lines (52 loc) · 1.92 KB
/
YoutubeDl.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.YoutubeDl = void 0;
const sanitizer_1 = require("./sanitizer");
const { exec } = require("child_process");
const path = require('path');
const isWin = process.platform === "win32";
class YoutubeDl {
static async getVideoMetadata(url, options, schema) {
options = options || {};
options.cli = options.cli || "yt-dlp";
options.cliOptions = options.cliOptions || '-f \"best\"';
url = sanitizer_1.Sanitizer.sanitizeUrl(url);
const bin = path.resolve(__dirname, '../tools/bin/' + options.cli + (isWin ? '.exe' : ''));
if (url.startsWith("-") && !url.startsWith("-- ")) {
url = "-- " + url;
}
const command = `${bin} ${options.cliOptions} --dump-single-json --no-warnings ${url}`;
return await new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject({ error: error.message, stderr, stdout });
return;
}
try {
let resultObject = JSON.parse(stdout);
if (schema) {
resultObject = YoutubeDl.filterKeys(resultObject, schema);
}
resolve(resultObject);
}
catch (e) {
reject({ error: e, stderr, stdout });
}
});
});
}
static filterKeys(obj, keys) {
if (!Array.isArray(keys)) {
keys = [keys];
}
const reducer = function (accumulator, currentValue) {
if (obj[currentValue]) {
accumulator[currentValue] = obj[currentValue];
}
return accumulator;
};
return keys.reduce((reducer), {});
}
}
exports.YoutubeDl = YoutubeDl;
//# sourceMappingURL=YoutubeDl.js.map