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!
To start working with videos using OpenCV, we use the following functions: Cv2. VideoCapture() : It establishes a connection to a Video.It takes a parameter that indicates whether to use the built-in camera or an add-on camera. The value '0' denotes the built-in camera.
With the cv2. VideoWriter() function, with the first parameter, we choose where the file will be saved and what its name will be, along with the type of file it will be. In this case, it will be an mp4 video file. If saving in the current working directory, you will simply specify the name and type of video file.
To save a video in OpenCV cv. VideoWriter() method is used.
Do some of your frames have different colorspaces or depths? A few observations:
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
:
In Quicktime:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With