如何使用openCV将视频导出为.mp4? [英] How to export video as .mp4 using openCV?

查看:1926
本文介绍了如何使用openCV将视频导出为.mp4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openCV将视频导出为.mp4。我尝试了几种编解码器,但现在我没有成功。



这是一个从框架构建视频的功能:


$ b $
img1 = cv2.imread(temp / scr0.png)
高度,宽度,layers = img1.shape
codec = cv2.cv.CV_FOURCC('X','V','I','D')
video = cv2.VideoWriter(out_directory,codec,fps, (宽,高))

在我的范围(total_frames):
img_name =temp / scr+ str(i)+.png
img = cv2 .imread(img_name)
video.write(img)

video.release()
cv2.destroyAllWindows()

我通常会收到下一个错误消息,使用不同的编解码器:

 标签XVID / 0x44495658与输出编解码器ID'13'不兼容

是否可以这样做和如何?

解决方案

直接解决您导出为.avi,然后使用python的调用终端命令转换为.mp4。

 从子进程导入调用

dir = out_directory.strip(。avi)
command =avconv -i%s.avi -c:v libx264 -c:a copy%s.mp4%(dir,dir )
调用(command.split())


I am trying to export video as .mp4 with openCV. I have tried several codecs but for now I had no success.

This is a function that constructs a video from frames:

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

I usually get next error message, using different codecs:

Tag XVID/0x44495658 incompatible with output codec id '13'

Is is possible to do this and how?

解决方案

There is a non-direct solution. You export as .avi and then convert to .mp4 using python's call which calls terminal command.

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆