3

I am trying to create a TimeLapse creator in python using OpenCV, and have the following code:

import cv2
import os
import string

directory = (r"C:\Users\Josh\Desktop\20130216")
video = cv2.VideoWriter(r"C:\Users\Josh\Desktop\video.avi", cv2.cv.CV_FOURCC('F','M','P', '4'), 15, (1536, 1536), 1)

for filename in os.listdir(directory):
    if filename.endswith(".jpg"):
        video.write(cv2.imread(filename))

cv2.destroyAllWindows()
video.release()

The folder has 1,440 pictures in it, yet video.avi is only 5.54kb in size, and has an empty output when played. Can anyone see any flaws in my code, and give me any help?

Josh Wood
  • 1,598
  • 3
  • 16
  • 21
  • Have you tried displaying images individually? This would tell you if the images are actually being read (i.e. their paths are valid)). – Aurelius Jun 26 '13 at 21:20

3 Answers3

5

I have the same issue because the input video size (width and height) does not match the output file. Mine shows 6KB.

I believe this is because openCV is not intended to do this kind of job (record/save video)

To fix this, just match the width and height

width, height = frame.shape
out = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc('M','J','P','G'), fps, (width, height))
Pay C.
  • 1,048
  • 1
  • 13
  • 20
1

It seems to be that, you have windows without ffmpeg support. I had the same problem and OpenCV 2.4 VideoCapture not working on Windows helped me on that.

The opencv246\opencv\3rdparty\ffmpeg\opencv_ffmpeg.dll should be copied to c:\Python27\opencv_ffmpeg246.dll

Your frame size defined in your code as 1536x1536, so all of your .jpg files should match that size.

video = cv2.VideoWriter(r"C:\Users\Josh\Desktop\video.avi", 
cv2.cv.CV_FOURCC('F','M','P', '4'), 15, (1536, 1536), 1)
Community
  • 1
  • 1
jshepherd
  • 900
  • 1
  • 9
  • 22
1

The reason why cv2.VideoWriter can't save the video content with correct code is the output video's width, height size is different with input video's width, height size.

You need to match them

Chance Park
  • 83
  • 1
  • 9