ed22
ed22

Reputation: 1237

How to pad video with black frames to a given length with FFmpeg?

I am aware that tpad filter has the stop_duration parameter that adds frames to the end of video. How do I make sure that the resulting video is say 5 seconds in length when the input length is unknown (but less than 5s)?

Upvotes: 0

Views: 1279

Answers (2)

user3083171
user3083171

Reputation: 59

The right way would be to first make a padding video with the same specs, then concatenate the video to your video, and finally to cut the video to the desired length. Note that all steps can be done with or without reencoding.

It is the case that to trim from the right, even without reencoding is precise (up to whatever frame you want). So the best would be to concat and trim without reencoding.

Trimming:

ffmpeg -i input_file -c copy -ss start_time -to end_time output_file

Concat (python):

import subprocess

video1 = 'path/to/your/video1.mp4'
video2 = 'path/to/your/video2.mp4'
output_file = 'output_concatenated.mp4'

with open('input.txt', 'w') as f:
    f.write(f"file '{video1}'\n")
    f.write(f"file '{video2}'\n")

subprocess.run(
    ['ffmpeg', '-f', 'concat', '-safe', '0', '-i', 'input.txt', '-vcodec', 'copy',
     '-acodec', 'copy', "-strict", "experimental", output_file]
)

Black video:

ffmpeg -f lavfi -i color=c=black:s=1920x1080:r=30:d=10 -c:v libx264 -preset ultrafast -tune stillimage -crf 18 -vf "fps=30" output_blank.mp4

Should work :)

PS: you could also use opencv do reencode your video (but then no sound) and add blank frames at the end.

Upvotes: 1

Gyan
Gyan

Reputation: 93018

Add a trim filter to limit total duration, e.g.

ffmpeg -i in -vf tpad=stop=-1,trim=end=5 out

Upvotes: 1

Related Questions