ffmpeg Command: Tutorial & Examples

Manipulate multimedia files from the command line

ffmpeg is a versatile command-line utility for decoding, encoding, transcoding, muxing, demuxing, streaming, filtering, and playing virtually any multimedia file format. It is widely used on Linux servers and virtual machines (VMs) to process audio and video content without requiring a graphical interface. This article explains what ffmpeg is, how it works, and how to use it effectively with practical examples.

What ffmpeg Is and Why It Matters

ffmpeg is a comprehensive multimedia framework that allows users to convert audio and video formats, extract or replace audio tracks, resize and filter videos, create screenshots, and much more. It supports a vast array of codecs and container formats, making it an indispensable tool in server environments for media processing, streaming, and automated workflows.

On Linux servers and VMs, where graphical tools are not practical or available, ffmpeg provides the power to manipulate multimedia files entirely via the command-line. Its performance, flexibility, and wide format support make it important for tasks such as video transcoding in media servers, automated video processing pipelines, and streaming setups.

Installation

Before using ffmpeg, ensure it is installed on your system. On Debian-based distributions like Ubuntu, you can install it using the apt-get package manager:

sudo apt-get update
sudo apt-get install ffmpeg

For other distributions, use the respective package manager, such as dnf for Fedora or pacman for Arch Linux.

How to Use ffmpeg

ffmpeg uses a simple syntax: specify input files, options, and output files. The most basic command converts a file from one format to another:

ffmpeg -i input.mp4 output.avi

This converts an MP4 video to AVI format by decoding and re-encoding the streams.

Decoding and Encoding

You can convert audio or video files between formats:

ffmpeg -i input.mp3 output.wav

This converts an MP3 audio file to WAV format.

Extracting Audio From Video

To extract only the audio stream from a video file:

ffmpeg -i input.mp4 -vn output.mp3

The -vn option disables video recording in the output.

Applying Filters and Effects

ffmpeg supports many filters to modify audio and video streams.

  • Resize a video to 640x480 pixels:

    ffmpeg -i input.mp4 -vf "scale=640:480" output.mp4
    
  • Add a watermark image at position (10,10):

    ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10" output.mp4
    
  • Increase audio volume by 2x:

    ffmpeg -i input.mp3 -af "volume=2.0" output.mp3
    

Concatenating Multiple Files

To concatenate files without re-encoding, create a text file list.txt with content like:

file 'file1.mp4'
file 'file2.mp4'
file 'file3.mp4'

Then run:

ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp4

This merges the files quickly and without quality loss.

Alternatively, for certain container formats, you can concatenate files using:

ffmpeg -i "concat:file1.ts|file2.ts|file3.ts" -c copy output.ts

Capturing Screenshots

To capture a single frame from a video at a specific timestamp:

ffmpeg -ss 00:01:30 -i input.mp4 -vframes 1 screenshot.jpg

This captures a frame at 1 minute and 30 seconds.

Transcoding with Specific Codecs

To convert a video using the H.264 codec for video and AAC for audio:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4

This is useful for compatibility or compression purposes.

Joining Files with Audio and Video Maps

When joining files with multiple audio tracks, ensure you map both audio and video streams:

ffmpeg -i "concat:VTS_01_1.VOB|VTS_01_2.VOB|VTS_01_3.VOB" -map 0:v -map 0:a -c copy output.VOB

Common Command-Line Parameters

Some frequently used options include:

1. -i <file> : Specify input file.

2. -c:v <codec> : Set video codec (e.g., libx264, copy).

3. -c:a <codec> : Set audio codec (e.g., aac, mp3, copy).

4. -vf <filter> : Apply video filters.

5. -af <filter> : Apply audio filters.

6. -ss <time> : Seek to a position before processing.

7. -t <duration> : Process only a duration of media.

8. -f <format> : Force format for output or input.

9. -map <stream> : Select specific streams for output.

10. -b:v <bitrate> : Set video bitrate.

11. -b:a <bitrate> : Set audio bitrate.

12. -preset <preset> : Set encoding speed preset (e.g., ultrafast, slow).

13. -r <fps> : Set frame rate.

14. -async <value> : Audio sync method (useful for sync issues).

Troubleshooting and Common Errors

Unsupported Formats

If you see errors about unsupported formats or codecs, your ffmpeg build may lack necessary libraries. Installing ffmpeg from official repositories or compiling with appropriate codec support can help.

Audio/Video Sync Issues

Audio and video may become unsynchronized after transcoding. Use the -async option to correct this:

ffmpeg -i input.mp4 -async 1 output.mp4

Permission Issues

When reading or writing files, ensure you have appropriate permissions. Lack of write access can cause failures.

File Not Found or Incorrect Paths

Double-check file paths and names to avoid errors like "No such file or directory".

Tips and Best Practices

  • Use -c copy to avoid re-encoding when possible, which saves time and preserves quality.
  • Always test conversions on short clips before processing large files.
  • Use hardware acceleration options (if available) for faster encoding.
  • Keep your ffmpeg version updated for the latest codec support.
  • Use verbose logging (-loglevel debug) for detailed error information.
  • Beware of processing untrusted files due to potential security vulnerabilities.

Scripting and Automation Examples

ffmpeg can be integrated into shell scripts for batch processing:

Example script to convert all .mov files in a directory to .mp4:

    #!/bin/bash
    for file in *.mov; do
        output="${file%.mov}.mp4"
        ffmpeg -i "$file" -c:v libx264 -c:a aac "$output"
    done

Example to extract audio from all .mp4 videos:

    #!/bin/bash
    for video in *.mp4; do
        audio="${video%.mp4}.mp3"
        ffmpeg -i "$video" -vn -acodec libmp3lame "$audio"
    done

Automate screenshot capture every 10 seconds up to 1 minute:

    ffmpeg -i input.mp4 -vf fps=1/10 -vframes 6 thumbnails_%03d.jpg

See Also

Further Reading

As an Amazon Associate, I earn from qualifying purchases.

The text above is licensed under CC BY-SA 4.0 CC BY SA