FFmpeg技术问题:如何将视频从1917×1080 resize并pad至1920×1080?
Hey Jules, let’s figure out why your pad operation isn’t sticking, and address that initial vertical source size concern—it’s definitely playing a role here!
First, Let’s Diagnose the Problem
Your initial video is 640×1136 (vertical), which after rotation, resize, and audio merging ended up at 1917×1080. The 3-pixel width gap you’re trying to pad isn’t being fixed likely because of one of two issues:
- Your filter chain order in previous steps caused unintended dimension locking
- You’re applying the pad as a separate step instead of integrating it with your other video manipulations
Why the Initial Vertical Size Matters
Vertical-to-horizontal conversion needs careful ordering of operations. If you rotated after resizing, or resized without accounting for the aspect ratio shift from rotation, you ended up with that odd 1917×1080 dimension instead of cleanly hitting your target. Fixing the workflow order will eliminate this headache.
The Solution: Combine All Video Operations in One Filter Chain
Instead of splitting rotation, resize, and pad into separate commands, bundle them into a single filter_complex call. This ensures each step passes the correct dimensions to the next. Here’s a tailored command for your use case:
-i your_vertical_source.mp4 -i your_audio_file.mp4 -filter_complex "[0:v]rotate=PI/2:ow=ih:oh=iw[v_rot];[v_rot]scale=1920:1080:force_original_aspect_ratio=decrease[v_scaled];[v_scaled]pad=1920:1080:(ow-iw)/2:(oh-ih)/2:color=black[v_final]" -map "[v_final]" -map 1:a -c:a copy final_output.mp4
Let’s break down each filter step:
- Rotate:
rotate=PI/2:ow=ih:oh=iwconverts your vertical 640×1136 video to horizontal 1136×640, explicitly setting output width/height to match the rotated aspect. - Scale:
scale=1920:1080:force_original_aspect_ratio=decreaseresizes the rotated video to fit within 1920×1080 without stretching—this is why you ended up at 1917×1080 before (it’s the closest proportional fit). - Pad:
pad=1920:1080:(ow-iw)/2:(oh-ih)/2adds equal black bars to the left/right to fill the 3-pixel gap, centering your video perfectly.
If You Still Want to Use Your Existing Intermediate File
If you don’t want to reprocess from scratch, verify your intermediate video’s actual dimensions first with:
-i your_1917x1080_video.mp4
Then apply the pad directly with this simple command (make sure there are no conflicting filters from previous steps):
-i your_1917x1080_video.mp4 -vf pad=1920:1080:(1920-iw)/2:(1080-ih)/2:color=black -c:a copy padded_output.mp4
Key Takeaway
Your initial vertical source size was indeed a contributing factor—rotating after resizing can throw off aspect ratio calculations. By handling rotation → scaling → padding in one continuous filter chain, you avoid dimension discrepancies and ensure your final video hits the exact 1920×1080 resolution you need.
内容的提问来源于stack exchange,提问作者Jules

