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



我正在尝试阅读视频,调整它们的大小并使用不同的编解码器编写它们,使用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)

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

codec = 'x264'

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

最新更新