openCV - ffmpeg H264 和 Webm 错误

openCV - ffmpeg H264 and Webm error

我安装了 ubuntu 16.04 LTS 和 OpenCV 3.4.0(Intel i5 和 AMD 显卡),我需要创建一个浏览器支持的视频,可以在浏览器中播放。

如果我使用的是 H264,我会收到

OpenCV: FFMPEG: tag 0x34363248/'H264' is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1' [h264_nvenc @ 0x7f4e0407f5e0] Cannot load libcuda.so.1 Could not open codec 'h264_nvenc': Unspecified error

如果我使用的是 webm VP8

OpenCV: FFMPEG: tag 0x30385056/'VP80' is not supported with codec id 139 and format 'webm / WebM'

如果我使用的是 webm VP9

OpenCV: FFMPEG: tag 0x30395056/'VP90' is not supported with codec id 167 and format 'webm / WebM'

我正在使用此代码进行转换。

    fourcc = cv2.VideoWriter_fourcc(*'VP80')
    frame = cv2.imread(movements[0].file_path)
    height, width, _ = frame.shape
    event_video_name = video.file_name.split('.')[0] + '_eventvideo.webm'
    event_video = cv2.VideoWriter(path + event_video_name, fourcc, 5, (width, height))

    for _, image in enumerate(movements):
        image = Image.objects.get(id=image.id)
        frame = cv2.imread(image.file_path)
        event_video.write(frame)
    event_video.release()

我这周遇到了同样的问题。在探索并浪费大量时间后,None 对我有用。 https://developer.mozilla.org/en-US/docs/Web/Media/Formats 请仔细阅读这篇文章,它肯定会对你们有所帮助,因为它对我有很大帮助,它将提供有关编解码器及其合适的容器类型及其浏览器兼容性的详细知识。

我推荐请看一遍这篇文章。

在尝试了许多合适的编解码器组合后,容器类型 'webm' 的编解码器 'VP90' 适合我。我正在使用 Ubuntu 18.04 LTS 和 Python3 以及 'opencv-python 4.2.0.34'

fourcc =  cv2.VideoWriter_fourcc(*'VP90')
            self.writer = cv2.VideoWriter('videoName.webm', fourcc, 20, (self.im_width,self.im_height)) 

不知为何我还是发现了这个错误信息,但如果它出现请忽略它。因为上面的代码片段将处理您的视频并将其成功保存为浏览器兼容的格式。

错误信息:

OpenCV: FFMPEG: tag 0x30395056/'VP90' is not supported with codec id 167 and format 'webm / WebM'

请忽略此错误消息,等待视频处理。 试试这个,它有效。 谢谢。

感谢您的回答,我使用多线程解决了这个问题。由于 write 方法需要更多时间,在此期间 opencv 可能会错过参考帧,所以我使用单独的线程进行读取和写入。然后用于在写入磁盘之前排队存储图像。