Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create a basic video file using OpenCV

Tags:

python

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!

like image 553
iman453 Avatar asked Jun 04 '26 06:06

iman453


1 Answers

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.

like image 58
karlphillip Avatar answered Jun 07 '26 13:06

karlphillip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!