Introduction to common parameters for ffmpeg to synthesize pictures into videos

FFmpeg series article directory



Preface

Use ffmpeg to synthesize pictures into videos, and introduce some common command parameters.


Use ffmpeg to synthesize pictures into videos

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

Before using ffmpeg to splice pictures into a video, you need to preprocess the picture file names. There must be numbers in the file names to mark their order. Here I directly use the file name + timestamp to rename the pictures, as follows: directly use the
Insert image description herecommand ffmpeg -f image2 -i %d.jpeg output.mp4 can convert it to mp4 video. The %d in the command is a numerical placeholder. ffmpeg will load *.jpeg as input in order. We did not specify any other parameters here, so ffmpeg used the default parameters, such as the frame rate is 25fps, the video is encoded with h264, and the resolution directly uses the original resolution of the image...

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 suit 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, which means 25 pictures will be spliced ​​in 1 second. We can control the duration 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 a 25-second video.
Here you need to pay attention to the position of the -r 10 parameter. The effect before -i %d.jpeg is different from that after -i %d.jpeg. Putting it after -i will only change the output video frame rate, but the input is still the default value of 25, such as ffmpeg -f image2 -i %d.jpeg -r 10 output1.mp4, 250 pictures will still only generate a 10s video , but the video playback rate will be reduced to 10.

-b:v adjust video bitrate

-b:v bitrate of video. If the original image is larger, the video size generated by the default parameters will be larger. For example, the pictures I used above are all 2k high-definition pictures, and the final generated 10s video is 35MB, with a bit rate of nearly 30Mb/s (the bit rate is the amount of data broadcast in only 1s, please note that the unit here is small b).

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

An additional reminder here is that changing the bit rate will affect the video definition, but it does not mean that a high bit rate video must have higher definition than a low bit rate video. This also depends on the video encoding format. For example, h265 encoding can be used A smaller bit rate produces the same video quality as h264, and encodings such as av1, v8, v9 are also better than h264.

-crf adjust video quality

-crf Constant Rate Factor, 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 will be lost and the video quality will be worse. 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 video encoding format

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

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

Compared with the output.mp4 generated above, the video file size of output4.mp4 is reduced by 60%, but the video quality remains unchanged. 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 videos by default. You can use the -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 a resolution of 640x480. If the original image is not 4:3, the original image will definitely be stretched. You can use the following command to scale proportionally

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

The above are a few commonly used parameters. These parameters are not only limited to converting pictures to videos, but can also be used when converting videos to videos.


Guess you like

Origin blog.csdn.net/zyq880625/article/details/129554078