Video clip tool (v1.8.3-video-mgmt) #13

Merged
ngu-git-admin merged 7 commits from feature-tools into main 2026-09-29 03:38:40 +01:00
2 changed files with 106 additions and 41 deletions
Showing only changes of commit a393bde2da - Show all commits

View file

@ -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:

View file

@ -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,14 +245,38 @@ 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);
try {
await run(
YTDLP,
[
...cookies.args,
job.source_url,
// YouTube needs a JavaScript runtime to list formats; the one
// running this server will do.
@ -277,6 +317,10 @@ async function download(db, job) {
},
);
} 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);
const { size } = await stat(file);