I'm trying to resize a video input using OpenCV and export it as .avi or .mp4 file. For some unknown reason, the result file outcomes corrupted and refuses to be played.
I have tried several different combinations of video formats and codec types, such as: avi-MJPG, avi-XVID, avi-h264, avi-x264, mp4-mp4v and mp4-avc3. None of them seems to be working correctly.
import cv2
cap = cv2.VideoCapture('input.mp4')
out = cv2.VideoWriter(
'output.avi',
cv2.VideoWriter_fourcc(*'XVID'),
cap.get(cv2.CAP_PROP_FPS),
(640, 480)
)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame = cv2.resize(frame, (640, 480))
out.write(frame)
# cv2.imshow("video", frame)
# cv2.waitKey(1)
cap.release()
out.release()
# cv2.destroyAllWindows()
UPD: Running this code on Windows 10 machine with Python 3.11.1, using pre-built opencv-python (4.7.0.68)
and opencv-contrib-python (4.7.0.68)
.
As mentioned below this code works perfectly fine in another runtime environment, so probably this issue is related with OS.