[英]How to export video as .mp4 using openCV?
我正在尝试使用openCV将视频导出为.mp4。 我已经尝试了几种编解码器,但目前还没有成功。
此功能可根据帧构造视频:
def create_movie(self, out_directory, fps, total_frames):
img1 = cv2.imread("temp/scr0.png")
height, width, layers = img1.shape
codec = cv2.cv.CV_FOURCC('X','V','I','D')
video = cv2.VideoWriter(out_directory, codec, fps, (width, height))
for i in range(total_frames):
img_name = "temp/scr" + str(i) + ".png"
img = cv2.imread(img_name)
video.write(img)
video.release()
cv2.destroyAllWindows()
我通常会使用不同的编解码器得到下一条错误消息:
Tag XVID/0x44495658 incompatible with output codec id '13'
有可能做到这一点,怎么做?
有一个非直接的解决方案。 您导出为.avi,然后使用python的调用转换为.mp4的调用终端命令。
from subprocess import call
dir = out_directory.strip(".avi")
command = "avconv -i %s.avi -c:v libx264 -c:a copy %s.mp4" % (dir, dir)
call(command.split())
回答这个问题可能有点晚了,但是如果您想使用OpenCV编写.MP4文件,请尝试以下操作:
import cv2
#your previous code here
fourcc = cv2.VideoWriter_fourcc(*'a\0\0\0')
out = cv2.VideoWriter('out.mp4', fourcc, fps, res)
#the character '\0' is the Null-Terminator or simply 0x00 in the ASCII-Table
#tag: *'a\0\0\0' corresponds to 0x00000061
#your following code here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.