eladg / ffmpeg-by-example

Creative Commons Attribution 4.0 International
8 stars 5 forks source link

Loop images at specific frame rate to generate a video - FFmpeg By Example #86

Open eladg opened 2 years ago

eladg commented 2 years ago

https://www.ffmpegbyexample.com/examples/fnjrnx47/loop_images_at_specific_frame_rate_to_generate_video/

Given a set of 9 images (e.g. for example), generate a video that loops at 12 fps for 3 seconds.

Command breakdown:

-i gif/%d.png will instruct ffmpeg to load 1.png, 2.png, ... files from the gif/ folder.

-vf '[in]fps=12[v]; adjust the frame rate of the [in] stream (i.e. the images) to 12 frames per second. Name this stream 'v', to be used as part of the video filter chain. More info on the fps filter here.

[v]loop=loop=-1:size=9:start=0[out]' Take input stream 'v', and loop it. The loop should start on the 0 frames, with a size of 9 frames and it should repeat indefinitely (i.e. -1). You may specify how many times you want the loop to repeat. See more info on the loop filter here.

-c:v libx264 -crf 21 Use the libx264 encoding library with a "Constant Rate Factor" of 21.

-r 30 Although the 'v' stream loops at 12 fps, the generated video stream is at 30 fps. It is very likely that we are repeating say frame 0 a couple of times on the output video. For optimization purposes, it could be wise to set the frame rate to 12, however, it was important for me to show with this example that these 12 variables are independent of each other.

-t 3 Since our loop filter will repeat the same input indefinitely, we want to limit the output stream to 3 seconds. This will break the loop operation once the generated video is 3 seconds.