@@ -83,7 +83,7 @@ vi.mock("../utils/ffprobe.js", async (importOriginal) => {
8383
8484import { RATE_RANGE } from "@hyperframes/core/audio-automation" ;
8585import { AudioFxRenderError } from "./audioFxRender.js" ;
86- import { parseAudioElements , processCompositionAudio } from "./audioMixer.js" ;
86+ import { MAX_MIX_INPUTS , parseAudioElements , processCompositionAudio } from "./audioMixer.js" ;
8787import { encodeAac } from "./aacEncode.js" ;
8888
8989describe ( "parseAudioElements strict literal timing" , ( ) => {
@@ -1093,6 +1093,140 @@ describe("processCompositionAudio", () => {
10931093 expect ( runFfmpegMock ) . toHaveBeenCalledTimes ( 2 ) ;
10941094 } ) ;
10951095
1096+ it ( "keeps how long a killed group mix ran and ffmpeg's last words" , async ( ) => {
1097+ const baseDir = mkdtempSync ( join ( tmpdir ( ) , "hf-audio-base-" ) ) ;
1098+ const workDir = mkdtempSync ( join ( tmpdir ( ) , "hf-audio-work-" ) ) ;
1099+ tempDirs . push ( baseDir , workDir ) ;
1100+ writeFileSync ( join ( baseDir , "voice.wav" ) , "stub" ) ;
1101+
1102+ const streamLines = Array . from (
1103+ { length : 400 } ,
1104+ ( _ , i ) =>
1105+ ` Stream #${ i } :0 -> #0:${ i } (pcm_s16le (native) -> atrim (graph 0 input from stream ${ i } :0))` ,
1106+ ) ;
1107+ runFfmpegMock
1108+ . mockResolvedValueOnce ( {
1109+ success : true ,
1110+ durationMs : 1 ,
1111+ stderr : "" ,
1112+ exitCode : 0 ,
1113+ terminationReason : "exit" ,
1114+ } )
1115+ . mockResolvedValueOnce ( {
1116+ success : false ,
1117+ durationMs : 530_000 ,
1118+ stderr : [ ...streamLines , "Exiting normally, received signal 15." ] . join ( "\n" ) ,
1119+ exitCode : 255 ,
1120+ signal : null ,
1121+ terminationReason : "exit" ,
1122+ failureReason : "external_interruption" ,
1123+ } ) ;
1124+
1125+ const result = await processCompositionAudio (
1126+ [
1127+ {
1128+ id : "voice" ,
1129+ src : "voice.wav" ,
1130+ start : 0 ,
1131+ end : 2 ,
1132+ mediaStart : 0 ,
1133+ layer : 0 ,
1134+ volume : 1 ,
1135+ type : "audio" ,
1136+ groupId : "sfx" ,
1137+ } ,
1138+ ] ,
1139+ baseDir ,
1140+ workDir ,
1141+ join ( baseDir , "out.m4a" ) ,
1142+ 2 ,
1143+ ) ;
1144+
1145+ const failure = result . failures ?. find ( ( f ) => f . elementId === "sfx" ) ;
1146+ expect ( failure ?. reason ) . toBe ( "external_interruption" ) ;
1147+ expect ( failure ?. subprocess ) . toMatchObject ( { exitCode : 255 } ) ;
1148+ expect ( failure ?. detail ) . toContain ( "ran 530000 ms" ) ;
1149+ expect ( failure ?. detail ) . toContain ( "Exiting normally, received signal 15." ) ;
1150+ } ) ;
1151+
1152+ it . each ( [
1153+ [ "ungrouped" , undefined ] ,
1154+ [ "grouped" , "sfx" ] ,
1155+ ] ) (
1156+ "never hands one ffmpeg more inputs than a graph holds, %s" ,
1157+ async ( _name , groupId ) => {
1158+ const baseDir = mkdtempSync ( join ( tmpdir ( ) , "hf-audio-base-" ) ) ;
1159+ const workDir = mkdtempSync ( join ( tmpdir ( ) , "hf-audio-work-" ) ) ;
1160+ tempDirs . push ( baseDir , workDir ) ;
1161+ const trackCount = 2_000 ;
1162+ const elements = Array . from ( { length : trackCount } , ( _ , i ) => {
1163+ writeFileSync ( join ( baseDir , `clip-${ i } .wav` ) , "stub" ) ;
1164+ return {
1165+ id : `clip-${ i } ` ,
1166+ src : `clip-${ i } .wav` ,
1167+ start : i * 0.1 ,
1168+ end : i * 0.1 + 0.5 ,
1169+ mediaStart : 0 ,
1170+ layer : i ,
1171+ volume : 1 ,
1172+ type : "audio" as const ,
1173+ ...( groupId ? { groupId } : { } ) ,
1174+ } ;
1175+ } ) ;
1176+ runFfmpegMock . mockClear ( ) ;
1177+
1178+ const result = await processCompositionAudio (
1179+ elements ,
1180+ baseDir ,
1181+ workDir ,
1182+ join ( baseDir , "out.m4a" ) ,
1183+ trackCount * 0.1 + 0.5 ,
1184+ ) ;
1185+
1186+ // The mocked ffmpeg writes no files, so only the ungrouped mix runs to the end.
1187+ if ( ! groupId ) expect ( result . success ) . toBe ( true ) ;
1188+ const inputsPerCall = runFfmpegMock . mock . calls . map (
1189+ ( call ) => ( call [ 0 ] as string [ ] ) . filter ( ( arg ) => arg === "-i" ) . length ,
1190+ ) ;
1191+ expect ( Math . max ( ...inputsPerCall ) ) . toBeLessThanOrEqual ( MAX_MIX_INPUTS ) ;
1192+ } ,
1193+ 60_000 ,
1194+ ) ;
1195+
1196+ it ( "lets a mix's ffmpeg deadline grow with the film length" , async ( ) => {
1197+ const baseDir = mkdtempSync ( join ( tmpdir ( ) , "hf-audio-base-" ) ) ;
1198+ const workDir = mkdtempSync ( join ( tmpdir ( ) , "hf-audio-work-" ) ) ;
1199+ tempDirs . push ( baseDir , workDir ) ;
1200+ writeFileSync ( join ( baseDir , "voice.wav" ) , "stub" ) ;
1201+ const voice = {
1202+ id : "voice" ,
1203+ src : "voice.wav" ,
1204+ start : 0 ,
1205+ end : 2 ,
1206+ mediaStart : 0 ,
1207+ layer : 0 ,
1208+ volume : 1 ,
1209+ type : "audio" as const ,
1210+ } ;
1211+ const mixTimeout = async ( totalDuration : number ) => {
1212+ runFfmpegMock . mockClear ( ) ;
1213+ await processCompositionAudio (
1214+ [ voice ] ,
1215+ baseDir ,
1216+ workDir ,
1217+ join ( baseDir , "out.m4a" ) ,
1218+ totalDuration ,
1219+ ) ;
1220+ const mixCall = runFfmpegMock . mock . calls . find ( ( call ) =>
1221+ ( call [ 0 ] as string [ ] ) . includes ( "-filter_complex_script" ) ,
1222+ ) ;
1223+ return ( mixCall ?. [ 1 ] as { timeout ?: number } | undefined ) ?. timeout ;
1224+ } ;
1225+
1226+ expect ( await mixTimeout ( 60 ) ) . toBe ( 300_000 ) ;
1227+ expect ( await mixTimeout ( 36_000 ) ) . toBe ( 3_600_000 ) ;
1228+ } ) ;
1229+
10961230 it ( "preserves and classifies unsupported FFmpeg filter failures" , async ( ) => {
10971231 const baseDir = mkdtempSync ( join ( tmpdir ( ) , "hf-audio-base-" ) ) ;
10981232 const workDir = mkdtempSync ( join ( tmpdir ( ) , "hf-audio-work-" ) ) ;
@@ -1466,21 +1600,22 @@ describe("processCompositionAudio", () => {
14661600 expect ( result . success ) . toBe ( true ) ;
14671601 expect ( result . tracksProcessed ) . toBe ( trackCount ) ;
14681602
1469- const mixArgs = runFfmpegMock . mock . calls . at ( - 1 ) ?. [ 0 ] as string [ ] ;
1470- expect ( mixArgs ) . toContain ( "-filter_complex_script" ) ;
1471- expect ( mixArgs ) . not . toContain ( "-filter_complex" ) ;
1472-
1473- // The only things that scale with track count are the -i pairs (short,
1474- // fixed-size each) and the filter SCRIPT FILE's content (off the command
1475- // line entirely) — not the args array's own total character length.
1476- const argsLength = mixArgs . join ( " " ) . length ;
1477- expect ( argsLength ) . toBeLessThan ( 20_000 ) ;
1603+ // 150 tracks exceed one graph: two slice sums, the fold of those two, then the final mix.
1604+ const mixCalls = runFfmpegMock . mock . calls . slice ( - 4 ) . map ( ( call ) => call [ 0 ] as string [ ] ) ;
1605+ for ( const mixArgs of mixCalls ) {
1606+ expect ( mixArgs ) . toContain ( "-filter_complex_script" ) ;
1607+ expect ( mixArgs ) . not . toContain ( "-filter_complex" ) ;
1608+ // Only the -i pairs and the filter script's content scale with track count.
1609+ expect ( mixArgs . join ( " " ) . length ) . toBeLessThan ( 20_000 ) ;
1610+ }
14781611
1479- const filter = capturedFilterScripts . at ( - 1 ) ;
1480- expect ( filter ) . toContain ( `amix=inputs=${ trackCount } ` ) ;
1612+ const [ first , second , folded , last ] = capturedFilterScripts . slice ( - 4 ) ;
1613+ expect ( first ) . toContain ( `amix=inputs=${ MAX_MIX_INPUTS } ` ) ;
1614+ expect ( second ) . toContain ( `amix=inputs=${ trackCount - MAX_MIX_INPUTS } ` ) ;
1615+ expect ( folded ) . toContain ( "amix=inputs=2" ) ;
1616+ expect ( last ) . toContain ( "amix=inputs=1" ) ;
14811617 // Each track is trimmed to its authored clip; only the mixed output is padded.
1482- expect ( ( filter ?. match ( / a t r i m = / g) ?? [ ] ) . length ) . toBe ( trackCount + 1 ) ;
1483- expect ( ( filter ?. match ( / a p a d , / g) ?? [ ] ) . length ) . toBe ( 1 ) ;
1618+ expect ( ( last ?. match ( / a p a d , / g) ?? [ ] ) . length ) . toBe ( 1 ) ;
14841619 // 150 real files + 150 existence checks: ~60ms on Linux, but well past the 5s
14851620 // default on the Windows lane, where each file create is orders slower.
14861621 } , 30_000 ) ;
0 commit comments