Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge videos and images using ffmpeg

Tags:

ffmpeg

I'm trying to compile one .webm file that contains this:

  1. 10 seconds showing image1.jpg
  2. Show a movie (an .mp4 file), which lasts about 20 seconds
  3. 10 seconds showing image2.jpg
  4. 10 seconds showing image3.jpg

I was unable to find out how/if the concatenate functionality of ffmpeg could do such a thing. Any clues?

like image 234
Marcel Emblazoned Avatar asked May 13 '17 22:05

Marcel Emblazoned


Video Answer


1 Answers

You can use the concat filter.

Without audio

ffmpeg \
-loop 1 -framerate 24 -t 10 -i image1.jpg \
-i video.mp4 \
-loop 1 -framerate 24 -t 10 -i image2.jpg \
-loop 1 -framerate 24 -t 10 -i image3.jpg \
-filter_complex "[0][1][2][3]concat=n=4:v=1:a=0" out.mp4
  • Match -framerate with frame rate from video.mp4.

With audio

If there is audio in video.mp4 you'll need to provide audio for the images as well for it to be able to concatenate. Example of generating silence:

ffmpeg \
-loop 1 -framerate 24 -t 10 -i image1.jpg \
-i video.mp4 \
-loop 1 -framerate 24 -t 10 -i image2.jpg \
-loop 1 -framerate 24 -t 10 -i image3.jpg \
-f lavfi -t 0.1 -i anullsrc=channel_layout=stereo:sample_rate=44100 \
-filter_complex "[0:v][4:a][1:v][1:a][2:v][4:a][3:v][4:a]concat=n=4:v=1:a=1" out.mp4
  • Match channel_layout with audio channel layout (stereo, mono, 5.1, etc) from video.mp4.
  • Match sample_rate with audio sample rate from video.mp4.
  • No need to match the -t duration from anullsrc with any associated video input: the concat filter will automatically pad it to match video duration.
like image 83
llogan Avatar answered Oct 19 '22 03:10

llogan