简体   繁体   English

Python-使用OpenCV如何将.png粘贴到单个mp4中?

[英]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? 我有一个要粘贴到单个mp4中的.png图像目录? I am confident OpenCV can be used to do so, but I cannot find any resources on how exactly to? 我相信可以使用OpenCV来做到这一点,但是我找不到有关如何精确使用的任何资源? Any ideas? 有任何想法吗? or tutorials? 还是教程?

Thanks. 谢谢。

The best way to do this would be with ffmpeg , you would do it like this: 最好的方法是使用ffmpeg ,您可以这样做:

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

where: 哪里:

  • -framerate is your desired framerate (in fps) -framerate是您想要的帧率(以fps为单位)
  • -i indicates that your images are formatted in the format img001.png , img002.png ect... -i表示您的图像格式为img001.pngimg002.png ...
  • and output.mp4 is the output file 而output.mp4是输出文件

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: 您可以使用VideoWriter首先将mp4写入新文件,然后将图像写入此文件的末尾,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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