0

我尝试阅读视频,翻转帧然后保存它,但输出视频窗口显示它已损坏并且无法播放。

import cv2 as cv

cap = cv.VideoCapture('wtf.avi')
fourcc = cv.VideoWriter_fourcc(*'DIVX')
out= cv.VideoWriter('output.avi', fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        frame = cv.flip(frame, 0)
        # write the flipped frame
        out.write(frame)
        cv.imshow('frame', frame)
        if cv.waitKey(35) & 0xFF == ord('q'):
            break
    else:
        break

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

1 回答 1

1

根据我的经验,如果您的操作系统中没有安装编解码器或其他版本的编解码器,那么您尝试保存视频的版本可能会导致它崩溃。

即使这段代码是用 C++ 编写的,它也可以帮助您:

// 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)
于 2018-11-14T22:07:39.817 回答