Opencv视频读取和保存再次损坏

问题描述 投票:0回答:1

我尝试读取视频,翻转帧然后保存它,但输出视频窗口说它已损坏且不会播放。

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()
python-3.x video opencv3.3
1个回答
0
投票

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

即使这段代码是用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)
© www.soinside.com 2019 - 2024. All rights reserved.