Opencv video read and save again is corrupted

I try to read a video, flip the frames and then save it, but the output video windows says its corrupted and wont play.

import cv2 as cv

cap = cv.VideoCapture('wtf.avi')
assert cap.isOpened()

fourcc = cv.VideoWriter_fourcc(*'DIVX')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (640,480))
assert out.isOpened()

while True:
    ret, frame = cap.read()
    if not ret:
        break

    frame = cv.flip(frame, 0)

    # write the flipped frame
    out.write(frame)

    cv.imshow('frame', frame)
    if cv.waitKey(35) == ord('q'):
        break

# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()

Upvotes: 1

Views: 676

Answers (1)

Arritmic
Arritmic

Reputation: 509

In my experience, if you do not have the codec or a different version of the codec installed in your OS, with which one you are trying to save your video can provoke it crashes.

Even if this piece of code is in C++, it could help you:

// Load input video or camera stream
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
    std::cout << "!!! Input video could not be opened" << std::endl;
    return 1;
}

float fps = cap.get(CV_CAP_PROP_FPS);
int cols = cap.get(CV_CAP_PROP_FRAME_WIDTH);
int rows = cap.get(CV_CAP_PROP_FRAME_HEIGHT);


// Setup output video
cv::VideoWriter video("output.avi", cap.get(CV_CAP_PROP_FOURCC), fps, cv::Size(cols,rows),true)

Upvotes: 2

Related Questions