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.

image

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 ……

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.76.100
  Duration: 00:00:10.00, start: 0.000000, bitrate: 28144 kb/s
  Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, bt470bg/unknown/unknown), 2816x2160 [SAR 1:1 DAR 176:135], 28141 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]

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.

1
ffmpeg -r 10 -f image2 -i %d.jpeg output1.mp4

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).

1
ffmpeg -r 10 -f image2 -i %d.jpeg -b:v 4M output2.mp4

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.

1
ffmpeg -r 10 -f image2 -i %d.jpeg output3.mp4

-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.

1
ffmpeg -f image2 -i %d.jpeg -c:v libx265 output4.mp4

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.

1
ffmpeg -f image2 -i %d.jpeg -c:v libvpx output-v8.webm #注意webm默认生成的是低质量的视频,可使用-crf或者-b:v参数调整视频质量。

-vf scale adjusts the video resolution

-vf scale: Video Filter Scale

1
ffmpeg -f image2 -i %d.jpeg -s 640x480 output5.mp4

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

1
ffmpeg -f image2 -i %d.jpeg -vf scale=-1:480 output5.mp4 #-1表示比例缩放,也可-vf scale=640:-1固定宽度缩放高度

These are a few commonly used parameters, these parameters are not only limited to pictures to video, video to video can also be used.