Gurpreet Singh

Front-end Architect

Gurpreet Singh

FFmpeg: The One-Liner to Rule All Media Files

#Tech

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.

sudo apt update
sudo apt install ffmpeg
sudo dnf install ffmpeg
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

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:

ffmpeg -i input.mp4 -vn output.mp3
ffmpeg -i input.mp4 -ss 00:00:30 -t 00:00:30 output.mp4
ffmpeg -i input.mp4 -vcodec libx264 -crf 23 output.mp4

Use Cases

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

Reply via email


View posts by tag

NodeJS(3) Tech(14) Privacy(2) NodeJS Beginner Course(6) Thoughts(6) Raspberry PI(3) Sustainability(3) 11ty(1) Vegan(1) NextJS(1)