简体   繁体   中英

Python - Using OpenCV how to stick .png's into a single mp4?

I have a directory of .png images that I want to stick into a single mp4? I am confident OpenCV can be used to do so, but I cannot find any resources on how exactly to? Any ideas? or tutorials?

Thanks.

The best way to do this would be with ffmpeg , you would do it like this:

ffmpeg -framerate 24 -i img%03d.png output.mp4

where:

  • -framerate is your desired framerate (in fps)
  • -i indicates that your images are formatted in the format img001.png , img002.png ect...
  • and output.mp4 is the output file

More information here

You can use VideoWriter to first write your mp4 to a new file, and then write your images at the tail of this file, as shown:

import cv2
import cv
cap = cv2.VideoCapture("your_mp4.mp4")
ret,img=cap.read()
frame1=cv2.imread("your_img1.jpg")
frame2=cv2.imread("your_img2.jpg")
height , width , layers =  img.shape     
fps=20
video = cv2.VideoWriter("rec_out.avi", cv.CV_FOURCC(*'DIVX'), fps, (img.shape[1], img.shape[0]))
while True: 

    ret,img=cap.read()
    height , width , layers =  img.shape
        video.write(img)
    cv2.imshow('Video', img)
    video.write(img)
    if(cv2.waitKey(10) & 0xFF == ord('b')):
            break

#stick your images here
video.write(frame1)
video.write(frame2)
cv2.destroyAllWindows()
video.release()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM