13

I am trying to create a video using OpenCV 2.4.0 in python 2.7.2. But the avi file size is 0.

My code:

from cv2 import *

im1 = cv.LoadImage("1.jpg")

fps = 20
frame_size = cv.GetSize(im1)

#writer = cv.CreateVideoWriter("out.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, frame_size, True)

v = VideoWriter()

v.open("out.avi", cv.CV_FOURCC('F', 'M', 'P', '4'), fps, (800,600), True)
print v.isOpened()

isOpened() is always returning false.

Another try:

#!/usr/bin/env python
import sys

from cv2 import *

im1 = cv.LoadImage("1.jpg")

if not im1:
    print "Error loading image"

im2 = cv.LoadImage("2.jpg")

if not im1:
    print "Error loading image"

fps = 20
frame_size = cv.GetSize(im1)

writer = cv.CreateVideoWriter("out.avi", cv.CV_FOURCC('M', 'J', 'P', 'G'), fps, frame_size, True)

if not writer:
    print "Error in creating video writer"
    sys.exit(1)
else:
    cv.WriteFrame(writer, im1)
    cv.WriteFrame(writer, im2)

del writer

No errors, but the output is empty.

What am I missing?

4
  • I haven't used OpenCV before but I guess You need to call python's equivalent of this function when the writing is done. Commented Jan 21, 2013 at 14:21
  • related : stackoverflow.com/questions/6918995/opencv-error-on-python Commented Jan 21, 2013 at 14:27
  • @AshwiniChaudhary I have checked all of the questions in SO, nothing works. Also, I tried del writer, still nothing. Seems like I am missing some codecs, maybe. Commented Jan 21, 2013 at 15:04
  • I'm hitting similar issues, I'm guessing there may be a bug in the writer. Commented Mar 15, 2013 at 13:29

3 Answers 3

42
import cv2

img1 = cv2.imread('1.jpg')
img2 = cv2.imread('2.jpg')
img3 = cv2.imread('3.jpg')

height , width , layers =  img1.shape

video = cv2.VideoWriter('video.avi',-1,1,(width,height))

video.write(img1)
video.write(img2)
video.write(img3)

cv2.destroyAllWindows()
video.release()

A simple code for what you want to do. for details here

Sign up to request clarification or add additional context in comments.

1 Comment

I get: AttributeError: 'cv2.VideoWriter' object has no attribute 'release'
7

Found this code, which works for me (generating colored noise):

writer = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 30,(640,480))
for frame in range(1000):
    writer.write(np.random.randint(0, 255, (480,640,3)).astype('uint8'))
writer.release()

Source: https://github.com/ContinuumIO/anaconda-issues/issues/223#issuecomment-285523938

Comments

1
height, width, layers = img.shape
out = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"XVID"), 30,(width,height))
out.write(img)
out.release()

1 Comment

Uncorrect answer: cv2 2.4.0 has no attribute 'VideoWriter_fourcc'

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.