@@ -29,11 +29,14 @@ import {
2929import { tmpdir } from "node:os" ;
3030import { join } from "node:path" ;
3131import {
32+ applyFaststart ,
33+ muxVideoWithAudio ,
3234 PROVENANCE_RENDERER_NAME ,
3335 PROVENANCE_VERSION ,
3436 readRenderProvenance ,
3537 renderProvenanceArgs ,
3638} from "@hyperframes/engine" ;
39+ import { padOrTrimAudioToVideoFrameCount } from "../render/audioPadTrim.js" ;
3740import type { ChunkSliceJson } from "../render/stages/freezePlan.js" ;
3841import { assemble , cfrRateControlArgs } from "./assemble.js" ;
3942import type { DistributedFormat } from "./shared.js" ;
@@ -91,14 +94,14 @@ function buildPlanDir(
9194 * independently concatenable because GOP === frame count and the first
9295 * frame is forced as a keyframe.
9396 */
94- function makeMp4Chunk ( outputPath : string , frameCount : number ) : void {
97+ function makeMp4Chunk ( outputPath : string , frameCount : number , size = "160x120" ) : void {
9598 const args = [
9699 "-v" ,
97100 "error" ,
98101 "-f" ,
99102 "lavfi" ,
100103 "-i" ,
101- `testsrc=size=160x120 :rate=30:duration=${ frameCount / 30 } ` ,
104+ `testsrc=size=${ size } :rate=30:duration=${ frameCount / 30 } ` ,
102105 "-c:v" ,
103106 "libx264" ,
104107 "-preset" ,
@@ -229,6 +232,36 @@ function probeStream(
229232 return parsed . streams ?. [ 0 ] ?? null ;
230233}
231234
235+ /** Per-packet pts, dts, duration, size and payload hash of one stream. */
236+ function streamPackets ( path : string , stream : "v" | "a" ) : string {
237+ const result = spawnSync (
238+ "ffmpeg" ,
239+ [ "-v" , "error" , "-i" , path , "-map" , `0:${ stream } :0` , "-c" , "copy" , "-f" , "framemd5" , "-" ] ,
240+ { stdio : "pipe" } ,
241+ ) ;
242+ if ( result . status !== 0 ) throw new Error ( result . stderr . toString ( ) . slice ( - 400 ) ) ;
243+ return result . stdout . toString ( ) ;
244+ }
245+
246+ /** Three 1 s chunks plus audio 0.5 s too long, so the trim branch runs. */
247+ function buildAudioFixture ( ) : { planDir : string ; chunkPaths : string [ ] ; audioPath : string } {
248+ const chunks : ChunkSliceJson [ ] = [ 0 , 1 , 2 ] . map ( ( index ) => ( {
249+ index,
250+ startFrame : index * 30 ,
251+ endFrame : ( index + 1 ) * 30 ,
252+ } ) ) ;
253+ const planDir = buildPlanDir ( "mp4" , chunks , 90 , true ) ;
254+ const chunkPaths = chunks . map ( ( { index } ) => join ( planDir , `chunk-${ index } .mp4` ) ) ;
255+ for ( const chunkPath of chunkPaths ) makeMp4Chunk ( chunkPath , 30 , "640x360" ) ;
256+ const audioPath = join ( planDir , "audio.m4a" ) ;
257+ makeAacAudio ( audioPath , 3.5 ) ;
258+ return { planDir, chunkPaths, audioPath } ;
259+ }
260+
261+ function diskKb ( path : string ) : number {
262+ return Number ( spawnSync ( "du" , [ "-sk" , path ] ) . stdout . toString ( ) . split ( "\t" ) [ 0 ] ) ;
263+ }
264+
232265describe ( "assemble()" , ( ) => {
233266 const TIMEOUT_MS = 30_000 ;
234267
@@ -484,6 +517,86 @@ describe("assemble()", () => {
484517 TIMEOUT_MS ,
485518 ) ;
486519
520+ it (
521+ "keeps local disk under the chunks plus one copy of the output" ,
522+ async ( ) => {
523+ if ( ! hasFfmpeg || process . platform === "win32" ) return ;
524+ const { planDir, chunkPaths, audioPath } = buildAudioFixture ( ) ;
525+ const outputPath = join ( planDir , "output.mp4" ) ;
526+ // Every intermediate is complete when its ffmpeg exits, so the disk
527+ // total at each exit bounds the peak.
528+ const diskLog = join ( planDir , "disk.log" ) ;
529+ const wrapper = join ( runRoot , `ffmpeg-disk-${ Date . now ( ) } .sh` ) ;
530+ const realFfmpeg = process . env . HYPERFRAMES_FFMPEG_PATH ?. trim ( ) || "ffmpeg" ;
531+ writeFileSync (
532+ wrapper ,
533+ `#!/bin/sh\n"${ realFfmpeg } " "$@"\nstatus=$?\ndu -sk '${ planDir } ' | cut -f1 >> '${ diskLog } '\nexit $status\n` ,
534+ { mode : 0o755 } ,
535+ ) ;
536+ const baselineKb = diskKb ( planDir ) ;
537+ const previous = process . env . HYPERFRAMES_FFMPEG_PATH ;
538+ process . env . HYPERFRAMES_FFMPEG_PATH = wrapper ;
539+ try {
540+ await assemble ( planDir , chunkPaths , audioPath , outputPath ) ;
541+ } finally {
542+ if ( previous === undefined ) delete process . env . HYPERFRAMES_FFMPEG_PATH ;
543+ else process . env . HYPERFRAMES_FFMPEG_PATH = previous ;
544+ }
545+ const peakKb = Math . max ( ...readFileSync ( diskLog , "utf8" ) . trim ( ) . split ( "\n" ) . map ( Number ) ) ;
546+ const outputKb = statSync ( outputPath ) . size / 1024 ;
547+ expect ( peakKb - baselineKb ) . toBeLessThan ( outputKb * 1.5 ) ;
548+ } ,
549+ TIMEOUT_MS ,
550+ ) ;
551+
552+ it (
553+ "writes the same video and audio packets as separate concat, mux and faststart passes" ,
554+ async ( ) => {
555+ if ( ! hasFfmpeg ) return ;
556+ const { planDir, chunkPaths, audioPath } = buildAudioFixture ( ) ;
557+ const outputPath = join ( planDir , "output.mp4" ) ;
558+ await assemble ( planDir , chunkPaths , audioPath , outputPath ) ;
559+
560+ const concatList = join ( planDir , "legacy-list.txt" ) ;
561+ writeFileSync ( concatList , chunkPaths . map ( ( path ) => `file '${ path } '` ) . join ( "\n" ) ) ;
562+ const concatPath = join ( planDir , "legacy-concat.mp4" ) ;
563+ const concat = spawnSync ( "ffmpeg" , [
564+ ...[ "-v" , "error" , "-r" , "30" , "-f" , "concat" , "-safe" , "0" , "-i" , concatList ] ,
565+ ...[ "-c" , "copy" , ...renderProvenanceArgs ( concatPath ) , "-y" , concatPath ] ,
566+ ] ) ;
567+ expect ( concat . status ) . toBe ( 0 ) ;
568+ const paddedPath = join ( planDir , "legacy-audio.m4a" ) ;
569+ expect (
570+ (
571+ await padOrTrimAudioToVideoFrameCount ( {
572+ videoPath : concatPath ,
573+ audioPath,
574+ outputPath : paddedPath ,
575+ } )
576+ ) . success ,
577+ ) . toBe ( true ) ;
578+ const fps = { num : 30 , den : 1 } ;
579+ const muxPath = join ( planDir , "legacy-mux.mp4" ) ;
580+ const mux = await muxVideoWithAudio (
581+ concatPath ,
582+ paddedPath ,
583+ muxPath ,
584+ undefined ,
585+ { audioCodec : "aac" } ,
586+ fps ,
587+ ) ;
588+ expect ( mux . success ) . toBe ( true ) ;
589+ const legacyPath = join ( planDir , "legacy.mp4" ) ;
590+ expect ( ( await applyFaststart ( muxPath , legacyPath , undefined , undefined , fps ) ) . success ) . toBe (
591+ true ,
592+ ) ;
593+
594+ expect ( streamPackets ( outputPath , "v" ) ) . toBe ( streamPackets ( legacyPath , "v" ) ) ;
595+ expect ( streamPackets ( outputPath , "a" ) ) . toBe ( streamPackets ( legacyPath , "a" ) ) ;
596+ } ,
597+ TIMEOUT_MS ,
598+ ) ;
599+
487600 it (
488601 "cfr:true re-encodes for exact avg_frame_rate matching r_frame_rate" ,
489602 async ( ) => {
0 commit comments