Splicing pictures into video using ffmpeg

This paper introduces how to use ffmpeg to splice a large number of pictures into a video, and introduces the meaning of some parameters.

Before using ffmpeg to splice pictures into video, you need to preprocess the picture file name. There must be a number in the file name to mark its order. Here I directly rename the pictures with 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 number placeholder. Ffmpeg will load 1-250.jpeg as input in order. Here we do not specify how to other parameters, so ffmpeg uses the default parameters, such as the frame rate is 25fps, the video uses h264 coding, and the resolution directly uses the original resolution of the picture

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
    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)
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]

We can adjust its parameters to generate videos that better meet our needs. Here are some common parameters.

-r adjust frame rate

If the frame rate is not specified, ffmpeg will use the default 25 frames, that is, splicing 25 pictures in one second. We can control the length of the final generated video by adjusting the frame rate.

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

The above command will splice 10 pictures per second, and 250 pictures will eventually generate 25 seconds of video.
Note that the position of the - r 10 parameter has different effects before and after - i% d.jpeg. Placing after - i will only change the output video frame rate, but the default value is 25. For example, ffmpeg - f image2 - i% d.jpeg - r 10 output1.mp4250 pictures will still generate 10s video, but the video playback rate will be reduced to 10.

-b:v adjust video bit rate

-b:v bitrate of video. If the original picture is large, the video size generated by the default parameter will be large. For example, the pictures I used in the above are 2k high-definition pictures. The resulting 10s video has 35MB and the code rate is nearly 30Mb/s (the code rate is the amount of data broadcast in 1s. Note that the unit here is small b).

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

As an additional reminder, changing the bit rate will affect the video definition, but it does not mean that the video with high bit rate must have higher definition than that with low bit rate, which also depends on the video coding format. For example, h265 coding can generate h264 with the same video quality with a smaller bit rate, and coding such as av1, v8 and v9 is also better than h264.

-crf adjust video quality

-crf Constant Rate Factor is a parameter used to balance video quality and file size. The value range in ffmpeg is 0-51. The higher the value, the more content loss and worse video quality. The default value of ffmpeg is 23, and the recommended value range is 17-28.

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

-c:v adjust the encoding format of the video

-c:v codec of video. At present, ffmpeg uses h264 by default for mp4. You can use - c:v libx265 to generate h265 video with the same quality but smaller files.

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

output4.mp4 reduces the video file size by 60% 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 v8 and v9 encoded webm files, respectively.

ffmpeg -f image2 -i %d.jpeg -c:v libvpx output-v8.webm #Note that webm generates low-quality video by default. You can use - crf or - b:v parameters to adjust the video quality.

-vf scale adjust video resolution

-vf scale: Video Filter Scale

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

The above command will directly adjust the video to the resolution of 640x480. If the original image is not 4:3, it will stretch the original image. You can use the following command to scale equally

ffmpeg -f image2 -i %d.jpeg -vf scale=-1:480 output5.mp4 #-1 indicates scaling, or - vf scale=640:-1 fixed width scaling height

The above are several common parameters. These parameters are not limited to picture to video, but can also be used when video to video.

Tags: Java

Posted on Sun, 21 Nov 2021 16:45:24 -0500 by praveenbabur