3

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!

iman453
  • 9,105
  • 16
  • 54
  • 70

1 Answers1

3

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.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks for your reply :) I'm new to python, so sorry if this is a stupid question, but since this works through python bindings, do I still check for None while creating CreateVideoWriter? I think it's returning a none null object. I also tried all the different codecs mentioned on the link you mentioned, and I'm running OpenCV 2.2. The problem still persists though :s Is there anything else I could try? Thanks again – iman453 Apr 25 '11 at 21:30
  • I added a "if writer is None: print "error"" check. Yep, I have permission to create files in this directory. Thanks for your help, I really appreciate you taking the time! – iman453 Apr 25 '11 at 21:49
  • Oh sorry, I am infact running it with im1.width and height. I accidentally pasted the version with the error. Sorry about that. – iman453 Apr 25 '11 at 21:50
  • Hehe, sorry the pasted code has errors all over! My bad. I actually removed my username because it is a start ups code I'm working on, but the username exists in the code I'm running. Also, do the bindings for ReleaseVideoWriter not exist? I tried calling it and passing it the writer object, but it gives me "NameError: name 'ReleaseVideoWriter' is not defined". Thanks! – iman453 Apr 25 '11 at 21:59
  • 1
    My last suggestion is re-install **ffmpeg** on your system. Check: http://opencv.willowgarage.com/wiki/FFMPEG – karlphillip Apr 25 '11 at 22:03
  • It gives me a name error for that as well, "NameError: name 'CV_FOURCC_DEFAULT' is not defined". I will try reinstalling ffmpeg. Thanks a million for your time...I truly appreciate it! – iman453 Apr 25 '11 at 22:04
  • I'm getting the error "NameError: name 'cvReleaseVideoWriter' is not defined". Does that mean openCV was not installed properly? – iman453 Apr 25 '11 at 22:32
  • 1
    You are getting that error because due to the way you are importing OpenCV you must use `ReleaseVideoWriter`, remove the letters `cv` from the beginning. Or is that with my code? – karlphillip Apr 25 '11 at 22:36
  • I actually tried it both ways. I'm not able to import highgui as well. I think I'm going to try installing highgui somehow. I feel horrible for bothering you with this so much! heh – iman453 Apr 25 '11 at 22:38
  • Yeah, if you could let me know when they reply, I'd be really grateful. Thanks :) – iman453 Apr 26 '11 at 06:34