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
- Converting Video Format with ffmpeg
- Compressing Video Files
- Extracting Audio from a Video
- Concatenating Multiple Videos
- Extracting a Thumbnail from a Video
- Creating a GIF from a Video
- Adding Subtitles to a Video
- Converting Stereo Audio to 5.1 Surround Sound
Extracting Metadata with ffprobe
The following command extracts detailed metadata from a video file and saves it as a JSON file:
-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:
-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:
-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:
-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:
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:
-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:
-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:
-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:
Explanation:
pan=5.1
defines the output channel layout as 5.1 surround.c0=c0
andc1=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
andc5=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:
This is a more advanced filter chain that can be tailored based on the specific sound properties you want.