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
|
import { Config } from "./config.ts";
const ws = new WebSocket(Deno.args[0])
let config: Config = {} as unknown as Config
export class TextLineStream extends TransformStream<string, string> {
line = "";
constructor() {
super({
transform: (chunk, controller) => {
this.line += chunk;
while (true) {
const newline = this.line.indexOf("\n");
if (newline === -1) break;
controller.enqueue(this.line.slice(0, newline));
this.line = this.line.slice(newline + 1);
}
},
flush: (controller) => {
if (this.line === "") return;
controller.enqueue(this.line);
},
});
}
}
function key_to_url(key: string): string {
const [kind, id] = key.split(":", 2)
if (kind == "youtube") return `https://youtube.com/watch?v=${id}`
throw new Error("unknown kind");
}
async function do_download(key: string, data: { [key: string]: string }) {
const url = key_to_url(key)
if (!data.output) throw new Error("no output");
const output = data.output as string;
if (!data.profile) throw new Error("no profile");
const args = config.ytdlp_download.profiles[data.profile as string]
if (!args) throw new Error(`unknown profile ${data.profile}`);
await Deno.mkdir(output, { recursive: true })
const child = new Deno.Command("yt-dlp", {
args: [
"--quiet",
"--progress",
"--progress-template", "%(progress)j",
"--newline",
...args,
url
],
stdout: "piped",
stderr: "inherit",
cwd: output
}).spawn()
const lines = child.stdout.pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream())
for await (const line of lines) {
if (!line.length) continue
console.log(JSON.stringify(line));
const k = JSON.parse(line)
ws.send(JSON.stringify({
t: "metadata", key, data: {
progress: (k._percent ?? 0) / 100,
status: k._default_template?.trim() ?? ""
}
}))
}
const status = await child.status
if (!status.success) ws.send(JSON.stringify({ t: "metadata", key, data: { failed: true } }))
ws.send(JSON.stringify({ t: "metadata", key, data: { progress: null, status: null } }))
ws.send(JSON.stringify({ t: "complete", key }))
ws.send(JSON.stringify({ t: "save" }))
}
ws.onerror = () => console.error("ws error")
ws.onclose = () => {
console.error("ws closed")
Deno.exit(1)
}
ws.onopen = () => {
console.log("ws open");
ws.send(JSON.stringify({ t: "register", name: "yt-dlp video downloader", task_kinds: ["youtube"] }))
ws.send(JSON.stringify({ t: "accept" }))
}
ws.onmessage = async ev => {
if (typeof ev.data != "string") return
const p = JSON.parse(ev.data)
if (p.t == "config") config = p.config
if (p.t == "error") console.error(`error: ${p.message}`);
if (p.t == "work") {
await do_download(p.key, p.data)
ws.send(JSON.stringify({ t: "accept" }))
}
}
|