Wednesday, 6 March 2019

terminal - Create video from images and audio with varying image durations using FFMPEG?


What I want to do:


I've x amount of images png/jpg, and I want to show them for varying amount of duration, some longer, and some shorter amount of time.



  • a1.jpg 1st photo stays for 3 seconds

  • a2.png stays for 10 seconds

  • a3.jpg stays till end of audio


Video should be as long as audio.mp3


this is my code so far after reading documentation, not sure what's wrong, I get corrupt video with no sound and only shows last image.


Also would like to center images if possible.


ffmpeg -y \
-loop 1 -t 1 -i a1.jpg \
-loop 1 -t 1 -i a2.jpg \
-loop 1 -t 4 -i a3.jpg \
-loop 1 -t 1 -i a4.png \
-loop 1 -t 1 -i a5.png \
-i audio.mp3 \
-c:v libx264 -pix_fmt yuv420p -c:a copy video.mp4

try


ffmpeg -y -framerate 15 \
-loop 1 -t 1 -i a1.jpg \
-loop 1 -t 1 -i a2.jpg \
-loop 1 -t 4 -i a3.jpg \
-loop 1 -t 1 -i a4.png \
-loop 1 -t 1 -i a5.png \
-i audio.mp3 \
-c:v libx264 -pix_fmt yuv420p -c:a copy \
-filter_complex " \
[0]setdar=16/9[a1],[a1]scale=720x406[a];[1]setdar=16/9[b1],[b1]scale=720x406[b];[2]setdar=16/9[c1],[c1]scale=720x406[c];[3]setdar=16/9[d1],[d1]scale=720x406[d];[4]setdar=16/9[e1],[e1]scale=720x406[e];
[a][b][c][d][e]concat=n=5,format=yuv420p[v],[v]scale=720x406[v1],[v1]setdar=16/9[v2]" \
-map "[v2]" -c:a copy out.mp4

Answer



Basic template is


ffmpeg -y \
-loop 1 -t 1 -i a1.jpg \
-loop 1 -t 1 -i a2.jpg \
-loop 1 -t 4 -i a3.jpg \
-loop 1 -t 1 -i a4.png \
-loop 1 -i a5.png \
-i audio.mp3 \
-filter_complex "concat=n=5" -shortest \
-c:v libx264 -pix_fmt yuv420p -c:a aac video.mp4

The t specification for each image sets its duration. The t for the last image should be omitted if you want the video to last till the audio ends.


I use the concat filter to join all video inputs. Good practice is to label which inputs the concat filter will join, but if unspecified, the filter will grab the first N -i inputs, where N is the value set in the concat filter. All images must have the same resolution and aspect ratio.


Your given command copies the audio but MP3, although allowed, isn't standard in MP4, and some players, like Quicktime, will not play the file, so I've changed it to AAC.




For images of unequal sizes, change filter_complex to


"[0]scale=W:H:force_original_aspect_ratio=decrease,pad=W:H:(ow-iw)/2:(oh-ih)/2,setsar=1[i0];
[1]scale=W:H:force_original_aspect_ratio=decrease,pad=W:H:(ow-iw)/2:(oh-ih)/2,setsar=1[i1];
[2]scale=W:H:force_original_aspect_ratio=decrease,pad=W:H:(ow-iw)/2:(oh-ih)/2,setsar=1[i2];
[3]scale=W:H:force_original_aspect_ratio=decrease,pad=W:H:(ow-iw)/2:(oh-ih)/2,setsar=1[i3];
[4]scale=W:H:force_original_aspect_ratio=decrease,pad=W:H:(ow-iw)/2:(oh-ih)/2,setsar=1[i4];
[i0][i1][i2][i3][i4]concat=n=5"

W and H should be replaced with numerical values of target video width and height.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...