Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

  1. Resize "frame" before writing it
  2. Get frame size from videoCapture to initialize videoWriter.

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

  1. 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()
  1. Get frame size from videoCapture to initialize videoWriter. 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

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

  1. 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()
  1. 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