一些编解码器的OpenCV Python VideoWriter错误的fps速率

时间:2018-05-22 09:50:03

标签: python opencv

我正在尝试阅读视频,调整大小并使用不同的编解码器编写它们,使用OpenCV for Python3。原始帧速率应该保持不变。

如果我使用MJPG作为编解码器,这可以正常工作,但对于其他编解码器,输出的帧速率设置为600 fps。 (我试过XVID,DIVX,WMV1,WMV2)

是否可以使用原始帧速率编写具有这些编解码器的视频?

import os
import numpy as np
import cv2

codec = 'XVID'
new_size = (256, 256)

for root, dirs, files in os.walk("UCF-101"):
    new_root = root.replace('UCF-101', 'UCF-101_resized_' + codec)
    if not os.path.exists(new_root):
        os.makedirs(new_root)
    for file in files:
        cap = cv2.VideoCapture(root + '/' + file)
        fps = cap.get(cv2.CAP_PROP_FPS)
        fourcc = cv2.VideoWriter_fourcc(*codec)
        out = cv2.VideoWriter(new_root + '/' + file, fourcc, fps, new_size, isColor=True)
        while(cap.isOpened()):
            ret, frame = cap.read()
            if ret == True:
                frame = cv2.resize(src=frame, dst=frame, dsize=new_size)
                out.write(frame)
            else:
                break
        cap.release()
        out.release()
        print('wrote ' + new_root + '/' + file)

1 个答案:

答案 0 :(得分:0)

尝试将文件扩展名更改为使用.mp4而不是avi的输出文件名

codec = 'x264'

if ret == True:替换为if frame is not None: