@@ -3,8 +3,8 @@ import { EventEmitter } from "events";
33import { spawnSync } from "child_process" ;
44import { mkdtempSync , readFileSync , renameSync , rmSync , writeFileSync } from "fs" ;
55import { tmpdir } from "os" ;
6- import { basename , resolve } from "path" ;
7- import { afterEach , describe , expect , it , vi } from "vitest" ;
6+ import { basename , join , resolve } from "path" ;
7+ import { afterAll , afterEach , beforeAll , describe , expect , it , vi } from "vitest" ;
88import { extractMediaMetadata , extractPngMetadataFromBuffer , parseFrameRate } from "./ffprobe.js" ;
99
1010function crc32 ( buf : Buffer ) : number {
@@ -1203,6 +1203,39 @@ describe("ffprobe missing-binary fallback", () => {
12031203 expect ( meta . hasAlpha ) . toBe ( true ) ;
12041204 } ) ;
12051205
1206+ it ( "leaves alphaPlaneDecoded absent when the untagged VP9 decode probe fails" , async ( ) => {
1207+ const { spawn, calls } = createSpawnSpy ( [
1208+ {
1209+ kind : "exit" ,
1210+ code : 0 ,
1211+ stdout : JSON . stringify ( {
1212+ streams : [
1213+ {
1214+ codec_type : "video" ,
1215+ codec_name : "vp9" ,
1216+ width : 320 ,
1217+ height : 180 ,
1218+ r_frame_rate : "30/1" ,
1219+ avg_frame_rate : "30/1" ,
1220+ pix_fmt : "yuv420p" ,
1221+ } ,
1222+ ] ,
1223+ format : { duration : "1.5" } ,
1224+ } ) ,
1225+ } ,
1226+ { kind : "exit" , code : 1 , stderr : "decoder failed" } ,
1227+ ] ) ;
1228+ vi . resetModules ( ) ;
1229+ vi . doMock ( "child_process" , ( ) => ( { spawn } ) ) ;
1230+
1231+ const { extractMediaMetadata : extractMediaMetadataMocked } = await import ( "./ffprobe.js" ) ;
1232+ const meta = await extractMediaMetadataMocked ( "/tmp/probe-fails.webm" ) ;
1233+
1234+ expect ( calls [ 1 ] ?. args ) . toEqual ( expect . arrayContaining ( [ "-c:v" , "libvpx-vp9" ] ) ) ;
1235+ expect ( meta . hasAlpha ) . toBe ( false ) ;
1236+ expect ( "alphaPlaneDecoded" in meta ) . toBe ( false ) ;
1237+ } ) ;
1238+
12061239 it ( "extractMediaMetadata rethrows ffprobe-missing error for non-image files without fallback" , async ( ) => {
12071240 const { spawn } = createSpawnSpy ( [ { kind : "missing" } ] ) ;
12081241 hidePathBinaries ( ) ;
@@ -2136,3 +2169,69 @@ describe("runFfprobe process and stream handling", () => {
21362169 ) ;
21372170 } ) ;
21382171} ) ;
2172+
2173+ describe . skipIf ( ! HAS_FFPROBE ) ( "VP8/VP9 decoded alpha plane probe on real files" , ( ) => {
2174+ let dir : string ;
2175+ const encode = ( name : string , args : string [ ] ) : string => {
2176+ const out = join ( dir , name ) ;
2177+ const res = spawnSync ( "ffmpeg" , [
2178+ "-v" ,
2179+ "error" ,
2180+ "-f" ,
2181+ "lavfi" ,
2182+ "-i" ,
2183+ "[email protected] :s=64x64:d=1,format=rgba" , 2184+ ...args ,
2185+ "-y" ,
2186+ out ,
2187+ ] ) ;
2188+ expect ( res . status ) . toBe ( 0 ) ;
2189+ return out ;
2190+ } ;
2191+ let alpha : string ;
2192+ let alphaVp8 : string ;
2193+ let opaqueVp9 : string ;
2194+ let opaqueVp8 : string ;
2195+ let mpeg4 : string ;
2196+ beforeAll ( ( ) => {
2197+ dir = mkdtempSync ( join ( tmpdir ( ) , "hf-decoded-alpha-" ) ) ;
2198+ alpha = encode ( "alpha.webm" , [ "-c:v" , "libvpx-vp9" , "-pix_fmt" , "yuva420p" ] ) ;
2199+ // libvpx refuses transparency with its default alt-ref frames.
2200+ alphaVp8 = encode ( "alpha-vp8.webm" , [
2201+ "-c:v" ,
2202+ "libvpx" ,
2203+ "-auto-alt-ref" ,
2204+ "0" ,
2205+ "-pix_fmt" ,
2206+ "yuva420p" ,
2207+ ] ) ;
2208+ opaqueVp9 = encode ( "opaque-vp9.webm" , [ "-c:v" , "libvpx-vp9" , "-pix_fmt" , "yuv420p" ] ) ;
2209+ opaqueVp8 = encode ( "opaque-vp8.webm" , [ "-c:v" , "libvpx" , "-pix_fmt" , "yuv420p" ] ) ;
2210+ mpeg4 = encode ( "opaque.mp4" , [ "-c:v" , "mpeg4" , "-pix_fmt" , "yuv420p" ] ) ;
2211+ } ) ;
2212+ afterAll ( ( ) => rmSync ( dir , { recursive : true , force : true } ) ) ;
2213+
2214+ it ( "probeDecodedAlphaPlane sees the alpha plane only in the alpha stream" , async ( ) => {
2215+ const { probeDecodedAlphaPlane } = await import ( "./ffprobe.js" ) ;
2216+ expect ( await probeDecodedAlphaPlane ( alpha , "libvpx-vp9" ) ) . toBe ( true ) ;
2217+ expect ( await probeDecodedAlphaPlane ( alphaVp8 , "libvpx" ) ) . toBe ( true ) ;
2218+ expect ( await probeDecodedAlphaPlane ( opaqueVp9 , "libvpx-vp9" ) ) . toBe ( false ) ;
2219+ expect ( await probeDecodedAlphaPlane ( opaqueVp8 , "libvpx" ) ) . toBe ( false ) ;
2220+ expect ( await probeDecodedAlphaPlane ( join ( dir , "missing.webm" ) , "libvpx-vp9" ) ) . toBeUndefined ( ) ;
2221+ } ) ;
2222+
2223+ it ( "extractMediaMetadata records alphaPlaneDecoded=false only for untagged VP8/VP9" , async ( ) => {
2224+ const { extractMediaMetadata } = await import ( "./ffprobe.js" ) ;
2225+ const vp9 = await extractMediaMetadata ( opaqueVp9 ) ;
2226+ expect ( vp9 ) . toMatchObject ( { videoCodec : "vp9" , hasAlpha : false , alphaPlaneDecoded : false } ) ;
2227+ const vp8 = await extractMediaMetadata ( opaqueVp8 ) ;
2228+ expect ( vp8 ) . toMatchObject ( { videoCodec : "vp8" , hasAlpha : false , alphaPlaneDecoded : false } ) ;
2229+ // Tagged alpha is already decided by hasAlpha, so it is not probed.
2230+ const tagged = await extractMediaMetadata ( alpha ) ;
2231+ expect ( tagged . hasAlpha ) . toBe ( true ) ;
2232+ expect ( "alphaPlaneDecoded" in tagged ) . toBe ( false ) ;
2233+ const other = await extractMediaMetadata ( mpeg4 ) ;
2234+ expect ( other . videoCodec ) . toBe ( "mpeg4" ) ;
2235+ expect ( "alphaPlaneDecoded" in other ) . toBe ( false ) ;
2236+ } ) ;
2237+ } ) ;
0 commit comments