One/multiple pictures of ffmpeg to synthesize video

ffmpeg  synthesizes a picture into a video and sets the video duration

(1) execute the code,

Full code:

import subprocess
import os
 
# 把一张图片合成视频,设置视频时长
cmdLine = "ffmpeg -r 25 -loop 1 -i images/img1.png -pix_fmt yuv420p -vcodec libx264 -b:v 600k -r:v 25 -preset medium -crf 30 -s 720x576 -vframes 250 -r 25 -t 10 a.mp4"
subprocess.call(cmdLine, shell=True)

(2) Running results:

Combining ffmpeg and opencv to synthesize video from multiple pictures

Note: It is necessary to pay attention to the selection of parameters and image size

img_root: is the path where the jpg image is stored

out_root: is the save path of avi video

string = img_root + 'img' + str(im_name) + '.jpg': It is the storage path of the image. Here my pictures are under the images folder,

  • fps: is the frame rate, which will directly cause the number of frames and duration of the video to be different
  • parameter

(1) You can modify some variables and parameters in the code according to your own needs,

(2) execute the code,

My full code:

import cv2
from cv2 import VideoWriter, VideoWriter_fourcc, imread, resize
import os
from subprocess import call
 
 
img_root = 'images/'
out_root = 'pig.avi'
 
fps = 1
fourcc = VideoWriter_fourcc(*"MJPG")  #支持jpg
videoWriter = cv2.VideoWriter(out_root, fourcc, fps, (640, 480))
im_names = os.listdir(img_root)
print(len(im_names))
for im_name in range(1, 4):
    string = img_root + 'img' + str(im_name) + '.jpg'
    print(string)
    frame = cv2.imread(string)
    frame = cv2.resize(frame, (640, 480))
    videoWriter.write(frame)
 
videoWriter.release()
# 将输出的视频变为mp4格式或者压缩
dir = out_root.strip(".avi")
command = "ffmpeg -i %s.avi %s.mp4" % (dir, dir)
call(command.split())

(3) During the execution process, we can see that a total of 3 pictures have been processed.

Wait for the code to run to completion,

(4) Execution result: two video files will be generated, avi video file and mp4 video file,

One of the original  ffmpeg/multiple pictures to synthesize video

★The business card at the end of the article can receive free audio and video development learning materials, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

 

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/131724828