Skip to content

Available fourcc & extensions in macOS when saving a video with python-OpenCV #129

@FredWe

Description

@FredWe
Python3.__version__ == 3.6.3
openCV.__version__ == 3.3.0
macOS.__version__ == 10.12.6 (16G29)

When i tried to practice http://docs.opencv.org/master/dd/d43/tutorial_py_video_display.html this tutorial of python-OpenCV, i find it's difficult to execute directly the samplec code. So i struggled with this little piece of code. And finally some available parameters are found, incomplete but enough for me as so far.

Fourcc : mp4v, avc1, h264
Extension : .avi, .mkv

  1. : Don't replace fourcc by its default value -1, OpenCV in macOS doesn't support it.
[alac @ 0x7fbce5104e00] Specified sample format -1 is invalid or not supported
Could not open codec 'alac': Unspecified error
2017-10-05 13:33:50.721 Python[1991:15401653] AVF: AVAssetWriter status: Cannot Save
2017-10-05 13:33:50.771 Python[1991:15401653] mMovieWriter.status: 3. Error: Cannot Save
  1. The output file extension matters. Using extension mp4 could make some warning like :
penCV: FFMPEG: tag 0x34363268/'h264' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000021/'!???'

But it also works.

My sample code here:

import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret = cap.set(cv2.CAP_PROP_FRAME_WIDTH,640)
ret = cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480)

#define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'h264')
out = cv2.VideoWriter('output.avi', fourcc, 20., (640,480))

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

        #write the fliped frame
        out.write(frame)

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

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

Ref:

http://tsaith.github.io/record-video-with-python-3-opencv-3-on-osx.html

https://gist.github.com/takuma7/44f9ecb028ff00e2132e

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions