@@ -481,6 +481,11 @@ export function detectShaderTransitionUsage(html: string): boolean {
481481 return false ;
482482}
483483
484+ type SourceProbe = { profile : MediaProbeProfile ; fileDuration : number | null } | { error : unknown } ;
485+
486+ /** One probe per unique source for a whole compile, whatever number of elements point at it. */
487+ type SourceProbes = Map < string , Promise < SourceProbe > > ;
488+
484489async function resolveMediaDuration (
485490 src : string ,
486491 mediaStart : number ,
@@ -491,6 +496,7 @@ async function resolveMediaDuration(
491496 elementIdentity : string ,
492497 log ?: ProducerLogger ,
493498 urlByAbsentLocalPath ?: ReadonlyMap < string , string > ,
499+ probes ?: SourceProbes ,
494500) : Promise < { duration : number | null ; resolvedPath : string } > {
495501 let filePath = src ;
496502
@@ -541,64 +547,62 @@ async function resolveMediaDuration(
541547 return wrapped ;
542548 } ;
543549
544- return withMediaProbeSlot ( async ( ) => {
545- let profile : MediaProbeProfile ;
546- try {
547- // Payload sniff (STUDIO-5433): if an authoring bug hands us a text
548- // payload (e.g. an unresolved nested-composition preview URL), fail with
549- // a typed NotMediaPayloadError instead of letting ffprobe emit an opaque
550- // `[mov,mp4,...] moov atom not found` that routes as a codec bug.
551- // Deliberately inside this try: the audio/video split below is the
552- // contract, so a bad audio src must still degrade to duration 0 rather
553- // than take down the whole render.
554- await assertMediaPayload ( filePath , elementIdentity ) ;
555- profile = await probeMediaProfile ( filePath ) ;
556- } catch ( error ) {
557- // Preserve the historical split: invalid video sources surface their
558- // probe failure, while invalid/unreadable audio sources resolve to zero
559- // duration and are excluded by the compiler.
560- if ( tagName !== "video" ) {
561- if ( error instanceof NotMediaPayloadError ) {
562- // Dropping it silently is what let STUDIO-5433 resurface downstream
563- // as `prepare/ffmpeg_failed` with owner "system".
564- log ?. warn (
565- `[compile] Audio "${ elementIdentity } " (${ src } ) is a text document, not a media ` +
566- "file, so the element is dropped from the render. Point it at a rendered media file." ,
567- ) ;
568- }
569- return { duration : null , resolvedPath : filePath } ;
570- }
571- throw withSrcContext ( error ) ;
572- }
573- assertAssetMediaTypeProfile ( tagName === "video" ? "video" : "audio" , profile , elementIdentity ) ;
574-
575- let metadata : { durationSeconds : number } ;
576- if ( tagName === "video" ) {
550+ const probe = ( ) =>
551+ withMediaProbeSlot ( async ( ) : Promise < SourceProbe > => {
577552 try {
578- metadata = await extractMediaMetadata ( filePath ) ;
553+ // Payload sniff (STUDIO-5433): if an authoring bug hands us a text
554+ // payload (e.g. an unresolved nested-composition preview URL), fail with
555+ // a typed NotMediaPayloadError instead of letting ffprobe emit an opaque
556+ // `[mov,mp4,...] moov atom not found` that routes as a codec bug.
557+ await assertMediaPayload ( filePath , elementIdentity ) ;
558+ const profile = await probeMediaProfile ( filePath ) ;
559+ if ( tagName === "video" ) {
560+ return { profile, fileDuration : ( await extractMediaMetadata ( filePath ) ) . durationSeconds } ;
561+ }
562+ try {
563+ return { profile, fileDuration : ( await extractAudioMetadata ( filePath ) ) . durationSeconds } ;
564+ } catch {
565+ // Source file has no audio stream (e.g. a silent video used as an audio src).
566+ // Duration 0 excludes the element from the composition, as missing files do.
567+ return { profile, fileDuration : null } ;
568+ }
579569 } catch ( error ) {
580- throw withSrcContext ( error ) ;
570+ return { error } ;
581571 }
582- } else {
583- try {
584- metadata = await extractAudioMetadata ( filePath ) ;
585- } catch {
586- // Source file has no audio stream (e.g. a silent video used as an audio src).
587- // Return duration 0 so the element is excluded from the composition gracefully,
588- // matching how missing files and failed downloads are already handled above.
589- return { duration : null , resolvedPath : filePath } ;
572+ } ) ;
573+ const key = `${ tagName } \0${ filePath } ` ;
574+ let probed = probes ?. get ( key ) ;
575+ if ( ! probed ) {
576+ probed = probe ( ) ;
577+ probes ?. set ( key , probed ) ;
578+ }
579+ const outcome = await probed ;
580+ if ( "error" in outcome ) {
581+ // Invalid video sources surface their probe failure; invalid or unreadable
582+ // audio sources resolve to zero duration and are excluded by the compiler.
583+ if ( tagName !== "video" ) {
584+ if ( outcome . error instanceof NotMediaPayloadError ) {
585+ log ?. warn (
586+ `[compile] Audio "${ elementIdentity } " (${ src } ) is a text document, not a media ` +
587+ "file, so the element is dropped from the render. Point it at a rendered media file." ,
588+ ) ;
590589 }
590+ return { duration : null , resolvedPath : filePath } ;
591591 }
592-
593- const fileDuration = metadata . durationSeconds ;
594- const duration = resolveNaturalMediaTimelineDurationFromValues (
595- fileDuration ,
596- mediaStart ,
597- playbackRate ,
598- ) ;
599-
600- return { duration, resolvedPath : filePath } ;
601- } ) ;
592+ throw withSrcContext ( outcome . error ) ;
593+ }
594+ assertAssetMediaTypeProfile (
595+ tagName === "video" ? "video" : "audio" ,
596+ outcome . profile ,
597+ elementIdentity ,
598+ ) ;
599+ if ( outcome . fileDuration === null ) return { duration : null , resolvedPath : filePath } ;
600+ const duration = resolveNaturalMediaTimelineDurationFromValues (
601+ outcome . fileDuration ,
602+ mediaStart ,
603+ playbackRate ,
604+ ) ;
605+ return { duration, resolvedPath : filePath } ;
602606}
603607
604608/**
@@ -628,6 +632,7 @@ async function compileHtmlFile(
628632 downloadDir : string ,
629633 log ?: ProducerLogger ,
630634 urlByAbsentLocalPath ?: ReadonlyMap < string , string > ,
635+ probes ?: SourceProbes ,
631636) : Promise < { html : string ; unresolvedCompositions : UnresolvedElement [ ] } > {
632637 const { html : staticCompiled , unresolved } = compileTimingAttrs ( html ) ;
633638
@@ -650,6 +655,7 @@ async function compileHtmlFile(
650655 el . id ,
651656 log ,
652657 urlByAbsentLocalPath ,
658+ probes ,
653659 ) . then ( ( { duration } ) => ( { id : el . id , duration } ) ) ,
654660 ) ,
655661 ) ;
@@ -678,6 +684,8 @@ async function compileHtmlFile(
678684 el . tagName ,
679685 el . id ,
680686 log ,
687+ undefined ,
688+ probes ,
681689 ) ;
682690 return { id : el . id , tagName : el . tagName , duration : el . duration , maxDuration, src : el . src ! } ;
683691 } ) ,
@@ -746,6 +754,7 @@ async function parseSubCompositions(
746754 downloadDir : string ,
747755 visited : Set < string > = new Set ( ) ,
748756 urlByAbsentLocalPath ?: ReadonlyMap < string , string > ,
757+ probes ?: SourceProbes ,
749758) : Promise < { subCompositions : Map < string , string > } > {
750759 const subCompositions = new Map < string , string > ( ) ;
751760
@@ -792,6 +801,7 @@ async function parseSubCompositions(
792801 downloadDir ,
793802 undefined ,
794803 urlByAbsentLocalPath ,
804+ probes ,
795805 ) ;
796806
797807 const nested = await parseSubCompositions (
@@ -800,6 +810,7 @@ async function parseSubCompositions(
800810 downloadDir ,
801811 item . nestedVisited ,
802812 urlByAbsentLocalPath ,
813+ probes ,
803814 ) ;
804815
805816 return {
@@ -2040,12 +2051,14 @@ export async function compileForRender(
20402051 // start.
20412052 assertSubCompositionsUsable ( rawHtml , projectDir ) ;
20422053
2054+ const probes : SourceProbes = new Map ( ) ;
20432055 const { html : compiledHtml , unresolvedCompositions } = await compileHtmlFile (
20442056 rawHtml ,
20452057 projectDir ,
20462058 downloadDir ,
20472059 options . log ,
20482060 options . urlByAbsentLocalPath ,
2061+ probes ,
20492062 ) ;
20502063
20512064 // Compile each referenced sub-composition so the inliner can hoist it.
@@ -2055,6 +2068,7 @@ export async function compileForRender(
20552068 downloadDir ,
20562069 undefined ,
20572070 options . urlByAbsentLocalPath ,
2071+ probes ,
20582072 ) ;
20592073
20602074 // Ensure the HTML is a full document before inlining sub-compositions.
@@ -2208,9 +2222,12 @@ export async function compileForRender(
22082222 // ffprobe subprocesses and should not block compilation since they only produce warnings.
22092223 // The two probes run in sequence rather than in parallel: the keyframe analysis needs
22102224 // the video stream's own duration to classify a single-keyframe (single-GOP) file.
2225+ const advised = new Set < string > ( ) ;
22112226 for ( const video of videos ) {
22122227 if ( isHttpUrl ( video . src ) ) continue ;
22132228 const videoPath = resolve ( projectDir , video . src ) ;
2229+ if ( advised . has ( videoPath ) ) continue ;
2230+ advised . add ( videoPath ) ;
22142231 const reencode = `ffmpeg -i "${ video . src } " -c:v libx264 -r 30 -g 30 -keyint_min 30 -movflags +faststart -c:a copy output.mp4` ;
22152232 ( async ( ) => {
22162233 const metadata = await withMediaProbeSlot ( ( ) => extractMediaMetadata ( videoPath ) ) ;
0 commit comments