8

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 "VideoMixer.py", line 23, in <module>
    cv.WriteFrame(writer, cv.LoadImage(fileName))
cv.error: dst.data == dst0.data

Here's my script:

import cv
import sys

files = sys.argv[1:]

for f in files:
    capture = cv.CaptureFromFile(f)
    height = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)
    width = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)
    fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
    fourcc = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FOURCC)
    print fourcc
    writer = cv.CreateVideoWriter('ok.mov', int(fourcc),fps,(int(width),int(height)),1)
    print writer
    for i in range(30):
        frame = cv.QueryFrame(capture)
        print frame
        if frame:
            cv.WriteFrame(writer, frame)

Saving the frames as images works fine so I know there's nothing wrong with the capture. Am I creating the writer wrong? The 'print fourcc' outputs 0.0 but I've tried with many FOUR_CC values.

Thanks!

rgbrgb
  • 1,146
  • 1
  • 11
  • 17

3 Answers3

6

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

samplebias
  • 37,113
  • 6
  • 107
  • 103
  • 2
    Thanks a lot but are you able to play the out.avi? The script runs fine for me and makes the file but VLC gives me an error when I try to play it: ps error: cannot peek main debug: no demux module matching "avi" could be loaded main debug: TIMER module_need() : 303.765 ms - Total 303.765 ms / 1 intvls (Avg 303.765 ms) main error: no suitable demux module for `file/:///Users/***/Documents/Code/Video/out.avi' – rgbrgb Mar 25 '11 at 06:27
  • I can play the file, I'm using latest VLC on Mac. I'll update my answer with a screencap. – samplebias Mar 25 '11 at 15:46
  • I was also able to open and play the out.avi file using [AviDemux](http://avidemux.sourceforge.net/) and Quicktime. – samplebias Mar 25 '11 at 18:28
  • 3
    Hmm couldn't get AviDemux to work either. I think out.avi is corrupted because file is 8kb no matter how many frames I try to make. Are you using Quicktime or ffmpeg? I'm using quicktime so mayube this is the problem. Thanks again for your help. – rgbrgb Mar 26 '11 at 19:54
5

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.

mtoossi
  • 937
  • 8
  • 14
  • 1
    @mtoosi, what file extension did you save your file as? I tried `codec = cv.CV_FOURCC('M','P','4','V')` and saving the file as `/full/path/to/file/out.mp4v` which produced a valid file, but not one that opens in QuickTime or VLC. – cooncesean Dec 06 '12 at 07:24
  • @cooncesean, I used `.avi` as the file extension in all cases IIRC. Also, I was on OS X 10.8 and built OpenCV 2.4.3 myself with all the default flags and no third party libraries installed on the system. – mtoossi Dec 06 '12 at 17:23
  • FYI I'm on OS X 10.11 with OpenCV 2.4.13.2, and I was able to make this work with `cv2.cv.CV_FOURCC('M','P','4','V')` and an output filename with extension `.avi`. An extension of `.mp4` produced a video with a single frame and no motion. – trianta2 Apr 07 '17 at 20:35
  • With OpenCV 4.1.0 on OS X 10.14.5, `cv2.VideoWriter_fourcc(*"MP4V")` worked for me but only if I made the file an `.mp4`. If I tried `.avi`, QuickTime wouldn't play it. – Nathan Jul 12 '19 at 16:42
0

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

lunixbochs
  • 21,757
  • 2
  • 39
  • 47