使用FFmpeg将JPG序列生成动画:消除左右黑边及尺寸适配
Hey there, let's work through your problems with converting JPGs to an AVI animation step by step. I'll break this down into the two main issues you're facing, plus give you clear guidance on resizing your images properly.
1. Getting Rid of the Left/Right Black Bars
Your original images are 1200x900, which has a 4:3 aspect ratio. The black bars appear because FFmpeg is defaulting to a standard resolution (like 16:9) that doesn't match your image's natural ratio. Here are two solid fixes:
Option 1: Match output to your exact image size
Add the-sflag to explicitly set the output resolution in your FFmpeg command:ffmpeg -f image2 -i img%04d.jpg -r 24 -s 1200x900 sound.aviOption 2: Force the correct aspect ratio (more flexible)
Use a video filter to lock the 4:3 ratio, which prevents accidental stretching if you ever adjust sizes later:ffmpeg -f image2 -i img%04d.jpg -r 24 -vf "scale=1200:900,setdar=4/3" sound.avi
Resizing Images with convert (If Needed)
If you want to resize your JPGs first, always keep the 4:3 aspect ratio to avoid future black bars. For example:
- To shrink to 800x600 (still 4:3):
convert img%04d.jpg -resize 800x600 resized_img%04d.jpg - The
-resizeflag in ImageMagick automatically maintains aspect ratio—use800x600^(caret) to fill the exact size, or just800x600to fit within those dimensions without cropping.
2. Fixing the "Past Duration X too large" Warnings
These warnings pop up when FFmpeg struggles to sync input image timestamps with your specified output frame rate. Try either of these quick fixes:
Use variable frame rate sync
The-vsync vfrflag tells FFmpeg to adapt to the input's natural timing, eliminating timestamp mismatches:ffmpeg -f image2 -i img%04d.jpg -r 24 -vsync vfr -s 1200x900 sound.aviGenerate proper presentation timestamps
If the first fix doesn't clear the warnings, add-fflags +genptsto force FFmpeg to create accurate timestamps for each frame:ffmpeg -f image2 -i img%04d.jpg -r 24 -fflags +genpts -s 1200x900 sound.avi
You can even combine both flags for extra safety—they won't conflict with each other.
内容的提问来源于stack exchange,提问作者Clinton Winant

