FFmpeg: The One-Liner to Rule All Media Files

TL;DR

FFmpeg is a powerful command-line tool to handle all your media needs. Use it to quickly convert formats, create GIFs, resize images, trim videos, and extract audio right from your terminal. It’s the fast, free, and scriptable alternative to heavy video editing software.

Introduction to the Library

FFmpeg is a complete, cross-platform solution to record, convert, and stream audio and video. It’s the engine behind major platforms like YouTube and Netflix. While it can seem complex due to its command-line interface and numerous parameters, knowing the right commands can make it an invaluable tool for any media-related work. It originated in the early 2000s and has since become a cornerstone of internet video.

Installation Process

Here’s how to install FFmpeg on different operating systems:

macOS

The easiest way to install FFmpeg on a Mac is by using Homebrew, a package manager for macOS.

  1. Open the Terminal app.
  2. If you don’t have Homebrew installed, run the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Once Homebrew is installed, you can install FFmpeg with this command:
brew install ffmpeg

Windows

  1. Download the latest FFmpeg build from the official website. The builds from gyan.dev are recommended.
  2. Extract the downloaded .7z file using a tool like 7-Zip.
  3. Rename the extracted folder to FFmpeg and move it to the root of your C: drive (C:\FFmpeg).
  4. Add FFmpeg to your system’s PATH environment variable:
    • Search for “Edit the system environment variables” in the Start Menu and open it.
    • In the System Properties window, click on “Environment Variables”.
    • Under “System variables,” find and select the “Path” variable, then click “Edit.”
    • Click “New” and add the path to the bin folder inside your FFmpeg directory: C:\FFmpeg\bin.
    • Click “OK” to close all windows.
  5. Open Command Prompt and type ffmpeg to verify the installation.

Linux

You can use your distribution’s package manager to install FFmpeg.

  • Debian/Ubuntu:
sudo apt update
sudo apt install ffmpeg
  • Fedora/CentOS:
sudo dnf install ffmpeg
  • Arch Linux:
sudo pacman -S ffmpeg

Important Commands

Here are some of the most common and useful FFmpeg commands:

Image Resizing

To resize an image, you can use the scale filter. To maintain the aspect ratio, you can specify one dimension and set the other to -1.

ffmpeg -i input.jpg -vf scale=320:-1 output.png

This command resizes input.jpg to a width of 320 pixels while maintaining the original aspect ratio.

GIF Creation

You can easily create a GIF from a video. The following command creates a high-quality GIF from a specific portion of a video.

ffmpeg -ss 30 -t 3 -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 output.gif
  • -ss 30: Starts the GIF 30 seconds into the video.
  • -t 3: Makes the GIF 3 seconds long.
  • fps=10: Sets the frame rate to 10 frames per second.
  • scale=320:-1: Resizes the GIF to a width of 320 pixels.
  • The palettegen and paletteuse filters create a custom color palette for better quality.
  • -loop 0: Makes the GIF loop infinitely.

Video Format Changes

Converting a video from one format to another is straightforward.

ffmpeg -i input.mp4 output.avi

This command converts an MP4 file to an AVI file. You can also specify video and audio codecs, bitrates, and other options for more control over the output.

Other Useful Commands

  • Extract Audio:
ffmpeg -i input.mp4 -vn output.mp3
  • Cut a Video:
ffmpeg -i input.mp4 -ss 00:00:30 -t 00:00:30 output.mp4
  • Compress a Video:
ffmpeg -i input.mp4 -vcodec libx264 -crf 23 output.mp4

Use Cases

FFmpeg is incredibly versatile. Here are some of its key use cases:

  • Media Conversion: Convert audio and video files between various formats.

  • Video and Audio Editing: Trim, crop, merge, and apply filters to media files.

  • Streaming: Stream live video and audio content over the internet.

  • Screen Recording and Capture: Record your computer screen, capture video from webcams, and record audio from microphones.

  • Automation: Integrate FFmpeg into scripts to automate repetitive multimedia tasks.

  • Watermarking: Add text or image watermarks to your videos.

Buy me a coffee


View this page on GitHub