diff options
Diffstat (limited to 'scripts/ytdlp_download.ts')
-rw-r--r-- | scripts/ytdlp_download.ts | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/scripts/ytdlp_download.ts b/scripts/ytdlp_download.ts index f82e3b8..760ac26 100644 --- a/scripts/ytdlp_download.ts +++ b/scripts/ytdlp_download.ts @@ -1,5 +1,7 @@ +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 = ""; @@ -28,8 +30,15 @@ function key_to_url(key: string): string { throw new Error("unknown kind"); } -async function do_download(key: string, output: string) { +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: [ @@ -37,16 +46,7 @@ async function do_download(key: string, output: string) { "--progress", "--progress-template", "%(progress)j", "--newline", - "--download-archive", "archive", - "-f", "bestvideo+bestaudio", - "--embed-metadata", - "--embed-thumbnail", - "--embed-info-json", - "--embed-subs", - "--embed-chapters", - "--ignore-no-formats", - "--remux", "mkv", - "-o", "%(id)s", + ...args, url ], stdout: "piped", @@ -68,12 +68,15 @@ async function do_download(key: string, output: string) { } const status = await child.status if (!status.success) throw new Error("download failed"); - + ws.send(JSON.stringify({ t: "metadata", key, data: { progress: null, status: null } })) ws.send(JSON.stringify({ t: "complete", key })) } ws.onerror = () => console.error("ws error") -ws.onclose = () => console.error("ws closed") +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"] })) @@ -82,10 +85,10 @@ ws.onopen = () => { 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") { - if (!p.data.output) throw new Error("no output"); - await do_download(p.key, p.data.output) + await do_download(p.key, p.data) ws.send(JSON.stringify({ t: "accept" })) } } |