Video frame stills for featured cards; newsletter Pick from fix
- Image fields take frameFrom/frameWhen: "Frame at" saves a still from the row's YouTube or Facebook link to /uploads/thumbnails/ (POST /api/admin/video-frame, grabFrame in clips.js). Used on video featured cards. - clock/parseClock moved to src/lib/clock.ts, shared with Video clips. - Newsletter: new drafts no longer store source 'events' (the Pick from select's blank choice), which showed as "events no longer exists"; older drafts load it as blank. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3b45944094
commit
b8012c75e9
9 changed files with 172 additions and 25 deletions
|
|
@ -49,7 +49,7 @@
|
|||
|
||||
import { spawn } from "node:child_process";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { access, constants, copyFile, mkdir, readdir, rm, stat } from "node:fs/promises";
|
||||
import { access, constants, copyFile, mkdir, readFile, readdir, rm, stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import { UPLOADS_DIR, PUBLIC_PREFIX } from "./media.js";
|
||||
|
|
@ -446,6 +446,63 @@ async function cut(db, job) {
|
|||
});
|
||||
}
|
||||
|
||||
/* ── A single frame ──────────────────────────────────────── */
|
||||
|
||||
/* One frame of a YouTube or Facebook video as a JPEG, for a card's
|
||||
picture. yt-dlp only finds the stream's address and ffmpeg seeks
|
||||
in it, so nothing is downloaded but that frame; light enough to
|
||||
run in the request, outside the queue. */
|
||||
export async function grabFrame(sourceUrl, seconds) {
|
||||
const link = checkSourceUrl(sourceUrl);
|
||||
if (!Number.isFinite(seconds) || seconds < 0 || seconds > MAX_SOURCE_SECONDS) {
|
||||
throw new ClipError(400, "That isn't a time in the video.");
|
||||
}
|
||||
await mkdir(CLIPS_DIR, { recursive: true });
|
||||
const id = `frame-${randomBytes(6).toString("hex")}`;
|
||||
const out = path.join(CLIPS_DIR, `${id}.jpg`);
|
||||
const cookies = await cookieArgs(id);
|
||||
let stream = null;
|
||||
try {
|
||||
await run(
|
||||
YTDLP,
|
||||
[
|
||||
"--ignore-config",
|
||||
...cookies.args,
|
||||
"--js-runtimes",
|
||||
`node:${process.execPath}`,
|
||||
"--no-playlist",
|
||||
"-f",
|
||||
"bv*[height<=1080]/b[height<=1080]/b",
|
||||
"--get-url",
|
||||
"--",
|
||||
link,
|
||||
],
|
||||
{ onLine: (line) => (stream ??= /^https:\/\//.test(line.trim()) ? line.trim() : null) },
|
||||
);
|
||||
} catch (err) {
|
||||
if (err instanceof ClipError) throw err;
|
||||
console.error(`frame ${link}: ${err.message}`);
|
||||
throw new ClipError(422, "The video couldn't be opened. It may be private or removed.");
|
||||
} finally {
|
||||
await cookies.cleanup();
|
||||
}
|
||||
if (!stream) throw new ClipError(422, "The video couldn't be opened. It may be private or removed.");
|
||||
|
||||
try {
|
||||
// -ss before -i seeks in the stream instead of reading up to it.
|
||||
await run(FFMPEG, ["-hide_banner", "-loglevel", "error", "-ss", String(seconds), "-i", stream, "-frames:v", "1", "-q:v", "2", "-y", out]);
|
||||
return await readFile(out);
|
||||
} catch (err) {
|
||||
// Past the end, ffmpeg finishes without writing anything.
|
||||
if (err.code === "ENOENT") throw new ClipError(422, "The video doesn't run that long.");
|
||||
if (err instanceof ClipError) throw err;
|
||||
console.error(`frame ${link} at ${seconds}: ${err.message}`);
|
||||
throw new ClipError(422, "The frame couldn't be taken from the video.");
|
||||
} finally {
|
||||
await rm(out, { force: true });
|
||||
}
|
||||
}
|
||||
|
||||
let running = false;
|
||||
let dbRef = null;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue