NPM fluent-ffmpeg Package


Usage

Installation

npm install fluent-ffmpeg

Basic Example

const ffmpeg = require('fluent-ffmpeg');

const command = ffmpeg('input.mp4')
  .withVideoCodec('libx264')
  .withAudioCodec('aac')
  .withOutputOption('-movflags faststart')
  .withOutputOption('-hls_time 10')
  .toFormat('hls')
  .output('output.m3u8');

// trigger transcoding
command.run();

Implementation Guide

Instance Methods

input(source)

  • Specify the input file or stream.

output(destination)

  • Specify the output file or stream.

addInput(source)

  • Add an additional input file or stream.

videoCodec(codec)

  • Set the video codec.

videoBitrate(bitrate)

  • Set the video bitrate in kbps.

videoResolution(width, height)

  • Set the video resolution.

audioCodec(codec)

  • Set the audio codec.

audioBitrate(bitrate)

  • Set the audio bitrate in kbps.

audioFrequency(frequency)

  • Set the audio frequency in Hz.

audioChannels(channels)

  • Set the number of audio channels (1 for mono, 2 for stereo).

withOutputOption(option)

  • Add a custom output option.

format(format)

  • Specify the output format.

outputOptions(options)

  • Set multiple output options at once.

on(event, listener)

  • Listen for transcoding events.

run()

  • Trigger transcoding.

kill()

  • Kill the transcoding process.

Configuration Options

logger

  • Custom logger to use (defaults to console).

cwd

  • Custom working directory to use (defaults to the current directory).

timeout

  • Timeout for transcoding in milliseconds (defaults to 0).

args

  • Additional arguments to pass to the FFmpeg binary.

force

  • Force overwrite of output file (defaults to false).

maxBufferLength

  • The maximum buffer length for the input stream (defaults to 1024).

maxRetries

  • The number of times to retry a failed transcoding process (defaults to 0).

retryDelay

  • The delay between retries in milliseconds (defaults to 1000).

Best Practices

  • Use the most recent version of FFmpeg.
  • Use hardware acceleration if possible.
  • Optimize your input files for transcoding.
  • Use a CDN to deliver your videos.
  • Monitor your transcoding processes.