turtletree
turtletree

Reputation: 93

Can't write video by opencv in Python

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?

Upvotes: 6

Views: 17694

Answers (4)

Fahd Zaghdoudi
Fahd Zaghdoudi

Reputation: 161

Saving video in MP4 format not working for me. Another solution works well, is to save to avi format. Try this code :

    # initialize our video writer
    fourcc = cv2.VideoWriter_fourcc(*"MJPG")
    writer = cv2.VideoWriter("output.avi", fourcc, 25, (frame.shape[1], frame.shape[0]), True)

Upvotes: 0

Boooooooooms
Boooooooooms

Reputation: 296

For me this comes to the codec problem. Accroding to the offcial website, sometimes altough our computer supports a specific codec the opencv doesn't. And mpeg is always the safe choice.

Also be careful that

FourCC is a 4-byte code used to specify the video codec.

fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = 25
out = cv2.VideoWriter('output.avi', fourcc, fps, size, isColor=True)

The list of availabe codec can be found here. fourcc.org

Upvotes: 0

Kinght 金
Kinght 金

Reputation: 18341

If you are using OpenCV 3.X, then you should make some changes:

## opening videocapture
cap = cv2.VideoCapture(0)

## some videowriter props
sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

fps = 20
#fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') 
#fourcc = cv2.VideoWriter_fourcc('m', 'p', 'e', 'g') 
fourcc = cv2.VideoWriter_fourcc(*'mpeg') 

## open and set props
vout = cv2.VideoWriter()
vout.open('output.mp4',fourcc,fps,sz,True)

# ...

While, some warning infos occur. But you will still write successfully.

## mp4v
OpenCV: FFMPEG: tag 0x7634706d/'mp4v' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'

## mpeg
OpenCV: FFMPEG: tag 0x6765706d/'mpeg' is not supported with codec id 2 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000061/'a???'

Upvotes: 0

Jephron
Jephron

Reputation: 2758

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()

Upvotes: 13

Related Questions