OWolf

2024-09-24 Video Production

Essential ffmpeg and ffprobe Commands for Video and Audio Processing

By O. Wolfson

ffmpeg and ffprobe are powerful tools for video and audio manipulation. Whether you're extracting metadata or converting video formats, these commands are versatile and crucial for handling media files. Below, we’ll explore some of the most useful commands, including how to extract metadata from a video file.

Extracting Metadata with ffprobe

The following command extracts detailed metadata from a video file and saves it as a JSON file:

bash
ffprobe -v quiet -print_format json -show_format -show_streams video.mp4 > video.metadata.json
  • -v quiet: Suppresses unnecessary output.
  • -print_format json: Outputs the metadata in JSON format.
  • -show_format -show_streams: Displays format and stream information.

This is incredibly useful for understanding the properties of your media files, including codecs, bitrates, and resolutions.

Converting Video Format with ffmpeg

You can easily convert between video formats using this command:

bash
ffmpeg -i input.mov -c:v libx264 output.mp4
  • -i input.mov: Specifies the input file.
  • -c:v libx264: Encodes the video using the H.264 codec, one of the most common and efficient codecs.

This is handy when you need to switch formats for compatibility purposes or to reduce file size.

Compressing Video Files

If you need to reduce the file size of a video while maintaining quality, you can use this command:

bash
ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4
  • -vcodec libx265: Uses the H.265 codec, which offers better compression than H.264.
  • -crf 28: Constant Rate Factor (CRF) controls quality; a lower value means better quality (ranges from 0-51).

Extracting Audio from a Video

To extract only the audio from a video file, use:

bash
ffmpeg -i input.mp4 -q:a 0 -map a output.mp3
  • -q:a 0: Ensures the highest quality for the audio.
  • -map a: Maps only the audio stream to the output.

This is particularly useful if you need to create an audio file from a video.

Concatenating Multiple Videos

If you want to merge several video files into one, use a text file listing all the videos and run:

bash
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
  • filelist.txt: Contains the list of videos to concatenate (e.g., file 'video1.mp4' on each line).

This is helpful for combining multiple video clips into a single file.

Extracting a Thumbnail from a Video

To extract a specific frame as a thumbnail from a video:

bash
ffmpeg -i input.mp4 -ss 00:00:10.000 -vframes 1 thumbnail.png
  • -ss 00:00:10.000: Seeks to the 10-second mark in the video.
  • -vframes 1: Extracts a single frame.

This is perfect for creating preview images for video files.

Creating a GIF from a Video

To convert a video to a GIF:

bash
ffmpeg -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" -c:v gif output.gif
  • -vf "fps=10,scale=320:-1:flags=lanczos": Sets the frame rate, size, and scaling method.
  • -c:v gif: Specifies the output format as GIF.

This is a great way to create animated GIFs from video clips.

Adding Subtitles to a Video

To add subtitles to a video:

bash
ffmpeg -i input.mp4 -i subtitles.srt -c copy -c:s mov_text output.mp4
  • -i subtitles.srt: Specifies the subtitle file.
  • -c:s mov_text: Encodes the subtitles in the QuickTime Text format.

This is useful for including subtitles in your videos.

Converting Stereo Audio to 5.1 Surround Sound

Basic Mapping Strategy

  • Front left and front right (FL/FR): Send the left and right stereo channels.
  • Center (C): Mix both stereo channels to create a center channel.
  • LFE (Subwoofer): Use a low-pass filter to send bass frequencies to the subwoofer.
  • Surround left and surround right (SL/SR): Replicate the left and right stereo channels at a reduced volume to simulate ambient sound.

FFmpeg Command

Here’s an ffmpeg command that maps stereo audio to 5.1 surround:

bash
ffmpeg -i video.mp4 -filter_complex \
"[0:a]pan=5.1|c0=c0|c1=c1|c2=c0+c1|c3=c0+c1|c4=c0|c5=c1" \
-ac 6 -ar 48000 -b:a 640k video-surround.mp4

Explanation:

  • pan=5.1 defines the output channel layout as 5.1 surround.
  • c0=c0 and c1=c1 map the left and right stereo channels to the front left (FL) and front right (FR) speakers.
  • c2=c0+c1 mixes both stereo channels into the center (FC) channel.
  • c3=c0+c1 mixes both stereo channels into the LFE (subwoofer) channel.
  • c4=c0 and c5=c1 map the left and right stereo channels to the surround left (SL) and surround right (SR) speakers.

Customizing the Mapping for More Precision

If you want to split the frequency spectrum (e.g., bass goes to subwoofer, high tones to surrounds), you can use additional ffmpeg filters like firequalizer or bass, for example:

bash
ffmpeg -i video.mp4 -filter_complex \
"[0:a]channelsplit=channel_layout=stereo[left][right]; \
[left]bass=g=10,pan=5.1|c0=FL|c1=FR|c2=FC|c3=LFE|c4=BL|c5=BR[left_eq]; \
[right]bass=g=10[left_eq]" \
-ac 6 -ar 48000 -b:a 640k video-surround.mp4

This is a more advanced filter chain that can be tailored based on the specific sound properties you want.