@@ -64,9 +64,7 @@ import { createStudioPositionSeekReapplyScript } from "@hyperframes/studio-serve
6464import { getPositionEditsRenderScript } from "@hyperframes/core/runtime/position-edits-render" ;
6565import { defaultLogger , type ProducerLogger } from "../logger.js" ;
6666import { assertAssetMediaTypeProfile } from "./assetMediaType.js" ;
67- import { Semaphore } from "../utils/semaphore.js" ;
68-
69- const MAX_COMPILER_MEDIA_PROBES = 4 ;
67+ import { withMediaProbeSlot } from "../utils/mediaProbeConcurrency.js" ;
7068
7169export interface CompiledComposition {
7270 html : string ;
@@ -415,7 +413,6 @@ async function resolveMediaDuration(
415413 downloadDir : string ,
416414 tagName : string ,
417415 elementIdentity : string ,
418- mediaProbeSemaphore : Semaphore ,
419416) : Promise < { duration : number ; resolvedPath : string } > {
420417 let filePath = src ;
421418
@@ -436,8 +433,7 @@ async function resolveMediaDuration(
436433 return { duration : 0 , resolvedPath : filePath } ;
437434 }
438435
439- const releaseProbe = await mediaProbeSemaphore . acquire ( ) ;
440- try {
436+ return withMediaProbeSlot ( async ( ) => {
441437 let profile : MediaProbeProfile ;
442438 try {
443439 profile = await probeMediaProfile ( filePath ) ;
@@ -469,9 +465,7 @@ async function resolveMediaDuration(
469465 const duration = effectiveDuration > 0 ? effectiveDuration : fileDuration ;
470466
471467 return { duration, resolvedPath : filePath } ;
472- } finally {
473- releaseProbe ( ) ;
474- }
468+ } ) ;
475469}
476470
477471/**
@@ -482,7 +476,6 @@ async function compileHtmlFile(
482476 html : string ,
483477 baseDir : string ,
484478 downloadDir : string ,
485- mediaProbeSemaphore : Semaphore ,
486479 log ?: ProducerLogger ,
487480) : Promise < { html : string ; unresolvedCompositions : UnresolvedElement [ ] } > {
488481 const { html : staticCompiled , unresolved } = compileTimingAttrs ( html ) ;
@@ -496,15 +489,9 @@ async function compileHtmlFile(
496489 // Phase 1: Resolve missing durations (parallel ffprobe)
497490 const resolvedResults = await Promise . all (
498491 mediaUnresolved . map ( ( el ) =>
499- resolveMediaDuration (
500- el . src ! ,
501- el . mediaStart ,
502- baseDir ,
503- downloadDir ,
504- el . tagName ,
505- el . id ,
506- mediaProbeSemaphore ,
507- ) . then ( ( { duration } ) => ( { id : el . id , duration } ) ) ,
492+ resolveMediaDuration ( el . src ! , el . mediaStart , baseDir , downloadDir , el . tagName , el . id ) . then (
493+ ( { duration } ) => ( { id : el . id , duration } ) ,
494+ ) ,
508495 ) ,
509496 ) ;
510497 const resolutions : ResolvedDuration [ ] = resolvedResults . filter ( ( r ) => r . duration > 0 ) ;
@@ -526,7 +513,6 @@ async function compileHtmlFile(
526513 downloadDir ,
527514 el . tagName ,
528515 el . id ,
529- mediaProbeSemaphore ,
530516 ) ;
531517 return { id : el . id , tagName : el . tagName , duration : el . duration , maxDuration, src : el . src ! } ;
532518 } ) ,
@@ -588,7 +574,6 @@ async function parseSubCompositions(
588574 html : string ,
589575 projectDir : string ,
590576 downloadDir : string ,
591- mediaProbeSemaphore : Semaphore ,
592577 parentOffset : number = 0 ,
593578 parentEnd : number = Infinity ,
594579 visited : Set < string > = new Set ( ) ,
@@ -652,14 +637,12 @@ async function parseSubCompositions(
652637 item . rawSubHtml ,
653638 dirname ( item . filePath ) ,
654639 downloadDir ,
655- mediaProbeSemaphore ,
656640 ) ;
657641
658642 const nested = await parseSubCompositions (
659643 compiledSub ,
660644 projectDir ,
661645 downloadDir ,
662- mediaProbeSemaphore ,
663646 item . absoluteStart ,
664647 item . absoluteEnd ,
665648 item . nestedVisited ,
@@ -1856,15 +1839,10 @@ export async function compileForRender(
18561839 // start.
18571840 assertSubCompositionsUsable ( rawHtml , projectDir ) ;
18581841
1859- // One limiter is shared by both compiler passes and every recursive
1860- // sub-composition so nested Promise.all calls cannot multiply ffprobe load.
1861- const mediaProbeSemaphore = new Semaphore ( MAX_COMPILER_MEDIA_PROBES ) ;
1862-
18631842 const { html : compiledHtml , unresolvedCompositions } = await compileHtmlFile (
18641843 rawHtml ,
18651844 projectDir ,
18661845 downloadDir ,
1867- mediaProbeSemaphore ,
18681846 options . log ,
18691847 ) ;
18701848
@@ -1874,7 +1852,7 @@ export async function compileForRender(
18741852 audios : subAudios ,
18751853 images : subImages ,
18761854 subCompositions,
1877- } = await parseSubCompositions ( compiledHtml , projectDir , downloadDir , mediaProbeSemaphore ) ;
1855+ } = await parseSubCompositions ( compiledHtml , projectDir , downloadDir ) ;
18781856
18791857 // Ensure the HTML is a full document before inlining sub-compositions.
18801858 // When index.html is a fragment (no <html>/<head>/<body>), linkedom.parseHTML()
@@ -2034,7 +2012,10 @@ export async function compileForRender(
20342012 if ( isHttpUrl ( video . src ) ) continue ;
20352013 const videoPath = resolve ( projectDir , video . src ) ;
20362014 const reencode = `ffmpeg -i "${ video . src } " -c:v libx264 -r 30 -g 30 -keyint_min 30 -movflags +faststart -c:a copy output.mp4` ;
2037- Promise . all ( [ analyzeKeyframeIntervals ( videoPath ) , extractMediaMetadata ( videoPath ) ] )
2015+ Promise . all ( [
2016+ withMediaProbeSlot ( ( ) => analyzeKeyframeIntervals ( videoPath ) ) ,
2017+ withMediaProbeSlot ( ( ) => extractMediaMetadata ( videoPath ) ) ,
2018+ ] )
20382019 . then ( ( [ analysis , metadata ] ) => {
20392020 if ( analysis . isProblematic ) {
20402021 defaultLogger . warn (
@@ -2138,9 +2119,15 @@ export async function discoverMediaFromBrowser(page: Page): Promise<BrowserMedia
21382119 if ( ! image . id ) autoImageIds . set ( image , `hf-img-${ autoImageId ++ } ` ) ;
21392120 } ) ;
21402121
2141- const mediaEls = document . querySelectorAll (
2142- "video[data-start], audio[data-start], img[data-var-src]" ,
2122+ const mediaEls = new Set < Element > (
2123+ document . querySelectorAll ( "video[data-start], audio[data-start], img[data-var-src]" ) ,
21432124 ) ;
2125+ // A variable-bound <picture><source> changes the owning image's currentSrc;
2126+ // the <img> fallback itself does not necessarily carry data-var-src.
2127+ document . querySelectorAll ( "picture source[data-var-src]" ) . forEach ( ( source ) => {
2128+ const image = source . closest ( "picture" ) ?. querySelector ( "img" ) ;
2129+ if ( image ) mediaEls . add ( image ) ;
2130+ } ) ;
21442131 mediaEls . forEach ( ( el ) => {
21452132 const htmlEl = el as HTMLVideoElement | HTMLAudioElement | HTMLImageElement ;
21462133 const isImage = htmlEl . tagName . toLowerCase ( ) === "img" ;
@@ -2459,15 +2446,14 @@ export async function recompileWithResolutions(
24592446 if ( resolutions . length === 0 ) return compiled ;
24602447
24612448 const html = injectDurations ( compiled . html , resolutions ) ;
2462- const mediaProbeSemaphore = new Semaphore ( MAX_COMPILER_MEDIA_PROBES ) ;
24632449
24642450 // Re-parse sub-compositions with the updated parent bounds
24652451 const {
24662452 videos : subVideos ,
24672453 audios : subAudios ,
24682454 images : subImages ,
24692455 subCompositions,
2470- } = await parseSubCompositions ( html , projectDir , downloadDir , mediaProbeSemaphore ) ;
2456+ } = await parseSubCompositions ( html , projectDir , downloadDir ) ;
24712457
24722458 const mainVideos = parseVideoElements ( html ) ;
24732459 const mainAudios = parseAudioElements ( html ) ;
0 commit comments