Master the art of video processing with FFmpeg: from basic operations to complex video looping, GPU acceleration, and automated video creation pipelines.
Table of Contents
- Introduction
- Getting Started with FFmpeg
- Basic Video Operations
- Advanced Video Filtering
- GPU Acceleration on Mac Silicon
- Creating Looped Videos
- Video Processing Pipeline
- Real-World Examples
- Best Practices
- Troubleshooting
Introduction
FFmpeg is the Swiss Army knife of video processing – a powerful, open-source multimedia framework that can handle virtually any video or audio format. Whether you’re creating YouTube content, processing surveillance footage, or building automated video pipelines, FFmpeg provides the tools you need to manipulate media files with precision and efficiency.
In this comprehensive tutorial, we’ll explore advanced FFmpeg techniques used in real-world video production pipelines, including GPU acceleration, complex filtering, and automated video creation workflows.
Getting Started with FFmpeg
Installation on macOS
For Mac users, especially those with Apple Silicon (M1/M2/M3), FFmpeg can be installed via Homebrew:
# Install FFmpeg with hardware acceleration support
brew install ffmpeg
# Verify installation and check available encoders
ffmpeg -encoders | grep videotoolbox
Basic Command Structure
FFmpeg commands follow this general pattern:
ffmpeg [global options] -i input_file [input options] [output options] output_file
Basic Video Operations
1. Video Format Conversion
# Convert MP4 to AVI
ffmpeg -i input.mp4 output.avi
# Convert with specific codec
ffmpeg -i input.mp4 -c:v libx264 -c:a aac output.mp4
2. Video Resizing
# Resize to specific dimensions
ffmpeg -i input.mp4 -vf scale=1920:1080 output.mp4
# Resize maintaining aspect ratio
ffmpeg -i input.mp4 -vf scale=1920:-1 output.mp4
# Resize with specific resolution presets
ffmpeg -i input.mp4 -vf scale=1280:720 output_hd.mp4 # HD
ffmpeg -i input.mp4 -vf scale=1920:1080 output_fhd.mp4 # Full HD
ffmpeg -i input.mp4 -vf scale=640:480 output_sd.mp4 # SD
3. Removing Audio from Video
# Remove audio track
ffmpeg -i input.mp4 -c:v copy -an output_no_audio.mp4
# Extract audio only
ffmpeg -i input.mp4 -vn -c:a copy output_audio.aac
4. Trimming Videos
# Trim to first 4 seconds
ffmpeg -i input.mp4 -t 4 output_trimmed.mp4
# Trim from 10 seconds to 20 seconds
ffmpeg -i input.mp4 -ss 10 -t 10 output_trimmed.mp4
# Trim from specific time to end
ffmpeg -i input.mp4 -ss 00:01:30 output_from_1min30sec.mp4
Advanced Video Filtering
1. Complex Filter Graphs
FFmpeg’s filter_complex
allows you to chain multiple operations:
# Scale and reverse video
ffmpeg -i input.mp4 -filter_complex "[0:v]scale=1920:1080,reverse[v]" -map "[v]" output.mp4
2. Video Splitting and Concatenation
# Split video into two streams, reverse one, then concatenate
ffmpeg -i input.mp4 -filter_complex "
[0:v]split=2[v1][v2];
[v2]reverse[v2_rev];
[v1][v2_rev]concat=n=2:v=1:a=0[v_cycle]
" -map "[v_cycle]" output_forward_reverse.mp4
3. Logo Overlay
# Add logo to bottom-left corner
ffmpeg -i video.mp4 -i logo.png -filter_complex "
[0:v][1:v]overlay=10:H-h-10
" output_with_logo.mp4
GPU Acceleration on Mac Silicon
One of the most significant advantages of modern Macs is the built-in hardware acceleration. Using GPU acceleration can provide up to 8.5x faster encoding compared to CPU-only processing.
Enabling Hardware Acceleration
# Use Apple's hardware encoder for H.264
ffmpeg -i input.mp4 -c:v h264_videotoolbox -c:a aac output.mp4
# Use hardware encoder for HEVC/H.265
ffmpeg -i input.mp4 -c:v hevc_videotoolbox -c:a aac output.mp4
# With quality settings
ffmpeg -i input.mp4 -c:v h264_videotoolbox -q:v 50 -c:a aac output.mp4
Performance Comparison
Method | Encoding Speed | CPU Usage | Quality |
---|---|---|---|
Software (libx264) | 1x | High | Excellent |
Hardware (videotoolbox) | 8.5x | Low | Good |
Creating Looped Videos
Creating seamless looped videos is essential for background content, music videos, and ambient videos. Here are several approaches:
1. Simple Video Looping
# Loop video 5 times
ffmpeg -stream_loop 5 -i input.mp4 -c copy output_looped.mp4
# Infinite loop (until audio ends)
ffmpeg -stream_loop -1 -i input.mp4 -i audio.mp3 -c copy -shortest output.mp4
2. Forward-Reverse Looping
This creates a seamless loop by playing the video forward, then in reverse:
# Method 1: Single command with complex filter
ffmpeg -i input.mp4 -filter_complex "
[0:v]split=2[v1][v2];
[v2]reverse[v2_rev];
[v1][v2_rev]concat=n=2:v=1:a=0[v_cycle];
[v_cycle]loop=loop=-1:size=1:start=0[v_looped]
" -map "[v_looped]" output_seamless_loop.mp4
3. Two-Step Approach (More Reliable)
# Step 1: Create forward-reverse cycle
ffmpeg -i input.mp4 -filter_complex "
[0:v]split=2[v1][v2];
[v2]reverse[v2_rev];
[v1][v2_rev]concat=n=2:v=1:a=0[v_cycle]
" -map "[v_cycle]" -t 16 video_cycle.mp4
# Step 2: Loop the cycle to match audio duration
ffmpeg -stream_loop -1 -i video_cycle.mp4 -i audio.mp3 \
-c:v h264_videotoolbox -c:a aac -shortest output.mp4
# Clean up
rm video_cycle.mp4
Video Processing Pipeline
Automated Video Creation Workflow
Here’s a complete pipeline for creating professional videos:
#!/bin/bash
# Complete video processing pipeline
# Step 1: Prepare video (remove sound, resize, add logo)
ffmpeg -y -i input.mp4 \
-vf "scale=1920:1080,overlay=10:H-h-10:enable="between(t,0,inf)"" \
-i logo.png \
-c:v h264_videotoolbox -an \
video_prepared.mp4
# Step 2: Create seamless loop
ffmpeg -y -i video_prepared.mp4 \
-filter_complex "
[0:v]split=2[v1][v2];
[v2]reverse[v2_rev];
[v1][v2_rev]concat=n=2:v=1:a=0[v_cycle]
" \
-map "[v_cycle]" -t 16 video_cycle.mp4
# Step 3: Combine with audio
ffmpeg -y -stream_loop -1 -i video_cycle.mp4 \
-i concatenated_audio.mp3 \
-c:v h264_videotoolbox -c:a aac -shortest \
final_output.mp4
# Cleanup
rm video_cycle.mp4
Makefile Integration
# Makefile for automated video processing
OUTPUT_NAME ?= my-video
VIDEO_RESOLUTION ?= HD
all: $(OUTPUT_NAME).mp4
$(OUTPUT_NAME).mp4: video_prepared.mp4 audio_files/
@echo "Creating final video..."
@./combine_video_audio.sh --output-name $(OUTPUT_NAME)
video_prepared.mp4: input.mp4
@echo "Preparing video..."
@./prepare_video.sh --resolution $(VIDEO_RESOLUTION)
clean:
@rm -f *.mp4 *.mp3
@echo "Cleanup completed"
Real-World Examples
Example 1: Waterfall Background Video
# Create a 2.5-hour waterfall video with music
ffmpeg -y \
-i "Ban_Gioc_Waterfall.mp4" \
-i "concatenated_audio.mp3" \
-filter_complex "
[0:v]scale=1920:1080,setpts=PTS-STARTPTS[video_scaled];
[video_scaled]split=2[v1][v2];
[v2]reverse[v2_rev];
[v1][v2_rev]concat=n=2:v=1:a=0[v_cycle];
[v_cycle]loop=loop=-1:size=1:start=0[v_looped]
" \
-map "[v_looped]" \
-map 1:a \
-c:v h264_videotoolbox \
-c:a aac \
-shortest \
-r 24 \
"waterfall_music_video.mp4"
Example 2: Batch Video Processing
#!/bin/bash
# Process multiple videos with GPU acceleration
for video in *.mp4; do
echo "Processing $video..."
ffmpeg -y -i "$video" \
-vf "scale=1920:1080,crop=1920:1080:0:0" \
-c:v h264_videotoolbox \
-c:a aac \
-t 4 \
"${video%.*}_processed.mp4"
done
Example 3: YouTube-Optimized Video
# Create YouTube-optimized video with chapters
ffmpeg -y \
-i video.mp4 \
-i audio.mp3 \
-c:v h264_videotoolbox \
-c:a aac \
-b:a 128k \
-movflags +faststart \
-metadata title="My Video Title" \
-metadata description="Video description" \
youtube_ready.mp4
Best Practices
1. Performance Optimization
-
Always use GPU acceleration when available (
h264_videotoolbox
on Mac) -
Use appropriate quality settings (
q:v 50
for good quality/size balance) - Process in chunks for very large files
-
Use
shortest
when combining video and audio of different lengths
2. Quality Settings
# High quality (larger file)
ffmpeg -i input.mp4 -c:v h264_videotoolbox -q:v 20 output.mp4
# Balanced quality/size
ffmpeg -i input.mp4 -c:v h264_videotoolbox -q:v 50 output.mp4
# Smaller file size
ffmpeg -i input.mp4 -c:v h264_videotoolbox -q:v 60 output.mp4
3. Error Handling
# Always use -y to overwrite output files
ffmpeg -y -i input.mp4 output.mp4
# Check for errors in scripts
if [ $? -ne 0 ]; then
echo "FFmpeg failed!"
exit 1
fi
4. Memory Management
# For large files, process in segments
ffmpeg -i input.mp4 -ss 00:00:00 -t 00:05:00 -c copy segment1.mp4
ffmpeg -i input.mp4 -ss 00:05:00 -t 00:05:00 -c copy segment2.mp4
Troubleshooting
Common Issues and Solutions
-
“No such file or directory”
- Check file paths and permissions
- Ensure input files exist
-
“Codec not found”
- Install FFmpeg with full codec support
- Use
brew install ffmpeg
on macOS
-
High CPU usage
- Enable GPU acceleration with
h264_videotoolbox
- Use
preset fast
for faster encoding
- Enable GPU acceleration with
-
Audio/video sync issues
- Use
shortest
flag when combining streams - Check frame rates with
r
parameter
- Use
-
Out of memory errors
- Process files in smaller chunks
- Use
threads
parameter to limit CPU usage
Debugging Commands
# Check video information
ffprobe -v quiet -print_format json -show_format -show_streams input.mp4
# Test encoding without output
ffmpeg -f lavfi -i testsrc=duration=1:size=1920x1080:rate=30 -f null -
# Monitor system resources
top -pid $(pgrep ffmpeg)
Conclusion
FFmpeg is an incredibly powerful tool for video processing, and with the right techniques, you can create professional-quality videos efficiently. The key is to:
- Leverage hardware acceleration whenever possible
- Use complex filters for advanced effects
- Automate repetitive tasks with scripts and Makefiles
- Optimize for your target platform (YouTube, social media, etc.)
Whether you’re creating ambient videos, processing surveillance footage, or building automated video pipelines, FFmpeg provides the flexibility and power you need to achieve professional results.
Additional Resources
This tutorial is based on real-world video processing pipelines used in production environments. The examples and techniques shown here have been tested and optimized for performance and quality.