@@ -181,6 +181,8 @@ export interface Reencoder {
181181 choose : ( sourcePath : string , kind : MediaKind ) => Promise < PresetName | null > ;
182182 /** Positions per second a cut may land on (the frame rate), or null when the file cannot be cut. */
183183 trimGrid ?: ( sourcePath : string , kind : MediaKind ) => Promise < number | null > ;
184+ /** Whether a container named as video holds a video stream; `true` when that cannot be told. */
185+ hasVideo ?: ( sourcePath : string ) => Promise < boolean > ;
184186 run : (
185187 sourcePath : string ,
186188 preset : PresetName ,
@@ -451,7 +453,34 @@ async function trimGrid(sourcePath: string, kind: MediaKind): Promise<number | n
451453 return Number . isFinite ( fps ) && Math . abs ( fps - rate ( average ) ) < 0.01 ? fps : null ;
452454}
453455
454- const ffmpegReencoder : Reencoder = { choose : choosePreset , trimGrid, run : ffmpegReencode } ;
456+ async function hasVideoStream ( sourcePath : string ) : Promise < boolean > {
457+ const ffprobe = findFFprobe ( ) ;
458+ if ( ! ffprobe ) return true ;
459+ try {
460+ const { stdout } = await execFileAsync ( ffprobe , [
461+ "-v" ,
462+ "error" ,
463+ "-select_streams" ,
464+ "v:0" ,
465+ "-show_entries" ,
466+ "stream=codec_type" ,
467+ "-of" ,
468+ "csv=p=0" ,
469+ "--" ,
470+ sourcePath ,
471+ ] ) ;
472+ return stdout . trim ( ) !== "" ;
473+ } catch {
474+ return true ;
475+ }
476+ }
477+
478+ const ffmpegReencoder : Reencoder = {
479+ choose : choosePreset ,
480+ trimGrid,
481+ hasVideo : hasVideoStream ,
482+ run : ffmpegReencode ,
483+ } ;
455484
456485async function stagedSha256 ( file : StagedFile ) : Promise < string > {
457486 if ( Buffer . isBuffer ( file ) ) return createHash ( "sha256" ) . update ( file ) . digest ( "hex" ) ;
@@ -550,23 +579,31 @@ async function planFile(
550579 path : string ,
551580 file : StagedFile ,
552581) : Promise < FilePlan | null > {
553- const kind = mediaKind ( path ) ;
582+ let kind = mediaKind ( path ) ;
554583 const size = stagedSize ( file ) ;
555584 if ( ! kind || size < OFFLOAD_MIN_BYTES ) return null ;
556- const accepted = ACCEPTED [ extname ( path ) . toLowerCase ( ) ] ;
585+ const ext = extname ( path ) . toLowerCase ( ) ;
586+ const accepted = ACCEPTED [ ext ] ;
557587 // The API reads the stored bytes, not the name: a file whose bytes differ from its extension is re-encoded.
558- const asIs =
559- accepted !== undefined &&
560- size <= accepted . maxBytes &&
561- bytesMatchExtension ( extname ( path ) . toLowerCase ( ) , readHead ( file ) ) ;
588+ let asIs =
589+ accepted !== undefined && size <= accepted . maxBytes && bytesMatchExtension ( ext , readHead ( file ) ) ;
590+ let sourcePath : string | undefined ;
591+ if ( kind === "video" && ctx . reencoder . hasVideo ) {
592+ sourcePath = snapshotPath ( path , file , ctx . tempFiles ) ;
593+ // An audio-only MP4 is audio whatever its name says.
594+ if ( ! ( await ctx . reencoder . hasVideo ( sourcePath ) ) ) {
595+ kind = "audio" ;
596+ asIs = false ;
597+ }
598+ }
562599 const mayBeCached =
563600 ctx . opts . upload !== undefined || ( asIs ? ctx . cachedSizes . has ( size ) : ctx . cachedSizes . size > 0 ) ;
564601 const sha256 = mayBeCached ? await stagedSha256 ( file ) : undefined ;
565602 const keyFor = ( variant : string ) => sha256 && `${ ctx . opts . apiBaseUrl } ${ sha256 } ${ variant } ` ;
566603 if ( asIs ) {
567604 return { key : keyFor ( "as-is" ) , sourcePath : "" , contentType : accepted ?. contentType } ;
568605 }
569- const sourcePath = snapshotPath ( path , file , ctx . tempFiles ) ;
606+ sourcePath ?? = snapshotPath ( path , file , ctx . tempFiles ) ;
570607 const preset = await ctx . reencoder . choose ( sourcePath , kind ) ;
571608 // Files left as authored stay in the zip; the archive size check names them.
572609 if ( ! preset ) return null ;
0 commit comments