简体   繁体   English

无法使用 OpenCV 和 Python 写入和保存视频文件

[英]Can't write and save a video file using OpenCV and Python

I'm trying to process frames from a video stream, and it as a new video.我正在尝试处理来自视频 stream 的帧,并将其作为新视频处理。

This is what I'm doing:这就是我正在做的事情:

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))

I keep getting:我不断得到:

OpenCV: FFMPEG: tag 0x44495658/'XVID' is not supported with codec id 13 and format 'mp4 / MP4 (MPEG-4 Part 14)'
OpenCV: FFMPEG: fallback to use tag 0x00000020/' ???'

I think I'm using the wrong fourcc value... Which one should I use?我想我使用了错误的fourcc值......我应该使用哪一个? I've been trying a lot of them.我已经尝试了很多。

I'm using Ubuntu 16.04, Python 2.7.11 and OpenCV 3.1.0我正在使用 Ubuntu 16.04、Python 2.7.11 和 OpenCV 3.1.0

Define the codec and create VideoWriter object like this定义编解码器并像这样创建 VideoWriter 对象

fourcc = cv2.VideoWriter_fourcc(*'MPEG')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

For Windows users对于 Windows 用户

I am using OpenCV 2 with Python 3.6, on Windows 10.我在 Windows 10 上使用 OpenCV 2 和 Python 3.6。

The 'XVID' codec, along with producing a .avi file, seems to be the most generic solution (if not the only one that works). 'XVID'编解码器以及生成.avi文件似乎是最通用的解决方案(如果不是唯一有效的解决方案)。

fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('test.avi', fourcc, 60, (320, 240))

In addition, only BGR can be directly written with such a VideoWriter declaration.另外,只有BGR可以直接用这样的VideoWriter声明来写。 Don't try to write gray frames: the output would be empty.不要尝试写入灰色帧:输出将为空。

The problem that you are having is that you are trying to export the frames in XVID format but the name of your output file is ending with .mp4 .您遇到的问题是您试图以XVID格式导出帧,但输出文件的名称以.mp4结尾。 You should change the export format to MP4V or the name of your output file to .avi .您应该将导出格式更改为MP4V或将输出文件的名称更改为.avi

fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))

alternative替代品

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('Videos/output.avi',fourcc, fps, (1080,1080))

here you can find more information about the video codecs在这里您可以找到有关视频编解码器的更多信息

The size of the frame(width,height) you are giving, should match the size of the frame you want to save.您提供的框架(宽度,高度)的大小应与您要保存的框架的大小相匹配。 so所以
fw = int(cap.get(3)) fh = int(cap.get(4)) print("w,h",fw,fh) out = cv2.VideoWriter('fb1.avi',cv2.VideoWriter_fourcc('X','V','I','D'), fps, (fw,fh))

If you want to save video by opencv in mp4 format, let如果你想通过opencv以mp4格式保存视频,让

Follow this link :按照这个链接

you should replace:你应该更换:

fourcc = cv2.VideoWriter_fourcc(*'XVID')

by:通过:

fourcc = cv2.VideoWriter_fourcc(*'FMP4')

I tried and succeeded.我尝试并成功了。

I had the same problem.我有同样的问题。 With me, it turned out that I had switched the height and width of the video, so the frame dimensions did not match the video specifications and as a result nothing was written.对我来说,原来是我切换了视频的高宽,所以帧尺寸不符合视频规格,结果什么也没写。 Make sure they match exactly.确保它们完全匹配。

Also, OpenCV seems to give that same warning if the file extension does not match the codec used.此外,如果文件扩展名与使用的编解码器不匹配,OpenCV 似乎也会发出相同的警告。 Specifically, it wants .avi for the XVID codec.具体来说,它需要 .avi 用于 XVID 编解码器。

If you are using linux try this如果您使用的是 linux 试试这个

fourcc = 0x00000021 Fourcc = 0x00000021
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080)) out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))

我想保存为 .mp4,结果证明使用*"mp4v"是正确的代码,至少在 Linux 上是这样。

Install K-Lite Mega Codec Pack: https://filehippo.com/download_klite_mega_codec/安装 K-Lite Mega Codec Pack: https : //filehippo.com/download_klite_mega_codec/
This error occurs because some codecs are not available by default in Windows media player.出现此错误是因为某些编解码器在 Windows 媒体播放器中默认不可用。 So by installing this software, the video works fine with same code.因此,通过安装此软件,视频可以使用相同的代码正常运行。

I tried using different cv2.VideoWriter_fourcc types, i found in the answers.我尝试使用不同的 cv2.VideoWriter_fourcc 类型,我在答案中找到了。 My system responded with :我的系统响应:

OpenCV: FFMPEG: tag 0x34504d46/'FMP4' is not supported with codec id 12 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: fallback to use tag 0x7634706d/'mp4v' OpenCV:FFMPEG:标签 0x34504d46/'FMP4' 不受编解码器 ID 12 和格式'mp4 / MP4(MPEG-4 Part 14)'的支持 OpenCV:FFMPEG:回退使用标签 0x7634706d/'mp4v'

so i used what FFMPEG suggested in the error messgae.所以我使用了 FFMPEG 在错误消息中建议的内容。 I set fourcc = 0x7634706d and it worked.我设置了fourcc = 0x7634706d并且它起作用了。 Thanks guys :)多谢你们 :)

On a mac在 Mac 上

writer = cv2.VideoWriter('mysupervideo.mp4', cv2.VideoWriter.fourcc(*'mp4v'), 20, (width, height))

Works better效果更好

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

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