diff --git a/docs/video-clips.md b/docs/video-clips.md index b6e37cd..3271885 100644 --- a/docs/video-clips.md +++ b/docs/video-clips.md @@ -57,6 +57,27 @@ to an hour. Limits are constants at the top of `server/src/clips.js`. A source can be several GB, so leave room on that disk. + **If YouTube says "Sign in to confirm you're not a bot"**, it's + refusing the VPS's address, and yt-dlp needs a signed-in account's + cookies. Use a separate Google account made for this, never a + personal one: whoever has the cookies is signed in as it. Then set + one of: + + ```ini + # A cookies.txt exported from a browser signed in to that account + # (a "Get cookies.txt" browser extension, Netscape format). Keep it + # readable only by the service: chmod 600, owned by its user. + Environment=YTDLP_COOKIES=/var/lib/ngu/youtube-cookies.txt + + # Or: read them from a browser installed and signed in on the VPS + # itself, as yt-dlp's --cookies-from-browser takes it. + # Environment=YTDLP_COOKIES_FROM_BROWSER=firefox + ``` + + The file is copied for each download, so yt-dlp never rewrites the + original. Cookies expire; when downloads start failing again, export + a fresh file. + 4. **nginx**: the editor's video and clip downloads are big streams. Give them their own block so nginx passes them straight through: diff --git a/server/src/clips.js b/server/src/clips.js index 34f6d0e..296ceaa 100644 --- a/server/src/clips.js +++ b/server/src/clips.js @@ -12,6 +12,20 @@ FFMPEG_PATH default "ffmpeg" FFPROBE_PATH default "ffprobe" + When YouTube asks the server to "sign in to confirm you're not a + bot", yt-dlp needs a signed-in account's cookies. One of: + + YTDLP_COOKIES a cookies.txt exported from a browser + (Netscape format). Copied per download, + so yt-dlp never writes to the original + YTDLP_COOKIES_FROM_BROWSER a browser on this machine to read them + from, as yt-dlp's --cookies-from-browser + takes it: "firefox", "chrome", or + "firefox:/path/to/profile" + + Use a separate Google account, not a personal one: the cookies + are that account's sign-in. If both are set, the file wins. + ── Safety ── Only links on YouTube and Facebook are accepted (SOURCE_HOSTS), so yt-dlp can't be pointed at anything else, internal addresses @@ -44,6 +58,8 @@ export const CLIPS_DIR = path.resolve(process.env.CLIPS_DIR ?? "./clips"); const YTDLP = process.env.YTDLP_PATH ?? "yt-dlp"; const FFMPEG = process.env.FFMPEG_PATH ?? "ffmpeg"; const FFPROBE = process.env.FFPROBE_PATH ?? "ffprobe"; +const COOKIES_FILE = process.env.YTDLP_COOKIES || null; +const COOKIES_FROM_BROWSER = process.env.YTDLP_COOKIES_FROM_BROWSER || null; const MAX_SOURCE_SECONDS = 4 * 60 * 60; const MAX_SOURCE_SIZE = "4G"; @@ -229,53 +245,81 @@ function update(db, id, fields) { ); } +/* yt-dlp's cookie options, from the environment. A cookies file is + copied first: yt-dlp saves updated cookies back to the file it's + given, and the original may be read-only to the service. */ +async function cookieArgs(jobId) { + if (COOKIES_FILE) { + const copy = path.join(CLIPS_DIR, `.cookies-${jobId}.txt`); + try { + await copyFile(COOKIES_FILE, copy); + } catch (err) { + throw new Error( + `Couldn't read the cookies file at ${COOKIES_FILE} (YTDLP_COOKIES)${err.code === "ENOENT" ? ": it isn't there" : ""}.`, + ); + } + return { args: ["--cookies", copy], cleanup: () => rm(copy, { force: true }) }; + } + if (COOKIES_FROM_BROWSER) { + return { args: ["--cookies-from-browser", COOKIES_FROM_BROWSER], cleanup: async () => {} }; + } + return { args: [], cleanup: async () => {} }; +} + async function download(db, job) { const template = path.join(CLIPS_DIR, `source-${job.id}.%(ext)s`); let file = null; let title = null; + const cookies = await cookieArgs(job.id); - await run( - YTDLP, - [ - job.source_url, - // YouTube needs a JavaScript runtime to list formats; the one - // running this server will do. - "--js-runtimes", - `node:${process.execPath}`, - "--no-playlist", - "--no-simulate", - "--newline", - "--progress", - "--progress-template", - "download:PROGRESS %(progress._percent_str)s", - "--print", - "before_dl:TITLE %(title)s", - "--print", - "after_move:FILE %(filepath)s", - "-f", - "bv*[height<=1080][ext=mp4]+ba[ext=m4a]/b[height<=1080][ext=mp4]/bv*[height<=1080]+ba/b", - "--merge-output-format", - "mp4", - "--max-filesize", - MAX_SOURCE_SIZE, - "--match-filter", - `duration <= ${MAX_SOURCE_SECONDS}`, - // Only when a path is configured: on the PATH, yt-dlp finds it. - ...(process.env.FFMPEG_PATH ? ["--ffmpeg-location", process.env.FFMPEG_PATH] : []), - "-o", - template, - ], - { - onLine: (line) => { - const percent = line.match(/^PROGRESS\s+([\d.]+)%/); - if (percent) update(db, job.id, { progress: Math.min(0.99, Number(percent[1]) / 100) }); - const t = line.match(/^TITLE (.+)$/); - if (t) title = t[1].slice(0, 300); - const f = line.match(/^FILE (.+)$/); - if (f) file = f[1].trim(); + try { + await run( + YTDLP, + [ + ...cookies.args, + job.source_url, + // YouTube needs a JavaScript runtime to list formats; the one + // running this server will do. + "--js-runtimes", + `node:${process.execPath}`, + "--no-playlist", + "--no-simulate", + "--newline", + "--progress", + "--progress-template", + "download:PROGRESS %(progress._percent_str)s", + "--print", + "before_dl:TITLE %(title)s", + "--print", + "after_move:FILE %(filepath)s", + "-f", + "bv*[height<=1080][ext=mp4]+ba[ext=m4a]/b[height<=1080][ext=mp4]/bv*[height<=1080]+ba/b", + "--merge-output-format", + "mp4", + "--max-filesize", + MAX_SOURCE_SIZE, + "--match-filter", + `duration <= ${MAX_SOURCE_SECONDS}`, + // Only when a path is configured: on the PATH, yt-dlp finds it. + ...(process.env.FFMPEG_PATH ? ["--ffmpeg-location", process.env.FFMPEG_PATH] : []), + "-o", + template, + ], + { + onLine: (line) => { + const percent = line.match(/^PROGRESS\s+([\d.]+)%/); + if (percent) update(db, job.id, { progress: Math.min(0.99, Number(percent[1]) / 100) }); + const t = line.match(/^TITLE (.+)$/); + if (t) title = t[1].slice(0, 300); + const f = line.match(/^FILE (.+)$/); + if (f) file = f[1].trim(); + }, }, - }, - ); + ); + + } finally { + await cookies.cleanup(); + } if (!file) throw new Error("The video couldn't be downloaded. It may be private, too long, or too big."); const info = await probe(file); diff --git a/src/lib/version.ts b/src/lib/version.ts index 00f63cd..0c38afe 100644 --- a/src/lib/version.ts +++ b/src/lib/version.ts @@ -7,4 +7,4 @@ box or a build banner can read the same value later. ═══════════════════════════════════════════════════════════════ */ -export const SITE_VERSION = "NGU-Web.v1.8.1-video-mgmt"; +export const SITE_VERSION = "NGU-Web.v1.8.2-video-mgmt";