24
votes

I used the following page from OpenCV 3.0.0 tutorial: Tutorial in docs

When I tried to use the example that saves videos, it doesn't work.

It displays the content from the webcam, and also creates a file called output.avi, but when I checked the size of ouput.avi, it was zero bytes.

I also tried using different codecs, like YUY2.

I use Python 2.7.8 and OpenCV 3.0.0 and Windows 8.1

14

14 Answers

27
votes

I had the same problem and i solved it by specifying the video output resolution to exactly the same as input:

cap = cv2.VideoCapture('vtest.avi')
...
out = cv2.VideoWriter('output.avi',fourcc, 20.0,(int(cap.get(3)),int(cap.get(4))))

Of course make sure you got ffmpeg installed and working.

22
votes

Replacing:

fourcc = cv2.VideoWriter_fourcc(*'XVID')

With:

fourcc = cv2.VideoWriter_fourcc('M','J','P','G')

Worked for me...

More generally:

Look up the fourcc code of the video compression format you're after here, and whatever the code is - for instance 'FMP4' for FFMpeg - plug it in in the following manner:

cv2.VideoWriter_fourcc('F','M','P','4')
11
votes

I was struggling with this problem for a few hours then I realized that I had typed the image's shape wrong.

It is (width, height), ie:

(image.shape[1], image.shape[0])

and not

(image.shape[0], image.shape[1])

This is how my working code looks like finally... (I am on a Windows machine):

video_path = FILENAME + '.avi'
size = images[0].shape[1], images[0].shape[0] ## <<<--- NOTICE THIS
video = cv2.VideoWriter(video_path,cv2.VideoWriter_fourcc(*'DIVX'), 60, size)  

for img in images:  
    video.write(img)

cv2.destroyAllWindows()
video.close()
9
votes

Make sure you are using the correct fourcc 4-byte code. The example on the tutorial has:

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

This 'XVID' code may only work for linux. The documentation above the example states (in the tutorial): "In Windows: DIVX (More to be tested and added)." So if you haven't, try replacing the fourcc line above with:

fourcc = cv2.VideoWriter_fourcc(*'DIVX')

I use cv2.VideoWriter on linux quite often and it always works. So if the above doesn't work you can always try it on a linux vm.

6
votes

In my case, I thought the codec was an obstacle but it wasn't. Instead, adjusting the dimensions being consumed by videoWriter() did the trick:

(ok, frame) = cv2.VideoCapture(videoPath).read()

 fourcc = cv2.VideoWriter_fourcc(*'XVID')
 out = cv2.VideoWriter(output, fourcc, 10.0, (1280, 720))

(1280,720) was used because frame.shape from my video outputs (1280, 720, 3). It made avi to mp4 and vice versa possible. Didn't have to worry about reconciling the codec.

4
votes

Check the resolution of your images! I was trying with odd-shaped images (1284x709) and ended up with unreadable videos of 1k in size. After resizing my image to the closest 'common' resolution: image = cv2.resize(image, (1280,720)), it worked just fine.

2
votes

It should be a problem with the codec you're using.

Have you tried:

cv.CV_FOURCC('i', 'Y', 'U', 'V')

This one worked for me.

2
votes

On windows 7 and using Python 2.7 and OpenCV 2.4.8, I experienced the fact that if the file NAME is "output.mpg" would not write.

I solved it by changing to "output.avi".

1
votes

I changed the Video writer size to my screen resolution size and it worked.

here is the solution.

out = cv2.VideoWriter("output.avi", fourcc, 5.0, (1920, 1080))
1
votes

I have the exact same issue. I am using OpenCV in C++, but I believe you can still pass -1 instead of choosing the codec so you can have a drop down menu of the available codecs even in python. From there I tried all different codecs and just like Leonard Zhou, the IYUV codec was the one that worked for me. Also note that it was the only one that worked even though I could use XVID just fine on another machine with the same code and same codec installer.

EDIT: What I suggested worked as a patch, the main issue was solved on my end by adding the opencv_ffmpeg dll in the executable's folder.

1
votes

I suspect there are a lot of reasons video writing fails silently, and in my case, my frame was a composite of a 3 channel image and a 4 channel image (transparent overlay). Converting the end result to BGR format as follows allowed the video to save successfully.

width = 1280
height = 720
FPS = 30
fourcc = VideoWriter_fourcc(*'avc1')
video = VideoWriter('output.mp4', fourcc, float(FPS), (width, height))
for img in images:
  frame = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
  video.write(frame)
video.release()
0
votes

It worked for me after I assigned the actual frame size and not what I thought the size was:

ret, frame = cap.read()
height, width, channels = frame.shape
codec = cv2.VideoWriter_fourcc(*"DIVX")
out=cv2.VideoWriter('new.avi',codec ,20.0,(width, height))

And put out.write(frame) inside the while loop before cv2.imshow("Frame", frame).

0
votes

To state the obvious, make sure you're giving not only a correct path, but also a video name. i.e. path/'video.avi' and not just path/. It will fail silently otherwise.

-2
votes

I tried by creating its exe using pyinstaller Recorder.py it worked for me.

Remember before using pyinstaller you have to pip install pyinstaller.