无法使用 OpenCV 和 Python 编写和保存视频文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38397964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't write and save a video file using OpenCV and Python
提问by lilian
I'm trying to process frames from a video stream, and it as a new video.
我正在尝试处理来自视频流的帧,并将其作为新视频。
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 ? I've been trying a lot of them.
我想我使用了错误的 Fourcc 值...我应该使用哪一个?我已经尝试了很多。
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
回答by Amar
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))
回答by Right leg
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. Don't try to write gray frames: the output would be empty.
另外,BGR
也只能直接写这样的VideoWriter
声明。不要尝试写入灰色帧:输出将为空。
回答by Gonzalo Garcia
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
. You should change the export format to MP4V
or the name of your output file to .avi
.
您遇到的问题是您试图以XVID
格式导出帧,但输出文件的名称以.mp4
. 您应该将导出格式更改为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
在这里您可以找到有关视频编解码器的更多信息
回答by M.Vu
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')
fourcc = cv2.VideoWriter_fourcc(*'XVID')
by:
经过:
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
fourcc = cv2.VideoWriter_fourcc(*'FMP4')
I tried and succeeded.
我尝试并成功了。
回答by werecow
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. Specifically, it wants .avi for the XVID codec.
此外,如果文件扩展名与使用的编解码器不匹配,OpenCV 似乎也会发出相同的警告。具体来说,它需要 .avi 用于 XVID 编解码器。
回答by user3900921
If you are using linux try this
如果您使用的是 linux 试试这个
fourcc = 0x00000021
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))
Fourcc = 0x00000021
out = cv2.VideoWriter('Videos/output.mp4',fourcc, fps, (1080,1080))
回答by Cole128
I wanted to save as a .mp4, and using *"mp4v"
turned out to be the right code, on Linux at least.
我想保存为 .mp4,并且使用*"mp4v"
结果证明是正确的代码,至少在 Linux 上是这样。
回答by Hariprasad
The size of the frame(width,height) you are giving, should match the size of the frame you want to save.
sofw = 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))
您提供的框架(宽度,高度)的大小应与您要保存的框架的大小相匹配。所以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))