简体   繁体   English

python OpenCV中mp4视频的编解码器是什么

[英]what is the codec for mp4 videos in python OpenCV

fourcc = cv2.cv.CV_FOURCC(*'XVID')

The above line is used for avi video.上面的行用于 avi 视频。 In the same fashion, which codec do we use for mp4 videos in Ubuntu?以同样的方式,我们在 Ubuntu 中为 mp4 视频使用哪种编解码器?

You can also use mp4v您也可以使用mp4v

fourcc = cv2.cv.CV_FOURCC(*'mp4v')

where the videowriter should look like this:视频作者应该是这样的:

out = cv2.VideoWriter('output.mp4',fourcc, 15, size)

But there are more codecs available for mp4.但是还有更多可用于 mp4 的编解码器。 You can see the list of them by setting fourcc = -1 , it will show a list like this:您可以通过设置fourcc = -1来查看它们的列表,它将显示如下列表:

OpenCV: FFMPEG: format mp4 / MP4 (MPEG-4 Part 14)
fourcc tag 0x7634706d/'mp4v' codec_id 000C
fourcc tag 0x31637661/'avc1' codec_id 001B
fourcc tag 0x33637661/'avc3' codec_id 001B
fourcc tag 0x31766568/'hev1' codec_id 00AD
fourcc tag 0x31637668/'hvc1' codec_id 00AD
fourcc tag 0x7634706d/'mp4v' codec_id 0002
fourcc tag 0x7634706d/'mp4v' codec_id 0001
fourcc tag 0x7634706d/'mp4v' codec_id 0007
fourcc tag 0x7634706d/'mp4v' codec_id 003D
....

All of them supports mp4 but h264 is supported by Web browsers if you want to serve the video into the web.它们都支持 mp4,但如果您想将视频提供给网络,则 Web 浏览器支持h264

The codec is H.264.编解码器是 H.264。

One of these should work for you:其中之一应该适合您:

fourcc = cv2.cv.CV_FOURCC(*'H264')
#or 
#fourcc = cv2.cv.CV_FOURCC(*'X264')

However, I should warn you that you'll probably need to have ffmpeg and the x264 libraries installed so since you are in Ubuntu, try doing this command in the terminal :但是,我应该警告您,您可能需要安装ffmpegx264库,因为您使用的是 Ubuntu,请尝试在终端中执行以下命令:

sudo apt-get install ffmpeg x264 libx264-dev

Also, check out this link from OpenCV tutorials for more details as to the kinds of FourCC codes available for your platform.此外,请查看 OpenCV 教程中的此链接,了解有关可用于您的平台的 FourCC 代码类型的更多详细信息。

In the above link, it says X264 is the FourCC code you should use, but switch between them until you get it to work.在上面的链接中,它说X264是您应该使用的 FourCC 代码,但在它们之间切换,直到您让它工作为止。

fourcc = cv2.VideoWriter_fourcc('m','p','4','v')

似乎总是有效。

This is an old question.这是一个老问题。 But, if anyone is facing an issue lately using the codec who can't get a saved video.但是,如果有人最近在使用无法获取已保存视频的编解码器时遇到问题。 They can use 0X00000021 as the codec value for OpenCV 3 and later.他们可以使用0X00000021作为 OpenCV 3 及更高版本的编解码器值。

I had to reinstall ffmpeg and compile opencv from source in order to get this working.我必须重新安装 ffmpeg 并从源代码编译 opencv 才能使其正常工作。 I documented the steps in this blog post .我在这篇博文中记录了这些步骤。

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

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