@@ -14,8 +14,11 @@ import {
1414 resolveLockedGopSize ,
1515} from "./chunkEncoder.js" ;
1616import { renderProvenanceArgs } from "../utils/renderProvenance.js" ;
17- import { SDR_CAPTURE_TO_BT709_FILTER } from "../utils/sdrCaptureColor.js" ;
18- import { getFfmpegBinary } from "../utils/ffmpegBinaries.js" ;
17+ import {
18+ SDR_CAPTURE_TO_BT709_FILTER ,
19+ SDR_RGB_TO_TAGGED_BT709_FILTER ,
20+ } from "../utils/sdrCaptureColor.js" ;
21+ import { getFfmpegBinary , getFfprobeBinary } from "../utils/ffmpegBinaries.js" ;
1922
2023const HAS_FFMPEG = spawnSync ( getFfmpegBinary ( ) , [ "-version" ] ) . status === 0 ;
2124
@@ -1226,7 +1229,7 @@ describe("buildEncoderArgs color space", () => {
12261229 expect ( args [ args . indexOf ( "-vf" ) + 1 ] ) . toBe ( "pad=ceil(iw/2)*2:ceil(ih/2)*2" ) ;
12271230 } ) ;
12281231
1229- it ( "leaves alpha ProRes untouched (no even-dim pad) " , ( ) => {
1232+ it ( "converts alpha ProRes to BT.709 without an even-dim pad" , ( ) => {
12301233 const args = buildEncoderArgs (
12311234 {
12321235 ...baseOptions ,
@@ -1238,7 +1241,7 @@ describe("buildEncoderArgs color space", () => {
12381241 inputArgs ,
12391242 "out.mov" ,
12401243 ) ;
1241- expect ( args . indexOf ( "-vf" ) ) . toBe ( - 1 ) ;
1244+ expect ( args [ args . indexOf ( "-vf" ) + 1 ] ) . toBe ( SDR_RGB_TO_TAGGED_BT709_FILTER ) ;
12421245 expect ( args . join ( " " ) ) . not . toContain ( "pad=" ) ;
12431246 } ) ;
12441247
@@ -1820,26 +1823,27 @@ describe("buildConcatArgs", () => {
18201823} ) ;
18211824
18221825describe . skipIf ( ! HAS_FFMPEG ) ( "buildEncoderArgs SDR colour" , ( ) => {
1826+ const ffmpeg = getFfmpegBinary ( ) ;
1827+ const pixelAt = ( file : string , decode : string , x : number , format = "rgb24" ) : number [ ] => [
1828+ ...spawnSync ( ffmpeg , [
1829+ "-v" ,
1830+ "error" ,
1831+ "-i" ,
1832+ file ,
1833+ "-vf" ,
1834+ `${ decode } format=${ format } ,crop=1:1:${ x } :8` ,
1835+ "-frames:v" ,
1836+ "1" ,
1837+ "-f" ,
1838+ "rawvideo" ,
1839+ "-" ,
1840+ ] ) . stdout ,
1841+ ] ;
1842+
18231843 // Chrome captures are BT.601 JPEGs. A direct YUV-to-YUV scale keeps that matrix under the BT.709
18241844 // tag (ffmpeg 7 and older) or tints greys (8 and newer), so the encode goes through RGB.
18251845 it ( "delivers Chrome's JPEG colours in the BT.709 the mp4 is tagged with" , ( ) => {
1826- const ffmpeg = getFfmpegBinary ( ) ;
18271846 const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-sdr-colour-" ) ) ;
1828- const rgbAt = ( file : string , decode : string , x : number ) : number [ ] => [
1829- ...spawnSync ( ffmpeg , [
1830- "-v" ,
1831- "error" ,
1832- "-i" ,
1833- file ,
1834- "-vf" ,
1835- `${ decode } format=rgb24,crop=1:1:${ x } :8` ,
1836- "-frames:v" ,
1837- "1" ,
1838- "-f" ,
1839- "rawvideo" ,
1840- "-" ,
1841- ] ) . stdout ,
1842- ] ;
18431847 try {
18441848 for ( const color of [ "0xC83C28" , "0xFE0000" , "0x101010" , "0x2050E0" ] ) {
18451849 const jpg = join ( dir , "frame.jpg" ) ;
@@ -1879,8 +1883,8 @@ describe.skipIf(!HAS_FFMPEG)("buildEncoderArgs SDR colour", () => {
18791883 [ 8 , 2 ] ,
18801884 [ 32 , 2 ] ,
18811885 ] as const ) {
1882- const captured = rgbAt ( jpg , "" , x ) ;
1883- const delivered = rgbAt ( out , "scale=in_color_matrix=bt709:in_range=tv," , x ) ;
1886+ const captured = pixelAt ( jpg , "" , x ) ;
1887+ const delivered = pixelAt ( out , "scale=in_color_matrix=bt709:in_range=tv," , x ) ;
18841888 const worst = Math . max ( ...delivered . map ( ( v , i ) => Math . abs ( v - captured [ i ] ! ) ) ) ;
18851889 expect (
18861890 worst ,
@@ -1892,4 +1896,70 @@ describe.skipIf(!HAS_FFMPEG)("buildEncoderArgs SDR colour", () => {
18921896 rmSync ( dir , { recursive : true , force : true } ) ;
18931897 }
18941898 } , 30_000 ) ;
1899+
1900+ // Untagged HD reads as BT.709 in QuickTime, so ProRes has to be BT.709 too. MOV captures are
1901+ // RGBA PNGs, and the conversion must leave their alpha plane alone. The tags come from
1902+ // setparams because prores_ks ignores -color_* (ffmpeg 5.1 writes none, 8.1 only the matrix).
1903+ it ( "delivers PNG colours and alpha in the BT.709 the ProRes mov is tagged with" , ( ) => {
1904+ const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-prores-colour-" ) ) ;
1905+ const png = join ( dir , "frame.png" ) ;
1906+ const out = join ( dir , "out.mov" ) ;
1907+ try {
1908+ for ( const color of [ "0xC83C28" , "0x00FF00" , "0x101010" ] ) {
1909+ const synth = spawnSync ( ffmpeg , [
1910+ "-v" ,
1911+ "error" ,
1912+ "-y" ,
1913+ "-f" ,
1914+ "lavfi" ,
1915+ "-i" ,
1916+ `color=c=${ color } @0.5:s=64x16,format=rgba` ,
1917+ "-frames:v" ,
1918+ "1" ,
1919+ png ,
1920+ ] ) ;
1921+ expect ( synth . status ) . toBe ( 0 ) ;
1922+ const args = buildEncoderArgs (
1923+ {
1924+ fps : { num : 30 , den : 1 } ,
1925+ width : 64 ,
1926+ height : 16 ,
1927+ codec : "prores" ,
1928+ preset : "4444" ,
1929+ quality : 23 ,
1930+ pixelFormat : "yuva444p10le" ,
1931+ } ,
1932+ [ "-i" , png ] ,
1933+ out ,
1934+ ) ;
1935+ expect ( spawnSync ( ffmpeg , args ) . status ) . toBe ( 0 ) ;
1936+
1937+ const captured = pixelAt ( png , "" , 8 , "rgba" ) ;
1938+ const delivered = pixelAt ( out , "scale=in_color_matrix=bt709:in_range=tv," , 8 , "rgba" ) ;
1939+ const worst = Math . max ( ...delivered . map ( ( v , i ) => Math . abs ( v - captured [ i ] ! ) ) ) ;
1940+ expect ( worst , `${ color } : capture ${ captured } delivered ${ delivered } ` ) . toBeLessThanOrEqual (
1941+ 2 ,
1942+ ) ;
1943+ }
1944+ // Read the first frame, not the stream: a prores_ks mov's stream-level range depends on
1945+ // the ffprobe version (7.1 and 8.0 report unknown), while every version tags the frames.
1946+ const tags = spawnSync ( getFfprobeBinary ( ) , [
1947+ "-v" ,
1948+ "error" ,
1949+ "-select_streams" ,
1950+ "v:0" ,
1951+ "-read_intervals" ,
1952+ "%+#1" ,
1953+ "-show_entries" ,
1954+ "frame=color_range,color_space,color_primaries,color_transfer" ,
1955+ "-of" ,
1956+ "csv=p=0" ,
1957+ "--" ,
1958+ out ,
1959+ ] ) ;
1960+ expect ( tags . stdout . toString ( ) . trim ( ) ) . toBe ( "tv,bt709,bt709,bt709" ) ;
1961+ } finally {
1962+ rmSync ( dir , { recursive : true , force : true } ) ;
1963+ }
1964+ } , 30_000 ) ;
18951965} ) ;
0 commit comments