Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV videowrite doesn't write video

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

like image 649
Zachzhao Avatar asked Mar 28 '15 18:03

Zachzhao


3 Answers

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.

like image 200
Pijar Avatar answered Oct 21 '22 13:10

Pijar


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')
like image 22
Lamar Latrell Avatar answered Oct 21 '22 12:10

Lamar Latrell


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()
like image 20
Sabito 錆兎 stands with Ukraine Avatar answered Oct 21 '22 13:10

Sabito 錆兎 stands with Ukraine