6

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?

2
  • I would try two things: First, try changing it to fourcc = cv2.VideoWriter_fourcc(*'DIVX') per the documentation. Second, try changing your final parameter in cv2.videoWriter from 1 to isColor=true. Commented Nov 20, 2015 at 19:59
  • unfortunately, the first change is only applicable for opencv3.0 that is the part I changed from the tutorial to my own version. the second change is equal to 1 which changed nothing. Commented Nov 20, 2015 at 20:40

4 Answers 4

13

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()
Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting this error: 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/' ???' Any idea why?
@Jephron in OpenCV 3, I'm getting AttributeError: module 'cv2.cv2' has no attribute 'cv'
The fact that they are case sensitive was they key for me, thank you!
0

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???'

Comments

0

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.