This article describes how to use ffmpeg to stitch a large number of images into a video, and introduces the meaning of some of the parameters.
Before using ffmpeg to stitch images into a video, you need to pre-process the image file name, the file name must have a number to mark out its order, here I directly renamed the image using numbers, as follows.
Directly use the command ffmpeg -f image2 -i %d.jpeg output.mp4
to convert it to mp4 video, the %d in the command is a numbered placeholder, ffmpeg will load 1-250.jpeg in order as input. Here we don’t specify how to use other parameters, so ffmpeg uses the default parameters, for example, the frame rate is 25fps, the video is encoded in h264, and the resolution is directly the original resolution of the picture ……
|
|
We can adjust its parameters to generate videos that better meet our needs, here are a few common parameters.
-r Adjust frame rate
If we don’t specify the frame rate, ffmpeg will use the default 25 fps, which is 25 images stitched in 1 second, we can control the final length of the generated video by adjusting the frame rate.
|
|
The command as above will stitch 10 images per second, 250 images will eventually generate 25 seconds of video.
**Note the position of the -r 10
parameter here, the effect is different before and after -i %d.jpeg
. For example, if ffmpeg -f image2 -i %d.jpeg -r 10 output1.mp4
, 250 images will still generate only 10s of video, but the video playback rate will be reduced to 10.
-b:v adjust the bitrate of the video
-b:v bitrate of video. If the original image is large, the default parameter will generate a larger video size. For example, the images I used above are 2k HD images, and the final generated 10s video is 35MB, with a bitrate of nearly 30Mb/s (bitrate is the amount of data broadcast over only 1s, note that the unit here is small b).
|
|
Here is an additional reminder that changing the bitrate will affect the video clarity, but it doesn’t mean that the video with high bitrate is necessarily sharper than the video with low bitrate, it also depends on the video encoding format, for example, h265 encoding can generate the same video quality of h264 with smaller bitrate, and encodings like av1, v8, v9, etc. are also better than h264.
-crf Adjust video quality
-crf Constant Rate Factor, a parameter to balance the video quality and file size, is 0-51 in FFMPEG, the higher the value the more content loss and the worse the video quality. The default value of ffmpeg is 23, the recommended range is 17-28.
|
|
-c:v adjusts the codec of video
-c:v codec of video. ffmpeg currently uses h264 by default for mp4, you can use -c:v libx265
to generate h265 video with the same quality, but smaller files.
|
|
output4.mp4 is 60% smaller in video file size compared to the output.mp4 generated above, but the video quality remains the same. You can also use -c:v libvpx
-c:v libvpx-vp9
to generate webm files with v8 and v9 encoding respectively.
|
|
-vf scale adjusts the video resolution
-vf scale: Video Filter Scale
|
|
The above command will resize the video directly to 640x480 resolution, if the original image is not 4:3 it will definitely stretch the original image. You can use the following command to scale it equally
|
|
These are a few commonly used parameters, these parameters are not only limited to pictures to video, video to video can also be used.