2

I have just started using OpenCV 2.4.8.2 in Python 2.7.6 on a MacBook Pro Retina running OS X 10.9.2. My main goal is to make a video file using a few NumPy-arrays. I would also like to do the inverse: decompose a video into separate frames (and consequently in NumPy-arrays).

To make a video, I use the following piece of code:

import cv2

# Composes a movie from separate frames.
videoMaker = cv2.VideoWriter("videoTest.mov", cv2.cv.CV_FOURCC('m', 'p', '4', 'v'), 1, (256, 256))
if (videoMaker.isOpened()):
    videoMaker.write(cv2.imread("videoTestFrame0.jpg"))
    videoMaker.write(cv2.imread("videoTestFrame1.jpg"))
    videoMaker.write(cv2.imread("videoTestFrame2.jpg"))
    videoMaker.release()

This piece of code seems to work fine - videoTest.mov is created, can be played in Quicktime, is 3 seconds long and consists of 3 different frames.

To load a video, I have put directly under the above code piece:

# Decomposes a movie into separate frames.
videoReader = cv2.VideoCapture("videoTest.mov")
count = 0
while True:
    gotImage, image = videoReader.read()
    if (gotImage):
        cv2.imwrite("videoTestDecomposedFrame%d.jpg" %count, image)
        count += 1
    else:
        break

The problem: When inspecting the 3 decomposed frames, the first frame of the movie (videoTestFrame0.jpg) is not one of them, while the last frame of the movie (videoTestFrame2.jpg) is stored twice.

How can I fix this problem and retrieve videoTestFrame0.jpg from the video without having to modify it by putting in this first frame twice? Might something be wrong with cv2.VideoCapture.read()? I have tried saving the movie as videoTest.avi instead of videoTest.mov, but the behavior at decomposition is the same.

Thanks in advance for your kind help! Martijn

Martijn
  • 21
  • 1
  • 3
  • I tried executing your first segment of code on a Windows Vista system running Python 2.7.6, with OpenCV 2.4.4, and FWIW, I couldn't even get OpenCV to create the `videoTest.mov` file. By inserting `print()` statements, and checking `isOpened()` multiple times, I was able to verify that OpenCV does indeed believe that it has opened and then closed the file, but no file actually appears. According to [this](http://stackoverflow.com/questions/11699298/opencv-2-4-videocapture-not-working-on-windows), OpenCV video seems somewhat broken by default. Perhaps you might try using another FOURCC codec? – stachyra Apr 27 '14 at 06:26
  • Hey stachyra, thanks for your comment! I assume you've used 3 correctly named 256 x 256 pixel .jpg-files for the test - strange that the video file does not appear. On my Mac however, the `videoTest.mov` or `videoTest.avi` creation *does* work and only the decomposition into frames seems to fail. I will try whether using another FOURCC codec works; thanks for the suggestion. :) – Martijn Apr 27 '14 at 13:19
  • Yes, I used 3 correctly named and sized jpg files, and put them in a directory, together also with a .py file containing your code, where I knew that I had write access. I don't think I screwed this up; the `VideoWriter` module is honestly doing something weird on my machine. And by (bad) design, I can't really even tell what that might be, because the `write()` and `release()` methods don't return any status values (i.e., return is `None`), so even if these methods are catching any errors internally, or generating warnings, I can't tell from the non-existent status values what those might be. – stachyra Apr 27 '14 at 13:35

0 Answers0