Unable to create a basic video file using OpenCV

后端 未结 1 645
清歌不尽
清歌不尽 2020-12-11 22:18

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

相关标签:
1条回答
  • 2020-12-11 22:45

    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.

    0 讨论(0)
提交回复
热议问题