繁体   English   中英

无法播放使用 OpenCV VideoWriter 创建的视频

[英]Can't play video created with OpenCV VideoWriter

MWE

import cv2


FPS = 30
KEY_ESC = 27
OUTPUT_FILE = "vid.mp4"

cam = cv2.VideoCapture(0)

codec = cv2.VideoWriter.fourcc(*"mp4v") # MPEG-4 http://mp4ra.org/#/codecs
frame_size = cam.read()[1].shape[:2]
video_writer = cv2.VideoWriter(OUTPUT_FILE, codec, FPS, frame_size)

# record until user exits with ESC
while True:
    success, image = cam.read()
    cv2.imshow("window", image)

    video_writer.write(image)

    if cv2.waitKey(5) == KEY_ESC:
        break

cam.release()
video_writer.release()

问题

视频不播放。

Firefox 报告“未找到支持格式和 MIME 类型的视频。”。

VLC 报告“找不到任何 /moov/trak”“未找到流”。

问题是np.ndarray.shape 虽然没有正确记录,但返回(rows, columns)二维数组,对应于(height, width) VideoWriter(frameSize) 虽然没有正确记录,但似乎期望(width, height)

您可以使用以下方法更正此问题:

frame_size = tuple(reversed(cam.read()[1].shape[:2]))

但是,我建议使用VideoCapture属性创建VideoWriter ,如下所示:

output_file = "vid.mp4"
codec = cv2.VideoWriter.fourcc(*"mp4v")

fps = cam.get(cv2.CAP_PROP_FPS)

frame_width = cam.get(cv2.CAP_PROP_FRAME_WIDTH)
frame_height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
frame_size = (int(frame_width), int(frame_height))

video_writer = cv2.VideoWriter(output_file, codec, fps, frame_size)

资料来源:
VideoWriter()
视频捕捉.get()
https://answers.opencv.org/question/66545/problems-with-the-video-writer-in-opencv-300/

有关的:
OpenCV - 在 Python 中保存后视频不播放
OpenCV VideoWriter:播放视频的问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM