python – 无法使用OpenCV创建基本视频文件

我正在尝试使用OpenCV(在Python中)创建基本视频文件.我有以下代码,运行没有任何错误,但我没有看到创建输出文件.我想知道是否有人知道出了什么问题.

from cv import *
im1 = LoadImage("/home/spoll/laptop1.jpg")
im2 = LoadImage("/home/spoll/laptop2.jpg")

writer = CreateVideoWriter("/home/spoll/out", CV_FOURCC('F', 'L', 'V', '1'), 2, (im1.width, im1.height))
if writer is None:
    print "Error in creating video writer"
else:
    print WriteFrame(writer, im1)
    print WriteFrame(writer, im2)

谢谢!

你为什么不检查错误?如果CreateVideoWriter()失败,你永远不会知道.

我认为CreateVideoWriter返回NULL.添加适当的代码以检查返回并验证是否为真.

如果是,问题很可能是CV_FOURCC(),它没有找到编解码器.

然后,检查其他编解码器的答案:Creating AVI files in OpenCV

我还建议您将OpenCV更新为最新版本(我认为它的v2.2).

编辑:

您还错过了CreateVideoWriter中的最后一个参数:

#!/usr/bin/env python
import sys

from opencv.cv import *
from opencv.highgui import *

im1 = cvLoadImage("img1.jpg")
if not im1:
    print "Could not load im1"

im2 = cvLoadImage("img2.jpg")
if not im2:
    print "Could not load im2"

fps = 4.0
frame_size = cvGetSize(im1)
#writer = cvCreateVideoWriter("out.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, frame_size, True)
writer = cvCreateVideoWriter("out.avi", CV_FOURCC('F', 'L', 'V', '1'), fps, frame_size, True)
if not writer:
    print "Error in creating video writer"
    sys.exit(1)
else:
    print cvWriteFrame(writer, im1)
    print cvWriteFrame(writer, im2)

cvReleaseVideoWriter(writer)

我认为关于cvCreateVideoWriter()的OpenCV / Linux / Python存在问题.我会尝试与开发人员交谈,并在收到回复时更新.

https://stackoverflow.com/questions/5781863/unable-to-create-a-basic-video-file-using-opencv

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:python – 无法使用OpenCV创建基本视频文件