Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

python - Unable to create a basic video file using OpenCV

I'm trying to create a basic video file using OpenCV (in Python). I have the following code, which runs without any errors, but I don't see the output file created. I was wondering if anyone had ideas as to what was going wrong.

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)

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why are you not checking errors? If CreateVideoWriter() is failing, you will never know.

I think that CreateVideoWriter is returning NULL. Add the appropriate code to check the return and verify if this is true.

If it is, the problem is most probably CV_FOURCC() which is not finding the codec.

Then, check this answer for other codecs: Creating AVI files in OpenCV

I also recommend you to update OpenCV to the most recent available (I think its v2.2).

EDIT:

You were also missing the last parameter in 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)

I think there's an issue with OpenCV/Linux/Python regarding cvCreateVideoWriter(). I'll try to talk with the devs and will update here when I get a reply.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...