Ask Your Question
0

output.avi file created but its size is 0k

asked 2016-08-24 09:51:59 -0600

ravinm gravatar image

updated 2016-08-24 16:35:25 -0600

harsha gravatar image

import numpy

import cv2

cap = cv2.VideoCapture(0)

fourcc = cv2.cv.CV_FOURCC(*'MJPG')

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

while(cap.isOpened()):

ret, frame = cap.read()

if ret==True:

    frame = cv2.flip(frame,1)

    # write the flipped frame
    out.write(frame)

    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
else:
    break

cap.release() out.release() cv2.destroyAllWindows()

image description

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-08-24 12:35:44 -0600

harsha gravatar image

updated 2016-08-24 15:28:56 -0600

From this answer, frame size used while initializing videoWriter and the size of the frame being written to the file should be the same.

Resize "frame" before writing it

import numpy
import cv2

cap = cv2.VideoCapture(0)
fourcc = cv2.cv.CV_FOURCC(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frameTmp = cap.read()
    if ret==True:
        frame = cv2.resize(frameTmp, (640, 480))
        frame = cv2.flip(frame,1)

        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

Get frame size from videoCapture to initialize videoWriter.

import numpy
import cv2

cap = cv2.VideoCapture(0)
width = cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
fourcc = cv2.cv.CV_FOURCC(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (int(width),int(height)))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,1)

        # write the flipped frame
        out.write(frame)
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
out.release()
cv2.destroyAllWindows()

Update 1: Added code snippets

edit flag offensive delete link more

Comments

Thanks for reply.... But none of the above corrections resolved the problem.

ravinm gravatar imageravinm ( 2016-08-24 16:43:12 -0600 )edit

Problem could be in videoCapture or videoWriter. Could you please check if there are any issues with videoCapture, by just visualizing the frames?

harsha gravatar imageharsha ( 2016-08-25 10:19:02 -0600 )edit
0

answered 2016-09-06 04:59:43 -0600

Karthikeyan gravatar image

I have faced the same issue, the reason is the application(.exe) folder should contain the ffmpeg dll depending on your OpenCV version in order to write the videofile. If the dll is not present there, the video file written will be of 0KB. Kindly check whether your application folder contains that dll.

For example, opencv_ffmpeg310.dll for OpenCV 3.1.0

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-08-24 09:51:59 -0600

Seen: 3,544 times

Last updated: Sep 06 '16