@@ -57,7 +57,7 @@ import {
5757 UrlDownloadError ,
5858 writeUrlDownloadTelemetry ,
5959} from "../utils/urlDownloader.js" ;
60- import { runFfmpeg , runFfmpegPipeline , type RunFfmpegResult } from "../utils/runFfmpeg.js" ;
60+ import { runFfmpeg , type RunFfmpegResult } from "../utils/runFfmpeg.js" ;
6161import { isFfmpegFilterAvailable } from "../utils/psnrFilterAvailability.js" ;
6262import { DEFAULT_CONFIG , type EngineConfig } from "../config.js" ;
6363import { unwrapTemplate } from "../utils/htmlTemplate.js" ;
@@ -127,9 +127,9 @@ export function isVideoFrameFormat(value: unknown): value is VideoFrameFormat {
127127/**
128128 * Resolve the frame count produced for a requested extraction duration.
129129 *
130- * CFR extraction uses FFmpeg's fps filter, whose end boundary rounds to the
131- * nearest frame. The VFR path normalizes with `-fps_mode cfr -r`, whose end
132- * boundary rounds up . Keep this calculation shared by superset slicing and
130+ * Both paths sample with FFmpeg's fps filter. A CFR window ends at the nearest
131+ * frame boundary; a VFR window rounds up, keeping the slot a trailing still
132+ * covers . Keep this calculation shared by superset slicing and
133133 * producer coverage accounting so a complete VFR extraction cannot be
134134 * rejected because the two paths disagree by one frame.
135135 */
@@ -297,8 +297,8 @@ function sdrToHdrTransformKey(transfer: HdrTransfer): string {
297297 * reflects max-across-parallel-workers, not sum.
298298 * - hdrPreflightMs includes its probe-time sibling (hdrProbeMs); the
299299 * probe-only field is a finer decomposition, not a separate carve-out.
300- * - vfrPreflightCount reports sources classified as VFR and routed through
301- * the one-pass `-fps_mode cfr -r` extraction path . DEFINITION CHANGE:
300+ * - vfrPreflightCount reports sources classified as VFR and extracted with
301+ * a rounded-up frame count . DEFINITION CHANGE:
302302 * before the one-pass refactor, vfrPreflightMs timed a per-source
303303 * VFR-to-CFR re-encode and could reach seconds; it now times only the
304304 * (promise-cached) classification probe and is expected to be ~0.
@@ -829,61 +829,6 @@ function chromeGuessForUntaggedMatrix(metadata: VideoMetadata): string[] {
829829 return [ `setparams=colorspace=${ hd ? "bt709" : "smpte170m" } ` ] ;
830830}
831831
832- /** Pixel formats nut carries as raw video unchanged on ffmpeg 5.1 to 8.1 (yuvj only loses its range). */
833- const NUT_RAW_PIXEL_FORMATS = new Set ( [
834- "yuv420p" ,
835- "yuvj420p" ,
836- "yuv422p" ,
837- "yuvj422p" ,
838- "yuv444p" ,
839- "yuvj444p" ,
840- "yuva420p" ,
841- "yuva444p" ,
842- "yuv440p" ,
843- "yuv411p" ,
844- "yuv410p" ,
845- "yuva422p" ,
846- "yuv420p10le" ,
847- "yuv422p10le" ,
848- "yuv444p10le" ,
849- "yuva420p10le" ,
850- "yuva444p10le" ,
851- "yuv420p12le" ,
852- "nv12" ,
853- "nv21" ,
854- "yuyv422" ,
855- "uyvy422" ,
856- "gray" ,
857- "gray10le" ,
858- "ya8" ,
859- "gbrp" ,
860- "gbrap" ,
861- "gbrp10le" ,
862- "gbrp12le" ,
863- "gbrap10le" ,
864- "rgb24" ,
865- "bgr24" ,
866- "rgba" ,
867- "bgra" ,
868- "argb" ,
869- "abgr" ,
870- "rgb48le" ,
871- "rgba64le" ,
872- ] ) ;
873-
874- /** nut drops colour tags, so raw frames get the source's back before any other filter reads them. */
875- function restoreSourceColourFilter ( metadata : VideoMetadata ) : string [ ] {
876- const tags = [
877- [ "range" , metadata . colorRange ] ,
878- [ "colorspace" , metadata . colorSpace ?. colorSpace ] ,
879- [ "color_primaries" , metadata . colorSpace ?. colorPrimaries ] ,
880- [ "color_trc" , metadata . colorSpace ?. colorTransfer ] ,
881- ] . filter ( ( [ , value ] ) => value && value !== "unknown" && value !== "reserved" ) ;
882- return tags . length > 0
883- ? [ `setparams=${ tags . map ( ( [ key , value ] ) => `${ key } =${ value } ` ) . join ( ":" ) } ` ]
884- : [ ] ;
885- }
886-
887832/**
888833 * Longest stretch of output one ffmpeg process extracts. `ffmpegProcessTimeout` bounds a
889834 * single process, so one process over a long clip timed out however much time the render
@@ -1036,8 +981,8 @@ export async function extractVideoFramesRange(
1036981 const toneMappedToSdr = options . toneMapHdrToSdr === true && isHdr && ( await toneMapsWithZscale ( ) ) ;
1037982 const decodeWithVideoToolbox = isHdr && isMacOS && ! toneMappedToSdr ;
1038983
1039- const sampleCfrAtOutputFps = ! options . finalFrameOnly && ! metadata . isVFR ;
1040- if ( options . frameRanges && ! sampleCfrAtOutputFps ) {
984+ const sampleAtOutputFps = ! options . finalFrameOnly ;
985+ if ( options . frameRanges && ( ! sampleAtOutputFps || metadata . isVFR ) ) {
1041986 throw new Error ( "frameRanges extraction needs a constant-frame-rate source" ) ;
1042987 }
1043988
@@ -1066,15 +1011,15 @@ export async function extractVideoFramesRange(
10661011 args . push ( "-i" , videoPath , "-ss" , String ( startTime ) , "-frames:v" , "1" ) ;
10671012 } else {
10681013 // An accurate seek drops the frame on screen at startTime; the fps sampling below needs it.
1069- if ( sampleCfrAtOutputFps ) args . push ( "-noaccurate_seek" ) ;
1014+ if ( sampleAtOutputFps ) args . push ( "-noaccurate_seek" ) ;
10701015 args . push ( "-ss" , String ( startTime ) , "-i" , videoPath , "-t" , String ( duration ) ) ;
10711016 }
10721017
10731018 const vfFilters : string [ ] = [ ] ;
10741019 if ( decodeWithVideoToolbox ) {
10751020 vfFilters . push ( "format=nv12" ) ;
10761021 }
1077- if ( sampleCfrAtOutputFps ) {
1022+ if ( sampleAtOutputFps ) {
10781023 // Each slot takes the last frame starting at or before its time. Ticks of 2 ms or finer
10791024 // first shift half a tick, absorbing container rounding (WebM/MKV use whole ms).
10801025 vfFilters . push (
@@ -1110,59 +1055,31 @@ export async function extractVideoFramesRange(
11101055 encodeArgs . push ( "-y" , outputPattern ) ;
11111056
11121057 const runOptions = { signal, timeout : ffmpegProcessTimeout } ;
1113- const resampleVfrToCfr = ! options . finalFrameOnly && metadata . isVFR ;
1114- if ( resampleVfrToCfr ) args . push ( "-fps_mode" , "cfr" , "-r" , ffmpegFps ) ;
1115- let processResult : RunFfmpegResult ;
1116- if (
1117- resampleVfrToCfr &&
1118- vfFilters . length > 0 &&
1119- ! isHdr &&
1120- NUT_RAW_PIXEL_FORMATS . has ( metadata . pixelFormat ?? "" )
1121- ) {
1122- // ffmpeg <6.1 ignores frame durations in CFR resampling once any -vf is set,
1123- // cutting a trailing still short, so the SDR filters run in a second process.
1124- processResult = await inExtractionSlot (
1125- ( ) =>
1126- runFfmpegPipeline (
1127- [ ...args , "-an" , "-sn" , "-dn" , "-c:v" , "rawvideo" , "-f" , "nut" , "pipe:1" ] ,
1128- [
1129- "-f" ,
1130- "nut" ,
1131- "-i" ,
1132- "pipe:0" ,
1133- "-vf" ,
1134- [ ...restoreSourceColourFilter ( metadata ) , ...vfFilters ] . join ( "," ) ,
1135- "-fps_mode" ,
1136- "passthrough" ,
1137- ...encodeArgs ,
1138- ] ,
1058+ const filterArgs = vfFilters . length > 0 ? [ "-vf" , vfFilters . join ( "," ) ] : [ ] ;
1059+ const segmentFrames = Math . round ( EXTRACTION_SEGMENT_SECONDS * fps ) ;
1060+ const totalFrames = extractionFrameCountForDuration ( duration , normalizedFps , metadata . isVFR ) ;
1061+ // A VFR window rounds its frame count up, and only the segments' `-frames:v` bound
1062+ // emits that last slot: the fps filter ends a `-t` window at the nearest slot. A
1063+ // decimal rate counts zero frames here and keeps the `-t` window.
1064+ const boundVfrFrameCount = metadata . isVFR && totalFrames > 0 ;
1065+ const processResult =
1066+ sampleAtOutputFps && ( options . frameRanges || boundVfrFrameCount || totalFrames > segmentFrames )
1067+ ? await runSegmentedExtraction ( {
1068+ decodeArgs,
1069+ filterAndEncodeArgs : [ ...filterArgs , ...encodeArgs ] ,
1070+ videoPath,
1071+ startTime,
1072+ fps,
1073+ segments : splitFrameRanges (
1074+ options . frameRanges ?? [ { firstFrame : 0 , frames : totalFrames } ] ,
1075+ segmentFrames ,
1076+ ) ,
11391077 runOptions,
1140- ) ,
1141- signal ,
1142- ) ;
1143- } else {
1144- const filterArgs = vfFilters . length > 0 ? [ "-vf" , vfFilters . join ( "," ) ] : [ ] ;
1145- const segmentFrames = Math . round ( EXTRACTION_SEGMENT_SECONDS * fps ) ;
1146- const totalFrames = extractionFrameCountForDuration ( duration , normalizedFps , false ) ;
1147- processResult =
1148- sampleCfrAtOutputFps && ( options . frameRanges || totalFrames > segmentFrames )
1149- ? await runSegmentedExtraction ( {
1150- decodeArgs,
1151- filterAndEncodeArgs : [ ...filterArgs , ...encodeArgs ] ,
1152- videoPath,
1153- startTime,
1154- fps,
1155- segments : splitFrameRanges (
1156- options . frameRanges ?? [ { firstFrame : 0 , frames : totalFrames } ] ,
1157- segmentFrames ,
1158- ) ,
1159- runOptions,
1160- } )
1161- : await inExtractionSlot (
1162- ( ) => runFfmpeg ( [ ...args , ...filterArgs , ...encodeArgs ] , runOptions ) ,
1163- signal ,
1164- ) ;
1165- }
1078+ } )
1079+ : await inExtractionSlot (
1080+ ( ) => runFfmpeg ( [ ...args , ...filterArgs , ...encodeArgs ] , runOptions ) ,
1081+ signal ,
1082+ ) ;
11661083 if ( processResult . failureReason === "external_interruption" ) {
11671084 throw new VideoSourceExtractionError (
11681085 "external_interruption" ,
@@ -1714,11 +1631,8 @@ function buildSupersetGroup(
17141631) : SupersetGroupPlan | null {
17151632 if ( misses . length < 2 ) return null ;
17161633 if ( misses . some ( ( { work } ) => work . finalFrameOnly ) ) return null ;
1717- // VFR normalization (`-fps_mode cfr -r`) establishes its duplicate/drop
1718- // phase relative to each seek. A union extraction therefore cannot be
1719- // sliced into the same frames as independently sought member ranges, even
1720- // when their offsets land on an integral output-frame boundary. Keep VFR
1721- // ranges direct until the extractor has a proven absolute timestamp phase.
1634+ // VFR ranges round their frame counts up, so a union extraction's slices
1635+ // are not proven to match independently sought member ranges. Keep them direct.
17221636 if ( misses . some ( ( { work } ) => work . metadata . isVFR ) ) return null ;
17231637 const baseStart = Math . min ( ...misses . map ( ( { work } ) => work . video . mediaStart ) ) ;
17241638 if ( ! misses . every ( ( { work } ) => isIntegralFrameOffset ( work . video . mediaStart - baseStart , fps ) ) ) {
0 commit comments