-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
90 lines (87 loc) · 2.73 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
/**
* yt-dlp wrapper. Provides platform-independent Promise API.
* @module boram/yt-dlp
*/
import assert from "assert";
import path from "path";
import {APP_PATH} from "../shared";
import {makeRunner, getRunPath} from "../util";
require("../../bin/yt-dlp." + (BORAM_WIN_BUILD ? "exe" : "zip"));
export default makeRunner("yt-dlp", {
_fixPathArgs(runpath, args) {
// Special case for ytdl on Mac: we run it with python.
if (BORAM_MAC_BUILD || !runpath) {
// We always pack yt-dlp binary on Windows.
assert(!BORAM_WIN_BUILD);
// Can be run as executable but this way we get better error
// message if Python is not installed.
const altexe = "python";
runpath = getRunPath(altexe, {system: true});
const zippath = path.join(APP_PATH, "yt-dlp.zip");
return [runpath, [zippath].concat(args), altexe];
} else {
return [runpath, args];
}
},
getInfo(url) {
return this._run([
"--no-playlist",
"--dump-json",
"--all-subs",
"--",
url,
]).then(JSON.parse);
},
download({url, format, outpath}, onUpdate) {
const {vfid, afid, sfid} = format;
const ppArgs = [];
const args = [
"--no-part",
"--no-playlist",
"--format", vfid + (afid ? `+${afid}` : ""),
"--merge-output-format", "mkv",
];
if (BORAM_MAC_BUILD) {
args.push("--ffmpeg-location", getRunPath("ffmpeg"));
}
if (sfid) {
args.push("--sub-lang", sfid, "--write-sub", "--embed-subs");
ppArgs.push("-c:s", "ass", "-f", "matroska");
}
if (ppArgs.length) {
args.push("--postprocessor-args", ppArgs.join(" "));
}
args.push("--output", outpath, "--", url);
let log = "";
let progress = 0;
return this._run(args, (chunk) => {
// Extract last printed line (status).
chunk = chunk.toString();
// console.log("@@@ IN", JSON.stringify(chunk));
if (BORAM_WIN_BUILD) {
chunk = chunk.replace(/\r\n/g, "\n");
}
const cr = chunk.lastIndexOf("\r");
const nl = chunk.lastIndexOf("\n");
const lastnl = Math.max(cr, nl);
if (lastnl > -1) {
let nextlastnl = -1;
if (lastnl > 0) {
const cr = chunk.lastIndexOf("\r", lastnl - 1);
const nl = chunk.lastIndexOf("\n", lastnl - 1);
nextlastnl = Math.max(cr, nl);
}
const status = nextlastnl > -1 ? chunk.slice(nextlastnl + 1, lastnl)
: log + chunk.slice(0, lastnl);
log = chunk.slice(lastnl + 1);
const matched = status.match(/\s(\d+(\.\d+)?)%\s/);
if (matched) {
progress = parseFloat(matched[1]);
}
onUpdate({progress, status});
} else {
log += chunk;
}
});
},
});