Reputation: 544
I would like to crop a region of a video, and save it. I used the following code to do it.
import cv2
cap = cv2.VideoCapture('input.avi')
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, 25, (1280, 720))
while True:
ret, image_np = cap.read()
if not ret:
break;
roi = image_np[300:1020, 0:1280]
out.write(roi)
cap.release()
out.release()
Video file is created, and I can watch in a media player, but if a want to read this file in python, I get an error message.
import cv2
cap = cv2.VideoCapture('output.avi')
_, image_np = cap.read()
Process finished with exit code -1073741819 (0xC0000005)
I think the problem is with the video I made, but I am not sure.
Upvotes: 1
Views: 5049
Reputation: 732
cv2.VideoWriter
expects the size of the frame to be same as the frame you write. roi
shape is not (1280, 720)
.
import cv2
cap = cv2.VideoCapture('input.avi')
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output.avi', fourcc, 25, (1280, 720))
writerInit = False
while True:
ret, image_np = cap.read()
if not ret:
break;
roi = image_np[300:1020, 0:1280]
if(not writerInit):
h,w,_ = roi.shape
out = cv2.VideoWriter('output.avi', fourcc, 25, (w, h))
writerInit = True
out.write(roi)
cap.release()
out.release()
Upvotes: 2