FFmpeg中音频时长更长时为视频追加黑帧的实现方法
Got it, let's tackle this problem. You're using FFmpeg because other editors drop frames, and right now your trimmed video is shorter than the trimmed audio—you want the final output to match the audio's duration by adding black frames to the end of the video.
First, the core issue here is that there's no direct "opposite of -shortest" flag, but we can use FFmpeg's tpad video filter to append black frames to the video until it matches the audio's length. Here's how to do it:
Step 1: Calculate the Duration Difference
First figure out how much extra time you need to add to the video. Your audio is trimmed to 00:09:54.38 (594.38 seconds) and the video to 00:09:51 (591 seconds)—the difference is 3.38 seconds.
Step 2: Modified FFmpeg Command
Use the tpad filter to add black frames to the video's end, then combine it with the trimmed audio:
-i "videoplayback1" -t 00:09:51 -vf "tpad=stop_mode=black:stop_duration=3.38" -c:v libx264 -crf 20 -i "audioplayback1" -t 00:09:54.38 -c:a copy -map 0:v -map 1:a "playback1.mp4"
Let's break down the key parts:
-vf "tpad=stop_mode=black:stop_duration=3.38": Thetpadfilter appends frames to the video.stop_mode=blacktells it to add black frames, andstop_durationsets how long the padding should be.-map 0:v -map 1:a: Explicitly maps the video from the first input and audio from the second input—this avoids any automatic stream selection issues FFmpeg might have.-c:a copy: Copies the audio stream directly without re-encoding, which saves time and preserves quality.
Alternative: Auto-Calculate the Padding Duration (No Manual Math)
If you don't want to calculate the difference manually, you can use ffprobe to get the audio's trimmed duration first, then pass that to FFmpeg to pad the video to match:
- First, get the trimmed audio duration in seconds:
This will output a number like 594.38.
- Then use that value in the FFmpeg command to pad the video to exactly that length:
-i "videoplayback1" -t 00:09:51 -vf "tpad=stop_mode=black:stop_duration=594.38" -c:v libx264 -crf 20 -i "audioplayback1" -t 00:09:54.38 -c:a copy -map 0:v -map 1:a "playback1.mp4"
This ensures the video is extended to match the audio's trimmed length perfectly.
Notes
- Since we're re-encoding the video with
libx264, make sure your FFmpeg build includes this codec (most standard builds do). - The
tpadfilter works seamlessly with H.264 encoding, so the black frames will blend naturally with the rest of the video.
内容的提问来源于stack exchange,提问作者Edward

