mediaforge - v2.2.0
    Preparing search index...

    Class FFmpegBuilder

    Fluent FFmpeg builder. All methods return this for chaining. Call .run() to execute or .spawn() for event-based control.

    await new FFmpegBuilder('input.mp4')
    .videoCodec('libx264')
    .videoBitrate('2M')
    .audioCodec('aac')
    .output('output.mp4')
    .run();
    Index

    Constructors

    Methods

    • Override the ffmpeg binary path (default: FFMPEG_PATH env or 'ffmpeg')

      Parameters

      • path: string

      Returns this

    • Check if a codec is available in the current binary. Does not throw — returns a GuardResult.

      Parameters

      • codec: string
      • direction: "encode" | "decode" = 'encode'

      Returns GuardResult

    • Check if a feature is available by version gate + capability.

      Parameters

      • featureKey: string

      Returns GuardResult

    • Select the best available video codec from a priority list. Returns null if none are available.

      Parameters

      Returns string | null

      const codec = builder.selectVideoCodec([
      { codec: 'h264_nvenc', featureKey: 'nvenc' },
      { codec: 'h264_vaapi' },
      { codec: 'libx264' },
      ]);
      builder.videoCodec(codec ?? 'libx264');
    • Select the best available hardware accelerator from a priority list.

      Parameters

      • candidates: string[]

      Returns string | null

    • Overwrite output files without asking (default: true)

      Parameters

      • yes: boolean = true

      Returns this

    • Enable progress reporting on stderr

      Returns this

    • Seek within the last-added input (fast seek, before -i)

      Parameters

      • position: string | number

      Returns this

    • Limit duration of the last-added input

      Parameters

      • duration: string | number

      Returns this

    • Force format of the last-added input

      Parameters

      • format: string

      Returns this

    • Add (or switch to) an output path. Call before codec/filter options.

      Parameters

      Returns this

    • Set video codec, e.g. 'libx264', 'libx265', 'copy'

      Parameters

      • codec: string

      Returns this

    • Set video bitrate, e.g. '2M', '4000k'

      Parameters

      • bitrate: string | number

      Returns this

    • Set output frame rate (-r after -i)

      Parameters

      • rate: string | number

      Returns this

    • Set input frame rate (-r before -i, for raw inputs)

      Parameters

      • rate: string | number

      Returns this

    • Set output video size, e.g. '1280x720' or '1280:720'

      Parameters

      • widthXheight: string

      Returns this

    • Set video filter chain (-vf). Accepts a filter string or a FilterChain / serialized filter function result.

      Parameters

      Returns this

    • Set pixel format

      Parameters

      • fmt: string

      Returns this

    • Set CRF value (codec-dependent)

      Parameters

      • value: number

      Returns this

    • Disable video stream

      Returns this

    • Set encoder preset (-preset), e.g. 'slow', 'faster', 'p4'

      Parameters

      • value: string

      Returns this

    • Set H.264/H.265 profile (-profile:v)

      Parameters

      • value: string

      Returns this

    • Set H.264/H.265 level (-level:v), e.g. '4.1'

      Parameters

      • value: string | number

      Returns this

    • Set container/muxer flags (-movflags), e.g. '+faststart'. Repeatable: movflags('+faststart').movflags('+frag_keyframe').

      Parameters

      • value: string

      Returns this

    • Max GOP size in frames (-g). For HLS this is what determines the keyframe interval, so it must be a multiple of the segment duration × fps.

      Parameters

      • frames: number

      Returns this

    • Constrain frame-rate conversion. Default ffmpeg behaviour is 'auto', which silently duplicates or drops frames depending on the input.

      The flag name is version-dependent: -vsync before ffmpeg 5.1 and -fps_mode from 5.1 on. Emitting -fps_mode unconditionally makes every encode fail with "Unrecognized option 'fps_mode'" on 4.x and 5.0, so the right one is chosen in buildArgs() from the probed binary version.

      Parameters

      Returns this

    • Rate-control bounds (VBV): min/max bitrate plus buffer size.

      Supplying all three keeps the encoder inside a sliding-window bitrate budget, which is what streaming targets require. Omit min to only cap the maximum; bufferSize defaults to max because ffmpeg's VBV needs a buffer to look at, and an unset one produces an unpredictable peak.

      Parameters

      • opts: { min?: string | number; max: string | number; bufferSize?: string | number }

      Returns this

    • Tag output colour properties (-color_primaries / -color_trc / -colorspace / -color_range). Passing an unknown key throws, so a typo cannot silently produce an untagged file.

      Parameters

      Returns this

    • Set audio codec, e.g. 'aac', 'libopus', 'copy'

      Parameters

      • codec: string

      Returns this

    • Set audio bitrate, e.g. '128k', '192k'

      Parameters

      • bitrate: string | number

      Returns this

    • Set audio sample rate, e.g. 44100, 48000

      Parameters

      • rate: number

      Returns this

    • Set number of audio channels

      Parameters

      • channels: number

      Returns this

    • Set audio filter chain (-af). Accepts a filter string or a FilterChain / serialized filter function result.

      Parameters

      Returns this

    • Disable audio stream

      Returns this

    • Set subtitle codec

      Parameters

      • codec: string

      Returns this

    • Disable subtitle stream

      Returns this

    • Force output format

      Parameters

      • format: string

      Returns this

    • Add stream mapping, e.g. '0:v:0', '0:a:1'

      Parameters

      • specifier: string

      Returns this

    • Limit output duration

      Parameters

      • d: string | number

      Returns this

    • Seek in output (re-encode seek, accurate)

      Parameters

      • position: string | number

      Returns this

    • Pass arbitrary extra args for the current output

      Parameters

      • ...args: string[]

      Returns this

    • Pass arbitrary extra args at the global level

      Parameters

      • ...args: string[]

      Returns this

    • Enable hardware acceleration (-hwaccel). Does NOT validate availability — use checkHwaccel() first.

      Parameters

      • accel: string
      • Optionalopts: { device?: string }

      Returns this

    • Set -filter_complex string directly. Only the last call takes effect.

      Parameters

      • filter: string

      Returns this

    • Every input path this builder will read, in order.

      Returns string[]

    • Every output path this builder will write, in order.

      Returns string[]

    • Run ffmpeg and return a Promise that resolves on success.

      Rejects with a typed error: FFmpegSpawnError on a non-zero exit, FFmpegTimeoutError when timeout elapses, FFmpegAbortError when signal fires.

      Parameters

      Returns Promise<void>

      // Cancel an encode from an HTTP request being aborted.
      const controller = new AbortController();
      req.on('close', () => controller.abort());
      await ffmpeg('in.mp4').output('out.mp4').run({ signal: controller.signal });
      // Retry a flaky encode, writing atomically so a retry never resumes
      // from a half-written file.
      await withAtomicOutput('out.mp4', async (temp) =>
      ffmpeg('in.mp4').output(temp).run({ retry: { retries: 3 } }),
      );
    • Probe the binary version and return a human-readable string. Also caches the version for subsequent requireVersion() checks.

      Returns Promise<string>

    • Return the CLI arguments without running ffmpeg. Useful for dry-runs, inspection, or custom piping.

      Returns string[]

      const args = ffmpeg('input.mp4').videoCodec('libx264').output('out.mp4').dry();
      // args: ['-i', 'input.mp4', '-c:v', 'libx264', '-c:a', 'aac', 'out.mp4']
    • Return a human-readable command line string. Useful for debugging or logging.

      Returns string