@@ -1786,6 +1786,98 @@ describe.skipIf(!HAS_FFMPEG)("video frame extraction format", () => {
17861786 } , 60_000 ) ;
17871787} ) ;
17881788
1789+ // Each output slot shows the frame on screen at its time, as the preview does.
1790+ describe . skipIf ( ! HAS_FFMPEG ) ( "frame sampling at the output frame rate" , ( ) => {
1791+ const FIXTURE_DIR = mkdtempSync ( join ( tmpdir ( ) , "hf-video-frame-sampling-" ) ) ;
1792+ const WIDTH = 32 ;
1793+ const HEIGHT = 16 ;
1794+ // Matroska stores whole-millisecond timestamps (a 30 fps frame at 66.67 ms reads 67 ms);
1795+ // a 1/30 timescale stores each frame exactly on a coarse tick.
1796+ const SOURCES = {
1797+ "mp4-60" : { fps : 60 , file : "index-60fps.mp4" , muxer : [ ] } ,
1798+ "mkv-30" : { fps : 30 , file : "index-30fps.mkv" , muxer : [ ] } ,
1799+ "mp4-30-timescale-30" : {
1800+ fps : 30 ,
1801+ file : "index-30fps-ts30.mp4" ,
1802+ muxer : [ "-video_track_timescale" , "30" ] ,
1803+ } ,
1804+ } as const ;
1805+ type SourceName = keyof typeof SOURCES ;
1806+ const sourcePath = ( name : SourceName ) => join ( FIXTURE_DIR , SOURCES [ name ] . file ) ;
1807+
1808+ beforeAll ( async ( ) => {
1809+ for ( const name of Object . keys ( SOURCES ) as SourceName [ ] ) {
1810+ // Frame k carries luma 16 + 2k, so each extracted frame names its source index.
1811+ const result = await runFfmpeg ( [
1812+ "-y" ,
1813+ "-hide_banner" ,
1814+ "-loglevel" ,
1815+ "error" ,
1816+ "-f" ,
1817+ "lavfi" ,
1818+ "-i" ,
1819+ `nullsrc=s=${ WIDTH } x${ HEIGHT } :r=${ SOURCES [ name ] . fps } :d=1.5,geq=lum='16+2*N':cb=128:cr=128` ,
1820+ "-c:v" ,
1821+ "libx264" ,
1822+ "-qp" ,
1823+ "0" ,
1824+ "-pix_fmt" ,
1825+ "yuv420p" ,
1826+ ...SOURCES [ name ] . muxer ,
1827+ sourcePath ( name ) ,
1828+ ] ) ;
1829+ if ( ! result . success ) throw new Error ( `index fixture synthesis failed: ${ result . stderr } ` ) ;
1830+ }
1831+ } , 30_000 ) ;
1832+
1833+ afterAll ( ( ) => {
1834+ rmSync ( FIXTURE_DIR , { recursive : true , force : true } ) ;
1835+ } ) ;
1836+
1837+ function sourceIndexes ( extracted : ExtractedFrames ) : number [ ] {
1838+ const decoded = spawnSync ( "ffmpeg" , [
1839+ "-v" ,
1840+ "error" ,
1841+ "-i" ,
1842+ join ( extracted . outputDir , extracted . framePattern ) ,
1843+ "-f" ,
1844+ "rawvideo" ,
1845+ "-pix_fmt" ,
1846+ "gray" ,
1847+ "pipe:1" ,
1848+ ] ) ;
1849+ if ( decoded . status !== 0 ) throw new Error ( decoded . stderr . toString ( ) ) ;
1850+ const indexes : number [ ] = [ ] ;
1851+ for ( let offset = 0 ; offset < decoded . stdout . length ; offset += WIDTH * HEIGHT ) {
1852+ indexes . push ( Math . round ( ( ( decoded . stdout [ offset ] ?? 0 ) * 219 ) / 255 / 2 ) ) ;
1853+ }
1854+ return indexes ;
1855+ }
1856+
1857+ it . each ( [
1858+ { source : "mp4-60" , fps : 24 , startTime : 0 , duration : 0.5 } ,
1859+ { source : "mp4-60" , fps : 10 , startTime : 0.37 , duration : 0.6 } ,
1860+ { source : "mkv-30" , fps : 30 , startTime : 0 , duration : 0.5 } ,
1861+ { source : "mp4-30-timescale-30" , fps : 60 , startTime : 0 , duration : 0.5 } ,
1862+ ] as const ) (
1863+ "$source: samples the frame on screen at each $fps fps slot from $startTime s" ,
1864+ async ( c ) => {
1865+ const extracted = await extractVideoFramesRange (
1866+ sourcePath ( c . source ) ,
1867+ `${ c . source } -${ c . fps } ` ,
1868+ c . startTime ,
1869+ c . duration ,
1870+ { fps : c . fps , outputDir : FIXTURE_DIR , format : "png" } ,
1871+ ) ;
1872+ const onScreen = Array . from ( { length : Math . round ( c . duration * c . fps ) } , ( _ , i ) =>
1873+ Math . floor ( ( c . startTime + i / c . fps ) * SOURCES [ c . source ] . fps + 1e-9 ) ,
1874+ ) ;
1875+ expect ( sourceIndexes ( extracted ) ) . toEqual ( onScreen ) ;
1876+ } ,
1877+ 30_000 ,
1878+ ) ;
1879+ } ) ;
1880+
17891881describe . skipIf ( ! HAS_FFMPEG ) ( "held tails on sparse-timestamp sources" , ( ) => {
17901882 const fixtureDir = mkdtempSync ( join ( tmpdir ( ) , "hf-sparse-held-tail-" ) ) ;
17911883 const cfrFixture = join ( fixtureDir , "sub-1fps-cfr.mp4" ) ;
0 commit comments