9

Let's say I have a video, which can have any dimensions or codec (e.g. mp4), and I want to add 3 seconds of black frames to its head.

I have seen other variants to this question which pertain to adding a single frame, and also answers which specify the width and height of the video. However the video can be of any width and height and I don't want to change those values. Just add the black. In a separate command, I'd also like to add the seconds of black to the tail.

There should be no audio in the black section - the black should just be concatenated to the front and end. Also, I'd like to avoid re-encoding the video if possible, and copy the result to a new file.

Any help would be appreciated.

  • Is audio present? And do you want to skip re-encoding the main video? – Gyan Feb 18 '17 at 12:30
  • @Mulvya thanks for your input. I've updated my question. – Matt C Feb 18 '17 at 12:49
  • @MattC You can delete the above comment now. This will need multiple commands, unless you re-encode. Is that ok? – Gyan Feb 18 '17 at 14:56
  • @Mulvya yes multiple commands is good. Thanks again. – Matt C Feb 18 '17 at 15:03
12

The basic method to join videos without re-encoding is to use the concat demuxer. This device requires that the files being joined share certain properties, like codec, framerate, video dimensions, timebase..etc

First step is to create the blank segment, which may be used both at the head and tail.

ffmpeg -i fullvideo.mp4 -vf trim=0:3,geq=0:128:128 -af atrim=0:3,volume=0 -video_track_timescale 600 3sec.mp4

Have to do one bookkeeping step for the full video. If not done, the combined video will likely run at the wrong speed.

ffmpeg -i fullvideo.mp4 -c copy -video_track_timescale 600 full600.mp4

Next, create a text file.

file 3sec.mp4
file full600.mp4
file 3sec.mp4

Then run

ffmpeg -f concat -i list.txt -c copy merged.mp4

My first step assumes that the video stream has the most common pixel format yuv420p. Which should be fine as this is the only pixel format supported by web players and smartphone cameras, consumer camcorders..etc. If it's not, the geq filter may fail.

I also assume, tacitly, that the full video does not use more reference frames than produced by preset medium of x264. It's a safe assumption for 95% of videos.

Finally, I assume the full video has AAC-LC as its audio codec. Expected to be true of most phone/cam/web videos.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.