- WaSQL Wired
- Posts
- A Master Class on FFmpeg
A Master Class on FFmpeg
Master the Swiss army knife of multimedia processing
FFmpeg is a powerful command-line tool and library for processing multimedia files. It can read, convert, and manipulate virtually any audio or video format, supporting more than 100 codecs and numerous container formats including MP4, MKV, WebM, AVI, MOV, and more. Originally developed by Fabrice Bellard in 2000, FFmpeg has grown into one of the most comprehensive open-source multimedia frameworks available. The name combines "FF" for "fast forward" and "MPEG," referring to the Moving Picture Experts Group. FFmpeg serves as the backbone for many popular applications and services that process audio and video, including media players, streaming platforms, and conversion tools.
Installation
FFmpeg is supported on virtually all major operating systems. Here's how to install it:
Windows
# Install with Chocolatey
choco install ffmpeg
# Or download the installer from the official website
# https://ffmpeg.org/download.html#windows
macOS
# Using Homebrew
brew install ffmpeg
# Using MacPorts
sudo port install ffmpeg
Linux
# Debian/Ubuntu
sudo apt update
sudo apt install ffmpeg
# CentOS/RHEL
sudo yum install ffmpeg
# Fedora
sudo dnf install ffmpeg
# Arch Linux
sudo pacman -S ffmpeg
Basic Usage
While FFmpeg can do a lot more, it's most commonly used to convert media files from one format to another. Here are some of the most common format conversions:
# MP4 to WebM
ffmpeg -i input.mp4 -c:v libvpx-vp9 -c:a libopus output.webm
# MKV to MP4
ffmpeg -i input.mkv -c:v libx264 -c:a aac output.mp4
# MOV to MP4
ffmpeg -i input.mov -c:v libx264 -c:a aac output.mp4
# Convert audio formats (MP3 to AAC)
ffmpeg -i input.mp3 -c:a aac output.m4a
# Extract audio from video
ffmpeg -i input.mp4 -vn -c:a copy output.aac
Video Resizing
FFmpeg offers multiple ways to resize video, with the most common being the -vf scale
filter:
# Resize to specific dimensions (maintaining aspect ratio)
ffmpeg -i input.mp4 -vf scale=1280:720 -c:a copy output.mp4
# Resize to specific width, height auto-calculated
ffmpeg -i input.mp4 -vf scale=1280:-1 -c:a copy output.mp4
# Resize to percentage of original
ffmpeg -i input.mp4 -vf scale=iw*0.5:ih*0.5 -c:a copy output.mp4
Video Compression
Optimize your videos for web distribution with better compression:
# H.264 compression with controlled quality (lower CRF = higher quality, 18-28 is typical)
ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
# H.265/HEVC compression (better compression but slower)
ffmpeg -i input.mp4 -c:v libx265 -crf 28 -preset medium -c:a aac -b:a 128k output.mp4
# VP9 compression (for WebM)
ffmpeg -i input.mp4 -c:v libvpx-vp9 -b:v 0 -crf 30 -c:a libopus output.webm
Working with GIFs
FFmpeg is excellent for creating and optimizing GIFs:
# Create a GIF from a video
ffmpeg -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" output.gif
# Convert a GIF to MP4 (much smaller file size)
ffmpeg -i input.gif -c:v libx264 -pix_fmt yuv420p -movflags +faststart output.mp4
# Create a high-quality GIF with better color palette
ffmpeg -i input.mp4 -vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" output.gif
Hardware Acceleration
Modern GPUs can significantly speed up video encoding and decoding:
NVIDIA (using NVENC)
# H.264 encoding with NVIDIA GPU
ffmpeg -i input.mp4 -c:v h264_nvenc -preset slow -b:v 5M -c:a copy output.mp4
# Hardware-accelerated decoding and encoding
ffmpeg -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:v h264_nvenc -preset slow -b:v 5M -c:a copy output.mp4
Intel Quick Sync
# H.264 encoding with Intel GPU
ffmpeg -i input.mp4 -c:v h264_qsv -preset medium -b:v 5M -c:a copy output.mp4
AMD (using AMF)
# H.264 encoding with AMD GPU
ffmpeg -i input.mp4 -c:v h264_amf -quality balanced -c:a copy output.mp4
Video Filters
FFmpeg provides a powerful filtering system to modify videos:
# Crop video (width:height:x:y)
ffmpeg -i input.mp4 -vf "crop=1280:720:0:0" output.mp4
# Rotate video (90 degrees clockwise)
ffmpeg -i input.mp4 -vf "transpose=1" output.mp4
# Adjust brightness, contrast, saturation
ffmpeg -i input.mp4 -vf "eq=brightness=0.1:contrast=1.1:saturation=1.5" output.mp4
# Add a text watermark
ffmpeg -i input.mp4 -vf "drawtext=text='Copyright 2025':fontcolor=white:fontsize=24:x=10:y=10" output.mp4
# Apply a blur effect
ffmpeg -i input.mp4 -vf "boxblur=5:1" output.mp4
Audio Processing
FFmpeg can handle various audio operations:
# Change volume
ffmpeg -i input.mp4 -filter:a "volume=1.5" output.mp4
# Normalize audio
ffmpeg -i input.mp4 -filter:a "loudnorm" output.mp4
# Change audio speed without changing pitch
ffmpeg -i input.mp3 -filter:a "atempo=1.5" output.mp3
# Extract audio from video
ffmpeg -i input.mp4 -vn -acodec copy output.aac
# Add audio to video
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -map 0:v:0 -map 1:a:0 output.mp4
Streaming
FFmpeg supports various streaming protocols including RTMP, HLS, and DASH. I built a raspberry pi that streamed church services every week for over three years using FFmpeg and RTMP…
RTMP Streaming
# Stream to an RTMP server (like YouTube, Twitch)
ffmpeg -i input.mp4 -c:v libx264 -preset veryfast -b:v 3000k -c:a aac -b:a 128k -f flv rtmp://server/live/stream_key
HLS (HTTP Live Streaming)
# Create HLS segments for adaptive streaming
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -b:v 1500k -b:a 128k \
-f hls -hls_time 4 -hls_playlist_type vod -hls_segment_filename "segment_%03d.ts" playlist.m3u8
DASH (Dynamic Adaptive Streaming over HTTP)
# Create DASH manifest and segments
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -b:v 1500k -b:a 128k \
-f dash -seg_duration 4 -use_template 1 -use_timeline 1 manifest.mpd
Live Capture
Capture from your computer's webcam, microphone, or screen:
# Webcam capture (record from webcam to MP4)
ffmpeg -f dshow -i video="Webcam Name" -c:v libx264 output.mp4
# Screen recording (Windows)
ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 screen_capture.mp4
# Screen recording (macOS)
ffmpeg -f avfoundation -i "1" -c:v libx264 screen_capture.mp4
# Screen recording (Linux)
ffmpeg -f x11grab -framerate 30 -i :0.0 -c:v libx264 screen_capture.mp4
Advanced Features
FFmpeg includes several command-line tools beyond just ffmpeg
:
ffprobe: Analyze media files and display detailed information
ffprobe -v error -show_format -show_streams input.mp4
ffplay: Simple media player for quick testing
ffplay input.mp4
ffmpeg-normalize: Normalize audio levels across multiple files
ffmpeg-normalize input.mp4 -o output.mp4
Complex Examples
Creating a Video Thumbnail Grid
ffmpeg -i input.mp4 -vf "select=not(mod(n\,300)),scale=320:180,tile=3x3" -frames:v 1 thumbnail_grid.jpg
Adding Subtitles to a Video
ffmpeg -i video.mp4 -i subtitles.srt -c:v copy -c:a copy -c:s mov_text output.mp4
Creating a Time-lapse from Images
ffmpeg -framerate 30 -pattern_type glob -i "img_*.jpg" -c:v libx264 -pix_fmt yuv420p timelapse.mp4
Multi-bitrate Adaptive Streaming
ffmpeg -i input.mp4 \
-map 0:v -map 0:a -map 0:v -map 0:a -map 0:v -map 0:a \
-c:v libx264 -crf 23 -preset medium \
-c:a aac -ar 48000 -b:a 128k \
-filter:v:0 scale=w=1280:h=720 \
-filter:v:1 scale=w=640:h=360 \
-filter:v:2 scale=w=320:h=180 \
-var_stream_map "v:0,a:0 v:1,a:1 v:2,a:2" \
-f hls -hls_time 4 -hls_playlist_type vod \
-master_pl_name master.m3u8 \
-hls_segment_filename stream_%v/segment_%03d.ts \
stream_%v.m3u8
FFmpeg stands as an indispensable tool for developers, content creators, and multimedia enthusiasts. Its command-line interface may seem intimidating at first, but the flexibility and power it offers are unmatched. Whether you need to perform simple format conversions, apply complex filters, or set up professional streaming workflows, FFmpeg has you covered with its comprehensive feature set.
If you're working with multimedia content in any capacity, investing time in mastering FFmpeg will pay significant dividends in your workflow efficiency and capabilities. Experiment with the examples provided here and explore the official documentation to unlock the full potential of this remarkable tool.
Thanks for reading. Please like and share! Let me know of other topics that interests you.