11// fallow-ignore-file code-duplication complexity
22import { spawn } from "child_process" ;
33import { readFileSync } from "fs" ;
4- import { extname } from "path" ;
4+ import { basename , extname } from "path" ;
5+ import { redactTelemetryString } from "@hyperframes/core" ;
56import { FFPROBE_PATH_ENV , getFfprobeBinary } from "./ffmpegBinaries.js" ;
67import { ManagedChildProcess } from "./managedChildProcess.js" ;
78import { trackChildProcess } from "./processTracker.js" ;
89
10+ const FFPROBE_STDERR_MAX_BYTES = 8 * 1024 ;
11+ const FFPROBE_ERROR_MAX_CHARS = 4 * 1024 ;
12+
13+ function redactFfprobeInput ( stderr : string , filePath : string ) : string {
14+ if ( ! filePath ) return stderr ;
15+
16+ let redacted = stderr . split ( filePath ) . join ( "[input]" ) ;
17+ const inputBasename = basename ( filePath ) ;
18+ if ( inputBasename ) redacted = redacted . split ( inputBasename ) . join ( "[input]" ) ;
19+
20+ // ManagedChildProcess retains an 8 KiB byte tail. When that boundary lands
21+ // inside the input path, stderr starts with only a suffix of the path, so
22+ // neither exact-path nor generic absolute-path redaction can recognize it.
23+ if ( ! redacted . startsWith ( "[input]" ) ) {
24+ for ( let offset = 1 ; offset < filePath . length ; offset += 1 ) {
25+ const suffix = filePath . slice ( offset ) ;
26+ if ( suffix . length < 4 || ! redacted . startsWith ( suffix ) ) continue ;
27+ const boundary = redacted [ suffix . length ] ;
28+ if ( boundary !== undefined && ! / [ \s : ' " ) \] } ] / . test ( boundary ) ) continue ;
29+ redacted = `[input]${ redacted . slice ( suffix . length ) } ` ;
30+ break ;
31+ }
32+ }
33+
34+ return redacted ;
35+ }
36+
37+ function sanitizeFfprobeDiagnostic ( stderr : string , filePath : string ) : string {
38+ const stderrWithoutInput = redactFfprobeInput ( stderr , filePath ) ;
39+ const redacted = redactTelemetryString ( stderrWithoutInput , FFPROBE_STDERR_MAX_BYTES ) ;
40+ if ( redacted . length <= FFPROBE_ERROR_MAX_CHARS ) return redacted ;
41+ return `…${ redacted . slice ( - ( FFPROBE_ERROR_MAX_CHARS - 1 ) ) } ` ;
42+ }
43+
944/** Spawn ffprobe with given args, return stdout. Throws on non-zero exit or missing binary. */
10- async function runFfprobe ( args : string [ ] , signal ?: AbortSignal ) : Promise < string > {
45+ async function runFfprobe (
46+ filePath : string ,
47+ argsWithoutInput : string [ ] ,
48+ signal ?: AbortSignal ,
49+ ) : Promise < string > {
1150 const command = getFfprobeBinary ( ) ;
12- const proc = spawn ( command , args ) ;
51+ const proc = spawn ( command , [ "-v" , "error" , ... argsWithoutInput , filePath ] ) ;
1352 trackChildProcess ( proc ) ;
1453 let stdout = "" ;
1554 proc . stdout . on ( "data" , ( data ) => {
@@ -18,6 +57,7 @@ async function runFfprobe(args: string[], signal?: AbortSignal): Promise<string>
1857 const managed = new ManagedChildProcess ( proc , {
1958 signal,
2059 deadlineAtMs : Date . now ( ) + 30_000 ,
60+ stderrMaxBytes : FFPROBE_STDERR_MAX_BYTES ,
2161 } ) ;
2262 const outcome = await managed . wait ( ) ;
2363 if ( outcome . reason === "spawn_error" ) {
@@ -32,8 +72,9 @@ async function runFfprobe(args: string[], signal?: AbortSignal): Promise<string>
3272 throw outcome . error ?? new Error ( outcome . stderr ) ;
3373 }
3474 if ( outcome . reason !== "exit" || outcome . exitCode !== 0 ) {
75+ const diagnostic = sanitizeFfprobeDiagnostic ( outcome . stderr , filePath ) ;
3576 throw new Error (
36- `[FFmpeg] ffprobe ${ outcome . reason } with code ${ outcome . exitCode } : ${ outcome . stderr } ` ,
77+ `[FFmpeg] ffprobe ${ outcome . reason } with code ${ outcome . exitCode } : ${ diagnostic } ` ,
3778 ) ;
3879 }
3980 return stdout ;
@@ -263,14 +304,11 @@ export async function extractMediaMetadata(filePath: string): Promise<VideoMetad
263304
264305 let output : FFProbeOutput | null = null ;
265306 try {
266- const stdout = await runFfprobe ( [
267- "-v" ,
268- "quiet" ,
307+ const stdout = await runFfprobe ( filePath , [
269308 "-print_format" ,
270309 "json" ,
271310 "-show_format" ,
272311 "-show_streams" ,
273- filePath ,
274312 ] ) ;
275313 output = parseProbeJson ( stdout ) ;
276314 } catch ( error ) {
@@ -361,7 +399,8 @@ export async function extractAudioMetadata(
361399
362400 const probePromise = ( async ( ) : Promise < AudioMetadata > => {
363401 const stdout = await runFfprobe (
364- [ "-v" , "quiet" , "-print_format" , "json" , "-show_format" , "-show_streams" , filePath ] ,
402+ filePath ,
403+ [ "-print_format" , "json" , "-show_format" , "-show_streams" ] ,
365404 options ?. signal ,
366405 ) ;
367406 const output = parseProbeJson ( stdout ) ;
@@ -373,17 +412,14 @@ export async function extractAudioMetadata(
373412 const sampleRate = audioStream . sample_rate ? parseInt ( audioStream . sample_rate ) : 44100 ;
374413 const audioCodec = audioStream . codec_name || "unknown" ;
375414 if ( audioCodec === "aac" && sampleRate > 0 ) {
376- const packetStdout = await runFfprobe ( [
377- "-v" ,
378- "quiet" ,
415+ const packetStdout = await runFfprobe ( filePath , [
379416 "-select_streams" ,
380417 "a:0" ,
381418 "-count_packets" ,
382419 "-show_entries" ,
383420 "stream=nb_read_packets" ,
384421 "-print_format" ,
385422 "json" ,
386- filePath ,
387423 ] ) ;
388424 const packetOutput = parseProbeJson ( packetStdout ) ;
389425 const packetCount = Number ( packetOutput . streams [ 0 ] ?. nb_read_packets ) ;
@@ -441,9 +477,7 @@ export async function analyzeKeyframeIntervals(filePath: string): Promise<Keyfra
441477}
442478
443479async function analyzeKeyframeIntervalsUncached ( filePath : string ) : Promise < KeyframeAnalysis > {
444- const stdout = await runFfprobe ( [
445- "-v" ,
446- "quiet" ,
480+ const stdout = await runFfprobe ( filePath , [
447481 "-select_streams" ,
448482 "v:0" ,
449483 "-skip_frame" ,
@@ -452,7 +486,6 @@ async function analyzeKeyframeIntervalsUncached(filePath: string): Promise<Keyfr
452486 "frame=pts_time" ,
453487 "-of" ,
454488 "csv=p=0" ,
455- filePath ,
456489 ] ) ;
457490
458491 const timestamps = stdout
0 commit comments