Writing video with OpenCV + Python + Mac

后端 未结 3 905
时光取名叫无心
时光取名叫无心 2020-12-08 17:50

I keep getting an assertion error when I\'m trying to write frames to video. The error I\'m getting is this:

Traceback (most recent call last):
  File \"Vide         


        
相关标签:
3条回答
  • 2020-12-08 18:14

    I tried various codecs including 'MJPG' and 'I420' and none of them worked on my Mac OpenCV build. They produced tiny unviewable output files without complaining.

    Then I found this page which lists some codecs that worked for me. E.g. 'mp4v' works fine on my Mac and QuickTime is able to play it.

    0 讨论(0)
  • 2020-12-08 18:17

    Do some of your frames have different colorspaces or depths? A few observations:

    • You have swapped the height and width in your code, is that on purpose?
    • Your fourcc should be an integer > 0. See my example below.

    I haven't personally generated Quicktime video using OpenCV, but this worked for me generating an uncompressed AVI file. I choose the I420 fourcc using the cv.CV_FOURCC function:

    import cv
    import sys
    
    # standard RGB png file
    path = 'stack.png'
    cap = cv.CaptureFromFile(path)
    fps = 24
    width = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_WIDTH))
    height = int(cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FRAME_HEIGHT))
    # uncompressed YUV 4:2:0 chroma subsampled
    fourcc = cv.CV_FOURCC('I','4','2','0')
    writer = cv.CreateVideoWriter('out.avi', fourcc, fps, (width, height), 1)
    for i in range(90):
        cv.GrabFrame(cap)
        frame = cv.RetrieveFrame(cap)
        cv.WriteFrame(writer, frame)
    

    Update: Screencapture of VLC playing out.avi:

    enter image description here

    In Quicktime:

    enter image description here

    0 讨论(0)
  • 2020-12-08 18:26

    This file in the OpenCV source implies on line #2598 - if this assert fails: "dst.data == dst0.data"

    it means that the destination size or type was incorrect

    0 讨论(0)
提交回复
热议问题