feat(media): media put seals durationMs, width and height measured by ffprobe
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -35,7 +35,7 @@ import type { MediaMeta } from './sealed.js';
|
||||
import { MediaSplitter } from './splitter.js';
|
||||
import { IMAGE_MIME, ImageStreamSplitter } from './image-stream.js';
|
||||
import { SUBTITLE_EXTENSIONS, subtitleFor, subtitlesOf, withSubtitles } from './subtitles.js';
|
||||
import { liveEncodePlan, prepareForWeb, probeLive, webIssues } from './web-prep.js';
|
||||
import { describeMedia, liveEncodePlan, prepareForWeb, probeLive, webIssues } from './web-prep.js';
|
||||
import { MediaLibrary } from './library.js';
|
||||
import { PutJobs, stillServed } from './put-jobs.js';
|
||||
import type { PutJob } from './put-jobs.js';
|
||||
@@ -355,6 +355,8 @@ async function sealAndPut(ctx: PutCtx, file: string, flags: Flags, as: PutAs, ke
|
||||
}
|
||||
if (tracks.length > 0) meta = withSubtitles(meta, tracks);
|
||||
}
|
||||
// Measured on what is sealed (after --web), so the numbers match the file a viewer gets.
|
||||
if (/^(video|audio|image)\//.test(meta.mime ?? '')) meta = { ...meta, ...(await describeMedia(source)) };
|
||||
const size = (await stat(source)).size;
|
||||
// A job's sealed copy is kept for the next run; anything else is temporary.
|
||||
const sealed = key !== null ? ctx.jobs.sealedPath(key) : join(tmpdir(), `shade-seal-${toHex(randomBytes(8))}.smf`);
|
||||
|
||||
@@ -69,6 +69,40 @@ export async function probe(input: string): Promise<ProbeStream[]> {
|
||||
return (JSON.parse(out) as { streams?: ProbeStream[] }).streams ?? [];
|
||||
}
|
||||
|
||||
/** What a viewer wants to know before opening a file: its length and picture size. */
|
||||
export interface MediaFacts {
|
||||
durationMs?: number;
|
||||
width?: number;
|
||||
height?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Length and picture size from ffprobe, for the meta a file is sealed with:
|
||||
* a viewer can then show "1:28" on a tile and reserve the right shape
|
||||
* without fetching a byte of the film. Empty without ffprobe or when the
|
||||
* file says nothing (a still picture has no length, a sound no size).
|
||||
*/
|
||||
export async function describeMedia(input: string): Promise<MediaFacts> {
|
||||
if (Bun.which('ffprobe') === null) return {};
|
||||
let parsed: { streams?: (ProbeStream & { width?: number; height?: number; duration?: string })[]; format?: { duration?: string; format_name?: string } };
|
||||
try {
|
||||
parsed = JSON.parse(await run(['ffprobe', '-v', 'error', '-show_streams', '-show_format', '-of', 'json', input]));
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
const facts: MediaFacts = {};
|
||||
const picture = parsed.streams?.find((s) => s.codec_type === 'video' && s.disposition?.attached_pic !== 1);
|
||||
if (picture?.width !== undefined && picture.height !== undefined && picture.width > 0 && picture.height > 0) {
|
||||
facts.width = picture.width;
|
||||
facts.height = picture.height;
|
||||
}
|
||||
// Image formats report a nominal length (one frame, or a GIF's loop); only timed media gets one.
|
||||
const still = /image2|_pipe$|png|webp|gif/.test(parsed.format?.format_name ?? '');
|
||||
const seconds = Number(parsed.format?.duration ?? picture?.duration);
|
||||
if (!still && Number.isFinite(seconds) && seconds > 0) facts.durationMs = Math.round(seconds * 1000);
|
||||
return facts;
|
||||
}
|
||||
|
||||
/**
|
||||
* What in this file browsers will not play, in plain words; empty if it
|
||||
* plays as it is. Without ffprobe nothing can be said, and nothing is.
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, test, expect, afterAll } from 'bun:test';
|
||||
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { liveEncodePlan, prepareForWeb, probe } from '../src/web-prep.js';
|
||||
import { describeMedia, liveEncodePlan, prepareForWeb, probe } from '../src/web-prep.js';
|
||||
import type { ProbeStream } from '../src/web-prep.js';
|
||||
|
||||
const hasFfmpeg = Bun.which('ffmpeg') !== null && Bun.which('ffprobe') !== null;
|
||||
@@ -105,3 +105,25 @@ describe('liveEncodePlan (live video --input)', () => {
|
||||
expect(() => liveEncodePlan('x', [], { realtime: false })).toThrow(/no audio or video/);
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!hasFfmpeg)('describeMedia (the meta media put seals)', () => {
|
||||
test('a film has length and size, a sound only length, a picture only size', async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'shade-describe-'));
|
||||
dirs.push(dir);
|
||||
const film = join(dir, 'film.mp4');
|
||||
const sound = join(dir, 'sound.ogg');
|
||||
const still = join(dir, 'still.webp');
|
||||
await ff(['-f', 'lavfi', '-i', 'testsrc=size=320x180:rate=25', '-t', '2.5', '-c:v', 'libx264', '-pix_fmt', 'yuv420p', film]);
|
||||
await ff(['-f', 'lavfi', '-i', 'sine=frequency=440:sample_rate=48000', '-t', '1.5', '-c:a', 'libvorbis', sound]);
|
||||
await ff(['-f', 'lavfi', '-i', 'testsrc=size=64x48', '-frames:v', '1', still]);
|
||||
|
||||
const f = await describeMedia(film);
|
||||
expect([f.width, f.height]).toEqual([320, 180]);
|
||||
expect(Math.abs(f.durationMs! - 2500)).toBeLessThan(80);
|
||||
const a = await describeMedia(sound);
|
||||
expect(a.width).toBeUndefined();
|
||||
expect(Math.abs(a.durationMs! - 1500)).toBeLessThan(80);
|
||||
expect(await describeMedia(still)).toEqual({ width: 64, height: 48 });
|
||||
expect(await describeMedia(join(dir, 'missing.mp4'))).toEqual({});
|
||||
}, 30_000);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user