VideoWriter输出损坏的视频文件 [英] VideoWriter outputs corrupted video file

查看:724
本文介绍了VideoWriter输出损坏的视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我保存web_cam流的代码.它可以正常工作,但是输出视频文件有问题.

This is my code to save web_cam streaming. It is working but the problem with output video file.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
#fourcc = cv2.cv.CV_FOURCC(*'DIVX')
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

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

推荐答案

由于错误的帧速率和帧分辨率,输出文件已损坏.使用此代码:

The output file is corrupted because of the wrong frame rate and frame resolution. Using this code :

out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))

我们将每秒fps/帧速率设置为20.这是不正确的.此外,框架的宽度和高度是错误的.我通过从捕获的web_cam配置文件中获取fps,宽度,高度来解决.

We set the fps/frame rate per second 20. Which was not correct. Also, the frame width and height was wrong. I solved by getting fps, width, height from the captured web_cam profile.

cap = cv2.VideoCapture(0)  #web-cam capture

fps = cap.get(cv2.CAP_PROP_FPS)
width  = cap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
out = cv2.VideoWriter('output.avi', -1,fps, (int(width), int(height)))

这篇关于VideoWriter输出损坏的视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆