Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a video using OpenCV 2.4.0 in python

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?

like image 917
ATOzTOA Avatar asked Jan 21 '13 14:01

ATOzTOA


People also ask

Does OpenCV work on videos?

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.


2 Answers

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

like image 95
Ayub Khan Avatar answered Oct 04 '22 21:10

Ayub Khan


height, width, layers = img.shape
out = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*"XVID"), 30,(width,height))
out.write(img)
out.release()
like image 20
YakovK Avatar answered Oct 04 '22 19:10

YakovK