Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: preserve short audio and selected stream timing in exports (#13108)
Adam Eivy81317809e4d0
diff --git a/docs/features/fableloom-export-v1.md b/docs/features/fableloom-export-v1.mdindex 50986a56be..841355dd6e 100644--- a/docs/features/fableloom-export-v1.md+++ b/docs/features/fableloom-export-v1.md@@ -66,7 +66,10 @@ actual byte report, not bitrate multiplied by duration, is the package size.Two-pass loudness normalization targets −16 LUFS, −1.5 dBTP and LRA 11 for theexisting authored audio mix (including dialogue and SFX). Silence remains-silence; clips without audio remain silent. No stem separation, speech synthesis,+silence; clips without audio remain silent. Audio too short or sparse for a+finite integrated-loudness measurement uses peak normalization to −1.5 dBTP+without padding its duration. Discarded subtitle/data tracks do not extend the+retained picture/audio duration. No stem separation, speech synthesis,provider calls or new creative media generation occurs. Normalization changeslevel, not authored occupancy intervals. Oversized, invalid, failed or timed-outencodes fail the job visibly; clips are never shortened to fit the byte limit.diff --git a/server/services/fableLoom/exportMedia.js b/server/services/fableLoom/exportMedia.jsindex 9bbd5d632b..9330c8f6e4 100644--- a/server/services/fableLoom/exportMedia.js+++ b/server/services/fableLoom/exportMedia.js@@ -57,17 +57,27 @@ async function probe(path, format = 'mov') {if (!bin) throw refusal('EXPORT_FFMPEG_REQUIRED', 'Install ffmpeg and ffprobe before exporting video.');const { stdout } = await execFileAsync(bin, ['-v', 'error', '-protocol_whitelist', 'file', '-f', format, '-i', path,- '-show_entries', 'stream=codec_type,codec_name,pix_fmt,width,height:format=duration', '-of', 'json',+ '-show_entries', 'stream=codec_type,codec_name,pix_fmt,width,height,start_time,duration:format=duration', '-of', 'json',], safeChildProcessOptions({ timeout: 10_000, killSignal: 'SIGKILL', maxBuffer: 64 * 1024 }));const result = JSON.parse(stdout);const video = result.streams?.filter(s => s.codec_type === 'video') || [];const audio = result.streams?.filter(s => s.codec_type === 'audio') || [];- const durationMs = Math.round(Number(result.format?.duration) * 1000);- if (video.length !== 1 || audio.length > 1 || !Number.isSafeInteger(durationMs) || durationMs <= 0+ if (video.length !== 1 || audio.length > 1|| !Number.isSafeInteger(video[0].width * video[0].height) || video[0].width * video[0].height > 16 * 1024 * 1024) {throw refusal('EXPORT_MEDIA_INVALID', 'Video must have one bounded picture stream, optional audio and a valid duration.');}- return { video: video[0], audio: audio[0], durationMs };+ // MP4 container duration can include discarded subtitles/data. Only retained+ // picture/audio timelines determine clip length. WebM output has no extra tracks+ // but may omit stream durations, so its own container is a safe fallback.+ for (const stream of [...video, ...audio]) {+ const seconds = stream.duration === undefined && format === 'matroska'+ ? Number(result.format?.duration) : Number(stream.duration);+ stream.durationMs = Math.round((seconds + Math.max(0, Number(stream.start_time) || 0)) * 1000);+ if (!Number.isSafeInteger(stream.durationMs) || stream.durationMs <= 0) {+ throw refusal('EXPORT_MEDIA_INVALID', 'Retained video/audio duration could not be verified.');+ }+ }+ return { video: video[0], audio: audio[0], durationMs: Math.max(...[...video, ...audio].map(s => s.durationMs)) };}export async function fableLoomExportEncoder(kind) {@@ -96,6 +106,11 @@ async function audioFilter(bin, inputArgs, profile, signal) {const measurement = JSON.parse(stderr.slice(stderr.lastIndexOf('{'), stderr.lastIndexOf('}') + 1));// Applying loudnorm to digital silence can emit non-finite samples into AAC.if (measurement.input_i === '-inf' && measurement.input_tp === '-inf') return null;+ // Short SFX may never fill loudnorm's 400 ms window. Peak normalization+ // preserves their samples/timing without padding them to invent a LUFS value.+ if (measurement.input_i === '-inf' && Number.isFinite(Number(measurement.input_tp))) {+ return `volume=${profile.truePeakDb - Number(measurement.input_tp)}dB`;+ }const values = ['input_i', 'input_tp', 'input_lra', 'input_thresh', 'target_offset'].map(k => Number(measurement[k]));if (!values.every(Number.isFinite)) throw refusal('EXPORT_MEDIA_INVALID', 'Audio loudness could not be measured.');return `${loudness(profile)}:measured_I=${values[0]}:measured_TP=${values[1]}:measured_LRA=${values[2]}:measured_thresh=${values[3]}:offset=${values[4]}:linear=true`;@@ -157,7 +172,7 @@ export async function prepareFableLoomExportMedia(bytes, kind, directory, optionawait encode(bin, [...common, ...encoding, '-bsf:v', 'filter_units=remove_types=6','-fflags', '+bitexact', '-movflags', '+faststart', '-fs', String(FABLELOOM_EXPORT_LIMITS.maxAssetBytes), output], signal);const encoded = await probe(output);- if (encoded.durationMs < source.durationMs - 1) throw refusal('EXPORT_MEDIA_SIZE', 'Encoding truncated the clip. Use a smaller profile or shorter clip.');+ if (['video', 'audio'].some(kind => source[kind] && (!encoded[kind] || encoded[kind].durationMs < source[kind].durationMs - 1))) throw refusal('EXPORT_MEDIA_SIZE', 'Encoding truncated the clip. Use a smaller profile or shorter clip.');const result = { bytes: await outputBytes(output), extension: 'mp4', durationMs: encoded.durationMs };if (profile.id !== 'source') {const poster = join(directory, 'poster.png');diff --git a/server/services/fableLoom/exports.test.js b/server/services/fableLoom/exports.test.jsindex 16a4ec47b2..8d11b3fad9 100644--- a/server/services/fableLoom/exports.test.js+++ b/server/services/fableLoom/exports.test.js@@ -274,6 +274,53 @@ describe('portable FableLoom export boundary', () => {expect(noAudioAssets.holds[0].audioOccupancy).toEqual(silentAssets.holds[0].audioOccupancy);});+ videoIt('normalizes short non-silent audio without padding its duration', async () => {+ const source = join(PATHS.videos, 'short-sfx.mp4');+ expect(await runFfmpegProcess({ bin, timeoutMs: 10_000, args: [+ '-nostdin', '-y', '-v', 'error', '-f', 'lavfi', '-i', 'color=c=blue:s=32x32:r=10:d=1',+ '-f', 'lavfi', '-i', 'sine=frequency=440:sample_rate=48000:duration=0.2', '-af', 'volume=0.1',+ '-c:v', 'libx264', '-c:a', 'aac', '-threads', '1', source,+ ] })).toEqual({ ok: true });+ loadHistory.mockResolvedValue(['entry-record', 'hold-record', 'exit-record'].map(id => ({ id, filename: 'short-sfx.mp4' })));+ const result = await archive(await begin({ episodeIds: ['episode-one'], mediaProfile: 'web-720p' }));+ const assets = JSON.parse(result.files.get('manifest.json')).loom.episodes[0].nodes[0].playbackAssets;+ expect(assets.entry.durationMs).toBe(1000);+ const output = join(PATHS.videos, 'short-normalized.mp4');+ await writeFile(output, result.files.get(assets.entry.file));+ const streams = JSON.parse(execFileSync(probe, ['-v', 'error', '-show_streams', '-of', 'json', output], { timeout: 10_000 }).toString()).streams;+ const audio = streams.find(stream => stream.codec_type === 'audio');+ expect(Number(audio.duration)).toBeGreaterThanOrEqual(0.2);+ expect(Number(audio.duration)).toBeLessThan(0.25);+ const analyzed = await runFfmpegProcess({ bin, timeoutMs: 10_000, returnStderr: true, stderrTailBytes: 8192,+ args: ['-nostdin', '-i', output, '-vn', '-af', 'loudnorm=I=-16:TP=-1.5:LRA=11:print_format=json', '-f', 'null', '-'] });+ expect(analyzed.ok).toBe(true);+ const level = JSON.parse(analyzed.stderr.slice(analyzed.stderr.lastIndexOf('{'), analyzed.stderr.lastIndexOf('}') + 1));+ expect(Number(level.input_tp)).toBeGreaterThan(-3);+ expect(Number(level.input_tp)).toBeLessThan(0);+ });++ videoIt('measures retained streams when discarded subtitle tracks outlast picture and audio', async () => {+ const subtitle = join(PATHS.videos, 'extended.srt');+ await writeFile(subtitle, '1\n00:00:00,000 --> 00:00:03,000\nExample subtitle\n');+ const source = join(PATHS.videos, 'extended-subtitle.mp4');+ expect(await runFfmpegProcess({ bin, timeoutMs: 10_000, args: ['-nostdin', '-y', '-v', 'error',+ '-i', join(PATHS.videos, 'independent-name.mp4'), '-i', subtitle, '-map', '0', '-map', '1:0',+ '-c', 'copy', '-c:s', 'mov_text', source] })).toEqual({ ok: true });+ const measured = JSON.parse(execFileSync(probe, ['-v', 'error', '-show_streams', '-of', 'json', source], { timeout: 10_000 }).toString());+ expect(Number(measured.streams.find(stream => stream.codec_type === 'subtitle').duration)).toBe(3);+ loadHistory.mockResolvedValue(['entry-record', 'hold-record', 'exit-record'].map(id => ({ id, filename: 'extended-subtitle.mp4' })));+ for (const mediaProfile of ['source', 'web-720p']) {+ const result = await archive(await begin({ episodeIds: ['episode-one'], mediaProfile }));+ const assets = JSON.parse(result.files.get('manifest.json')).loom.episodes[0].nodes[0].playbackAssets;+ expect(assets.entry.durationMs).toBe(1000);+ expect(assets.holds[0].audioOccupancy.durationMs).toBe(1000);+ const output = join(PATHS.videos, 'retained.mp4');+ await writeFile(output, result.files.get(assets.entry.file));+ const streams = JSON.parse(execFileSync(probe, ['-v', 'error', '-show_streams', '-of', 'json', output], { timeout: 10_000 }).toString()).streams;+ expect(streams.map(stream => stream.codec_type)).toEqual(['video', 'audio']);+ }+ });+videoIt('drains cancelled and failed encoder children before exposing terminal failure without an artifact', async () => {const realRun = ffmpeg.runFfmpegProcess;const started = Promise.withResolvers();