Still-picture songs save and play as audio plus one picture

This commit is contained in:
Jonathan Sykes
2026-10-17 07:00:22 +08:00
parent 2e6c79e360
commit eef9fbf05f
10 changed files with 262 additions and 24 deletions

View File

@@ -200,6 +200,9 @@ export function createMediaCache({
const optQueue = [];
const optPending = new Set();
let optActive = null; // videoId currently being re-encoded
const stillQueue = []; // copies whose still-picture check hasn't run yet
const stillChecks = new Map(); // videoId -> running check (dedupes the two lanes)
let stillActive = false;
const lastTouch = new Map();
function sweepTmp(prefix) {
@@ -216,7 +219,7 @@ export function createMediaCache({
let names = [];
try { names = readdirSync(dir); } catch { return; }
for (const n of names) {
const m = /^(.+)\.(\d+)\.(mp4|m4a)$/.exec(n);
const m = /^(.+)\.(\d+)\.(mp4|m4a|jpg)$/.exec(n);
if (!m || m[1] !== id || (keepGen !== null && Number(m[2]) === keepGen)) continue;
const p = join(dir, n);
const rm = () => { try { unlinkSync(p); } catch { /* gone */ } };
@@ -360,13 +363,14 @@ export function createMediaCache({
status: 'ready', gen,
size: probe.size + sizeOf(fileFor(id, gen, 'm4a')),
height: probe.height, vcodec: probe.vcodec, acodec: probe.acodec, duration: probe.duration,
optimized: 0, meta: JSON.stringify(metaFromInfo(info, probe.duration)),
optimized: 0, still: null, audio_sha256: null, meta: JSON.stringify(metaFromInfo(info, probe.duration)),
attempts: 0, error: null, retry_at: 0, updated_at: t, last_access: t, sha256,
};
await db.upsertMedia(id, fields);
removeFiles(id, gen);
job.status = 'ready';
log.info?.(`[media] cached ${id} ${probe.height}p ${fmtMB(fields.size)}`);
queueStill(id);
enqueueOptimize(id);
notifyReady(id, gen, sha256, probe, fields.meta);
return { ...(prev || {}), video_id: id, ...fields };
@@ -428,7 +432,7 @@ export function createMediaCache({
};
// Very long videos would tie the lane up for hours on 2 niced threads.
if (transcode.maxSeconds && row.duration > transcode.maxSeconds) { await markDone(); return; }
const still = row.duration >= STILL_MIN_DURATION && await isStill(src).catch(() => false);
const still = (await checkStill(id)) === 1;
if (still) return optimizeStill(id, row, src, markDone);
// Rev-2 copies already went through the HEVC lane; only the still check was new.
if ((row.optimized || 0) >= 2) { await markDone(); return; }
@@ -494,6 +498,8 @@ export function createMediaCache({
const oldAudio = fileFor(id, row.gen, 'm4a');
const newAudio = fileFor(id, gen, 'm4a');
try { linkSync(oldAudio, newAudio); } catch { copyFileSync(oldAudio, newAudio); }
const oldArt = fileFor(id, row.gen, 'jpg');
if (existsSync(oldArt)) { try { linkSync(oldArt, fileFor(id, gen, 'jpg')); } catch { copyFileSync(oldArt, fileFor(id, gen, 'jpg')); } }
await db.upsertMedia(id, {
gen, size: probe.size + sizeOf(newAudio), height: probe.height, vcodec: probe.vcodec,
duration: probe.duration, optimized: OPT_REV, updated_at: now(), sha256,
@@ -503,12 +509,74 @@ export function createMediaCache({
log.info?.(`[media] optimized ${id} (${label}): ${fmtMB(oldSize)} → ${fmtMB(probe.size)} (${probe.vcodec})`);
}
// Still-picture check, its own lane: keyframes only (~1 s a song), so it runs
// right after a copy lands instead of waiting behind the re-encode queue.
// A still copy gets its picture (<id>.<gen>.jpg) and the m4a's hash, which
// is what lets a device save audio + picture instead of the video.
function queueStill(id) {
if (!stillQueue.includes(id)) stillQueue.push(id);
pumpStill();
}
function pumpStill() {
if (stillActive || !stillQueue.length) return;
stillActive = true;
const id = stillQueue.shift();
checkStill(id)
.catch((err) => log.warn?.(`[media] still check ${id} failed: ${err.message}`))
.finally(() => { stillActive = false; pumpStill(); });
}
// Resolves to the row's still flag (1 | 0), or null when there is no copy.
function checkStill(id) {
if (stillChecks.has(id)) return stillChecks.get(id);
const p = (async () => {
const row = await db.getMedia(id);
if (!row || row.status !== 'ready') return null;
if (row.still === 0 || row.still === 1) return row.still;
const src = fileFor(id, row.gen);
if (!existsSync(src)) return null;
const fields = { still: 0 };
const prefix = `${id}-${now()}-art`;
try {
if (row.duration >= STILL_MIN_DURATION && await isStill(src).catch(() => false)) {
const tmp = join(tmpDir, `${prefix}.jpg`);
const at = String(Math.min(30, Math.floor(row.duration / 2)));
const args = ['-v', 'error', '-nostdin', '-y', '-ss', at, '-i', src, '-frames:v', '1',
'-vf', 'scale=-2:min(720\\,ih)', '-q:v', '2', tmp];
const f = nice ? await run(nice, ['-n', '19', ffmpeg, ...args]) : await run(ffmpeg, args);
if (f.code !== 0 || sizeOf(tmp) < 1024) throw new Error('picture grab failed: ' + tail(f.stderr));
const audioSha = await hashFile(fileFor(id, row.gen, 'm4a'));
const cur = await db.getMedia(id);
if (!cur || cur.status !== 'ready' || cur.gen !== row.gen) return null; // replaced meanwhile
renameSync(tmp, fileFor(id, row.gen, 'jpg'));
Object.assign(fields, { still: 1, audio_sha256: audioSha });
}
} catch (err) {
log.warn?.(`[media] still check ${id}: ${err.message}`);
return null; // left unchecked: retried at the next boot
} finally {
sweepTmp(prefix);
}
const cur = await db.getMedia(id);
if (!cur || cur.status !== 'ready' || cur.gen !== row.gen) return null;
await db.upsertMedia(id, fields);
if (fields.still) log.info?.(`[media] still picture ${id}: devices can save audio + picture`);
return fields.still;
})().finally(() => stillChecks.delete(id));
stillChecks.set(id, p);
return p;
}
// Keyframes only: ~1 s for a 5-minute video. Every keyframe-to-keyframe
// scene score must stay under STILL_MAX_SCENE.
async function isStill(src) {
const r = await run(ffmpeg, ['-hide_banner', '-nostdin', '-skip_frame', 'nokey', '-i', src, '-an',
'-vf', "scale=160:-2,select='gte(scene\\,0)',metadata=print:key=lavfi.scene_score", '-f', 'null', '-']);
if (r.code !== 0) return false;
// Our own still encode (one picture at 1 fps, a keyframe every 10 min) has
// too few keyframes to score; no YouTube upload is 1 fps.
if (/Stream #0:\d+[^\n]*Video:[^\n]*\b1 fps\b/.test(String(r.stderr))) return true;
const scores = [...String(r.stderr).matchAll(/scene_score=([\d.]+)/g)].map((m) => Number(m[1]));
return scores.length + 1 >= STILL_MIN_KEYFRAMES && scores.every((x) => x <= STILL_MAX_SCENE);
}
@@ -527,10 +595,16 @@ export function createMediaCache({
// a few seconds early, so read the audio stream's own length.
const pa = await run(ffprobe, ['-v', 'error', '-select_streams', 'a:0', '-show_entries', 'stream=duration', '-of', 'csv=p=0', src]);
const seconds = Number(String(pa.stdout).trim()) > 0 ? Number(String(pa.stdout).trim()) : row.duration;
const f = await niced(['-v', 'error', '-nostdin', '-y', '-ss', at, '-i', src, '-frames:v', '1',
'-vf', 'scale=-2:min(720\\,ih)', frame]);
if (f.code !== 0) throw new Error('still frame grab failed: ' + tail(f.stderr));
const r = await niced(['-v', 'error', '-nostdin', '-y', '-loop', '1', '-framerate', '1', '-i', frame, '-i', src,
// The picture devices show (<id>.<gen>.jpg) is the frame to loop, so the
// saved-as-audio copy and the video copy look the same.
let picture = fileFor(id, row.gen, 'jpg');
if (!existsSync(picture)) {
picture = frame;
const f = await niced(['-v', 'error', '-nostdin', '-y', '-ss', at, '-i', src, '-frames:v', '1',
'-vf', 'scale=-2:min(720\\,ih)', frame]);
if (f.code !== 0) throw new Error('still frame grab failed: ' + tail(f.stderr));
}
const r = await niced(['-v', 'error', '-nostdin', '-y', '-loop', '1', '-framerate', '1', '-i', picture, '-i', src,
'-map', '0:v', '-map', '1:a:0', '-t', String(seconds),
'-c:v', 'libx264', '-preset', 'medium', '-crf', String(transcode.crf), '-tune', 'stillimage',
'-profile:v', 'high', '-level:v', '4.0', '-pix_fmt', 'yuv420p', '-g', '600', '-threads', threads,
@@ -747,7 +821,7 @@ export function createMediaCache({
const byId = new Map(rows.map((r) => [r.video_id, r]));
for (const name of readdirSync(dir)) {
if (name === '.tmp') continue;
const m = /^(.+)\.(\d+)\.(mp4|m4a)$/.exec(name);
const m = /^(.+)\.(\d+)\.(mp4|m4a|jpg)$/.exec(name);
const r = m && byId.get(m[1]);
if (!m || !r || r.status !== 'ready' || r.gen !== Number(m[2])) {
try { unlinkSync(join(dir, name)); } catch { /* gone */ }
@@ -757,6 +831,7 @@ export function createMediaCache({
for (const r of rows) {
if (r.status === 'ready') {
if (!readyFilesExist(r)) { await db.deleteMedia(r.video_id); removeFiles(r.video_id); continue; }
if (r.still === null || r.still === undefined) queueStill(r.video_id);
if ((r.optimized || 0) < OPT_REV) enqueueOptimize(r.video_id);
} else if (r.status === 'queued' || r.status === 'downloading' || r.status === 'validating') {
startJob(r.video_id, r.priority === HIGH ? HIGH : LOW, !!r.auto, r);