6月2日 00:00
How to use FFmpeg for video editing, merging, and screenshots? What are some practical tips?
FFmpeg provides powerful video editing and processing capabilities, allowing precise control over video time ranges, segment extraction, and merging.
Video Editing
Clip by Time Range
bash# Start from 10 seconds, clip 30 seconds ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:30 -c copy output.mp4 # Start from 1 minute 30 seconds, clip to 3 minutes ffmpeg -i input.mp4 -ss 00:01:30 -to 00:03:00 -c copy output.mp4
Precise Clipping Techniques
When using the -ss parameter, placing it before -i enables fast seeking but may not be precise; placing it after -i is more precise but slower.
bash# Fast seeking (may not be precise) ffmpeg -ss 00:00:10 -i input.mp4 -t 00:00:30 -c copy output.mp4 # Precise seeking (slower) ffmpeg -i input.mp4 -ss 00:00:10 -t 00:00:30 -c copy output.mp4
Video Merging
Using concat protocol
bash# Create file list filelist.txt file 'part1.mp4' file 'part2.mp4' file 'part3.mp4' # Merge videos ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4
Using concat filter
bashffmpeg -i part1.mp4 -i part2.mp4 -i part3.mp4 \ -filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1[outv][outa]" \ -map "[outv]" -map "[outa]" output.mp4
Video Screenshots
Single Frame Screenshot
bash# Capture one frame at 5 seconds ffmpeg -i input.mp4 -ss 00:00:05 -vframes 1 screenshot.jpg # Capture first frame ffmpeg -i input.mp4 -vframes 1 first_frame.png
Batch Screenshots
bash# Capture one frame per second ffmpeg -i input.mp4 -vf fps=1 screenshot_%04d.jpg # Capture one frame every 5 seconds ffmpeg -i input.mp4 -vf fps=1/5 screenshot_%04d.jpg
Video Processing
Adjust Resolution
bash# Scale to 1280x720 ffmpeg -i input.mp4 -vf scale=1280:720 output.mp4 # Scale while maintaining aspect ratio ffmpeg -i input.mp4 -vf scale=1280:-1 output.mp4
Rotate Video
bash# Rotate 90 degrees clockwise ffmpeg -i input.mp4 -vf "transpose=1" output.mp4 # Rotate 90 degrees counter-clockwise ffmpeg -i input.mp4 -vf "transpose=2" output.mp4
Add Watermark
bash# Add image watermark ffmpeg -i input.mp4 -i watermark.png -filter_complex "overlay=10:10" output.mp4 # Add text watermark ffmpeg -i input.mp4 -vf "drawtext=text='Watermark':fontcolor=white:fontsize=24:x=10:y=10" output.mp4
Audio/Video Separation and Merging
Extract Audio
bash# Extract audio stream ffmpeg -i video.mp4 -vn -acodec copy audio.aac # Convert audio format ffmpeg -i video.mp4 -vn -acodec libmp3lame -ab 192k audio.mp3
Extract Video
bash# Extract video stream (no audio) ffmpeg -i video.mp4 -an -c:v copy video.mp4
Merge Audio and Video
bash# Merge audio and video ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental output.mp4
When editing and processing videos, using -c copy avoids re-encoding, improving processing speed while maintaining original quality.