Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoWriter fails quietly for non-square videos #4655

Closed
opencv-pushbot opened this issue Jul 27, 2015 · 5 comments
Closed

VideoWriter fails quietly for non-square videos #4655

opencv-pushbot opened this issue Jul 27, 2015 · 5 comments

Comments

@opencv-pushbot
Copy link
Contributor

Transferred from http://code.opencv.org/issues/3424

|| Talia Weiss on 2013-12-06 19:22
|| Priority: Normal
|| Affected: branch 'master' (3.0-dev)
|| Category: python bindings
|| Tracker: Bug
|| Difficulty: 
|| PR: 
|| Platform: x64 / Linux

VideoWriter fails quietly for non-square videos

In python, when using cv2.VideoWriter, if the dimensions of the video are not square, it will quietly fail and produce a 5.7 kb (for XVID/avi) empty video file.

When dimensions are square, the video is made correctly.

I tested this with several dimensions (1000, 1000), (720, 1024), (333, 333), (333, 334), (334, 333) in both isColor-True/False, and writing just pure black and white arrays of appropriate size.

History

Talia Weiss on 2013-12-06 23:37
Some more information on failure modes:

Example code: http://pastebin.com/jMFQLAN2

Resulting videos attached.

Discussion on opencv irc channel leads me to believe this bug does not occur when writing in C/C++ - so this is probably python only
-   File non-square.avi added
-   File square.avi added
Nikita Manovich on 2013-12-10 11:20
-   Category set to python bindings
Nikita Manovich on 2013-12-10 19:49
Hi,

Thank you for the bug report. Could you please help us and contribute to OpenCV project? Here you can find all necessary information: http://www.code.opencv.org/projects/opencv/wiki/How_to_contribute.
Can you reproduce the problem with opencv 2.4.7? Is it 3.0 specific?
-   Assignee set to Talia Weiss
-   Status changed from New to Open
Talia Weiss on 2013-12-11 15:15
I can try, but I have looked at the code and I really don't understand how the python bindings are generated completely - and my C/C++ is extremely rusty.  

I will check 2.4.7 now that I have gstreamer running
Talia Weiss on 2013-12-11 15:53
This also occurs with 2.4.7

(Same test code, with cv2.cv.FOURCC instead of cv2.VideoWriter_fourcc)
David Zwicker on 2014-08-02 04:45
I also encounter this bug on Mac OSX 10.9.4. Program version are

ffmpeg 2.3.1
opencv 2.4.9
python 2.7.8

all installed via macports 2.3.1
Veikko Leppävuori on 2015-01-14 08:20
Just stumbled with that same problem. In some phase width and height values are switching places (thats why square frames work). Workaround is to switch width and height when opening VideoWriter, ie if video frames are (800,600) open writer as (600,800) and it works just fine :D

Ubuntu 14.04
ffmpeg version 2.5.3
openCV 3.0.0
Python 3.4.0
@zeta274
Copy link

zeta274 commented Jul 18, 2016

Debian 8.5
FFmpeg 2.7.2
OpenCV 2.4.9
Python 2.7.8

Same issue. No solution in sight.

@pstch
Copy link

pstch commented Aug 12, 2016

As said in the other bug tracker, VideoWriter will handle non-square videos fine, but width should be passed before height.

The real bug looks to be in the error handling, I think this issue should be closed.

@kubilus1
Copy link

kubilus1 commented Dec 17, 2016

I believe I'm seeing this issue as well, but with a twist. I get successful videos for a time, then it suddenly spits out empty videos.

OpenCV 2.4.9
Python 2.7.9
Debian 8.0

on a raspberry pi

Essentially I'm doing the following:

        fourcc = cv2.cv.CV_FOURCC('M','J','P','G')
        h,w,c = self.frames[0].get('img').shape
        writer = cv2.VideoWriter(
            vidpath,
            fourcc,
            FPS,
            (w,h)
        )
        for f in self.frames:
            img = f.get('img')
            writer.write(img)
        writer.release()
        writer = None

self.frames is a list of VideoCapture'd frames. This works correctly for hours until it suddenly doesn't.

@kubilus1
Copy link

Here's a standalone script that illustrates the issue:

#!/usr/bin/pyton

import cv2
import time
import numpy
import subprocess

fourcc = cv2.cv.CV_FOURCC(*'MJPG')

allret = 0
f = numpy.ones((480,640,3), numpy.uint8)
h,w,c = f.shape
print h,w,c
counter = 0
while True:
    counter += 1
    print "Iteration: ", counter
    time.sleep(0.1)
    writer = cv2.VideoWriter("test.avi", fourcc, 5, (w,h))
    for i in xrange(20):
        writer.write(f)
    writer.release()
    writer = None
    ret = subprocess.call(["avprobe","test.avi"])
    allret += ret
    print "FAILURES:", allret
    if allret > 5:
        break

After about 800 or so successful videos we then only get bad videos. Restarting the scripts starts the process over. This implies that something in the module itself goes awry over time.

@mshabunin
Copy link
Contributor

As have been stated by @pstch, when creating VideoWriter in Python one should pass frame dimensions in form cv.VideoWriter(filename, fourcc, fps, (w, h), ...). And when creating frame itself - in reverse order: frame = np.zeros((h, w), ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants