Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't write video by opencv in Python

Tags:

python

opencv

I am an open cv beginner. I followed the steps in this tutorial to practice using it. http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video

I changed some lines for performance on my mac:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
        int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))

# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'DIVX') # upper case - yl3
out = cv2.VideoWriter('output.avi',fourcc, 20, size, 1) #20.0: number of frames per sec

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,1)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

I am using Python 2.7.10, opencv 2.4.12 and mac OS X 10.10.5 Now I can successfully get the camera to work and generate an output.avi file in the same directory, but the file was only 414k overtime and I can't open it. It seemed that it contained nothing but black.

Can anyone help me with that?

like image 266
turtletree Avatar asked Nov 20 '15 19:11

turtletree


People also ask

How to read and write a video using OpenCV?

Read, Write and Display a video using OpenCV 1 Reading a Video. In OpenCV, a video can be read either by using the feed from a camera connected to a computer or by reading a video file. 2 Displaying a video. After reading a video file, we can display the video frame by frame. ... 3 Writing a video. ...

Does Python-OpenCV videowrite write video?

python - OpenCV videowrite doesn't write video - Stack Overflow I used the following page from OpenCV 3.0.0 tutorial: Tutorial in docs When I tried to use the example that saves videos, it doesn't work. It displays the content from the webcam, and also creates ... Stack Overflow About Products For Teams

Why is my OpenCV video file is blank?

Most common are frame size error and api preference error. If the frame size is not similar to the video, then even though we get a video file at the output directory, it will be blank. If you are using the NumPy shape method to retrieve frame size, remember to reverse the output as OpenCV will return height x width x channels.

What is VideoWRITER FourCC in Python?

VideoWriter_fourcc ('M', 'J', 'P', 'G') in Python. VideoWriter::fourcc ('M', 'J', 'P', 'G') in C++. The video codec specifies how the video stream is compressed. It converts uncompressed video to a compressed format or vice versa.


Video Answer


1 Answers

So I had the same problem as you. I narrowed it down to the value of cv2.cv.CV_FOURCC. Those are apparently case sensitive. I wasn't able to get the .avi codecs working, but if you don't mind a MPEG, this worked for me.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
        int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))

fps = 20
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case
vout = cv2.VideoWriter()
success = vout.open('output.mp4',fourcc,fps,size,True) 
while True:
    ret, frame = cap.read()
    if not ret:
        break

    frame = cv2.flip(frame, -1)

    vout.write(frame) 

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
vout.release()
cv2.destroyAllWindows()
like image 189
Jephron Avatar answered Sep 20 '22 01:09

Jephron