Reputation: 1
I have tried different methods but none of them works. As video is running but and file output.avi is empty.
Here is code
import cv2
vid=cv2.VideoCapture(0)
if (not vid.isOpened()):
print 'Error'
size = (int(vid.get(3)), int(vid.get(4)))
fourcc = cv2.cv.CV_FOURCC('M','J','P','G')
newVideo=cv2.VideoWriter('output.avi',fourcc,20.0,(640,360))
while(vid.isOpened()):
ret,frame=vid.read()
if ret is True:
frame=cv2.flip(frame,0)
newVideo.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1)& 0xFF==ord('q'):
break
else:
break
vid.release()
newVideo.release()
cv2.destroyAllWindows()
Upvotes: 0
Views: 530
Reputation: 8005
You need to be sure that the output frame's dimension and initialized VideoWriter
dimensions are the same.
cv2.resize(frame, (640, 360))
But, I'm not sure you are using python3
. Since print 'Error'
and fourcc = cv2.cv.CV_FOURCC('M','J','P','G')
are not python3
statements.
For python2
the answer will be:
# Add true at the and of the parameter for specifying the frames are RGB.
newVideo = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 360), True)
.
.
if ret is True:
frame=cv2.flip(frame,0)
frame = cv2.resize(frame, (640, 360)) # missing statement
newVideo.write(frame)
cv2.imshow('frame',frame)
.
.
For python3
the answer will be:
import cv2
vid = cv2.VideoCapture(0)
if not vid.isOpened():
print('Error')
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
newVideo = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 360), True)
while vid.isOpened():
ret, frame = vid.read()
if ret:
frame = cv2.flip(frame, 0)
frame = cv2.resize(frame, (640, 360))
newVideo.write(frame)
cv2.imshow('frame', frame)
if (cv2.waitKey(1) & 0xFF) == ord('q'):
break
else:
break
cv2.destroyAllWindows()
vid.release()
newVideo.release()
Upvotes: 1
Reputation: 4367
The problem is simple, you are reading frame but you are trying to write them in a different size.
The solution is inside of your code. You defined:
size = (int(vid.get(3)), int(vid.get(4)))
But you didn't use it as like:
newVideo=cv2.VideoWriter('output.avi',fourcc,20.0,size)
You set the sizes as manually(640,360) which is not the size of captured frames.
Upvotes: 1