python opencv videowrite 不写视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29320976/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:23:50  来源:igfitidea点击:

python opencv videowrite doesn't write video

pythonopencvvideo

提问by Zachzhao

I just started programming python with opencv. I used the following page from opencv 3.0.0 tutorial

我刚开始用 opencv 编程 python。我使用了 opencv 3.0.0 教程中的以下页面

Tutorial at read the docs

阅读文档的教程

When I tried to use the example that saves videos, it doesn't work. It displays the content from the webcam, and also creates a file called output.avi, but when I checked the size of ouput.avi, it was zero bytes. I also tried using different codecs, like YUY2

当我尝试使用保存视频的示例时,它不起作用。它显示来自网络摄像头的内容,还创建了一个名为 output.avi 的文件,但是当我检查 ouput.avi 的大小时,它是零字节。我也尝试使用不同的编解码器,比如 YUY2

I use python 2.7.8 and opencv 3.0.0 and windows 8.1

我使用 python 2.7.8 和 opencv 3.0.0 和 windows 8.1

回答by Scott

Make sure you are using the correct fourcc 4-byte code. The example on the tutorial has:

确保您使用的是正确的fourcc 4 字节代码。本教程中的示例有:

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

This 'XVID' code may only work for linux. The documentation above the example states (in the tutorial): "In Windows: DIVX (More to be tested and added)." So if you haven't, try replacing the fourcc line above with:

此“XVID”代码可能仅适用于 linux。示例上方的文档说明(在教程中):“在 Windows 中:DIVX(更多待测试和添加)。” 因此,如果您还没有,请尝试将上面的 Fourcc 行替换为:

fourcc = cv2.VideoWriter_fourcc(*'DIVX')

I use cv2.VideoWriter on linux quite often and it always works. So if the above doesn't work you can always try it on a linux vm.

我经常在 linux 上使用 cv2.VideoWriter 并且它总是有效。因此,如果上述方法不起作用,您可以随时在 linux vm 上尝试。

回答by One Oasis

It should be a problem with the codec you're using.

您使用的编解码器应该有问题。

Have you tried:

你有没有尝试过:

cv.CV_FOURCC('i', 'Y', 'U', 'V')

cv.CV_FOURCC('i', 'Y', 'U', 'V')

This one worked for me.

这个对我有用。

回答by Lamar Latrell

Replacing:

更换:

fourcc = cv2.VideoWriter_fourcc(*'XVID')

With:

和:

fourcc = cv2.VideoWriter_fourcc('M','J','P','G')

Worked for me...

为我工作...

More generally:

更普遍:

Look up the fourcc code of the video compression format you're after here, and whatever the code is - for instance 'FMP4' for FFMpeg - plug it in in the following manner:

此处查找您要使用的视频压缩格式的 Fourcc 代码,无论代码是什么——例如 FFMpeg 的“FMP4”——以下列方式插入:

cv2.VideoWriter_fourcc('F','M','P','4')

回答by Pijar

I had the same problem and i solved it by specifying the video output resolution to exactly the same as input:

我遇到了同样的问题,我通过将视频输出分辨率指定为与输入完全相同来解决它:

cap = cv2.VideoCapture('vtest.avi')
...
out = cv2.VideoWriter('output.avi',fourcc, 20.0,(int(cap.get(3)),int(cap.get(4))))

Of course make sure you got ffmpeg installed and working.

当然,请确保您已安装并运行 ffmpeg。

回答by schwang

On windows 7 and using Python 2.7 and OpenCV 2.4.8, I experienced the fact that if the file NAME is "output.mpg"would not write.

在 Windows 7 和使用 Python 2.7 和 OpenCV 2.4.8 上,我遇到了这样一个事实:如果文件 NAME 是"output.mpg"不会写入的。

I solved it by changing to "output.avi".

我通过更改为"output.avi".

回答by mmmFood

In my case, I thought the codec was an obstacle but it wasn't. Instead, adjusting the dimensions being consumed by videoWriter() did the trick:

就我而言,我认为编解码器是一个障碍,但事实并非如此。相反,调整 videoWriter() 使用的尺寸可以解决问题:

(ok, frame) = cv2.VideoCapture(videoPath).read()

 fourcc = cv2.VideoWriter_fourcc(*'XVID')
 out = cv2.VideoWriter(output, fourcc, 10.0, (1280, 720))

(1280,720) was used because frame.shape from my video outputs (1280, 720, 3). It made avi to mp4 and vice versa possible. Didn't have to worry about reconciling the codec.

使用 (1280,720) 是因为我的视频输出 (1280, 720, 3) 中的 frame.shape。它使 avi 转 mp4 成为可能,反之亦然。不必担心协调编解码器。

回答by CCVT

I changed the Video writer size to my screen resolution size and it worked.

我将视频编写器大小更改为我的屏幕分辨率大小并且它起作用了。

here is the solution.

这是解决方案。

out = cv2.VideoWriter("output.avi", fourcc, 5.0, (1920, 1080))

回答by pandaman1234

I have the exact same issue. I am using OpenCV in C++, but I believe you can still pass -1instead of choosing the codec so you can have a drop down menu of the available codecs even in python. From there I tried all different codecs and just like Leonard Zhou, the IYUVcodec was the one that worked for me. Also note that it was the only one that worked even though I could use XVID just fine on another machine with the same code and same codec installer.

我有完全相同的问题。我在 C++ 中使用 OpenCV,但我相信您仍然可以通过-1而不是选择编解码器,因此即使在 python 中,您也可以拥有可用编解码器的下拉菜单。从那里我尝试了所有不同的编解码器,就像 Leonard Zhou 一样,IYUV编解码器对我有用。另请注意,即使我可以在另一台具有相同代码和相同编解码器安装程序的机器上很好地使用 XVID,它也是唯一可以工作的。

EDIT:What I suggested worked as a patch, the main issue was solved on my end by adding the opencv_ffmpegdll in the executable's folder.

编辑:我建议作为补丁工作,通过opencv_ffmpeg在可执行文件的文件夹中添加dll解决了主要问题。

回答by nchaumont

Check the resolution of your images! I was trying with odd-shaped images (1284x709) and ended up with unreadable videos of 1k in size. After resizing my image to the closest 'common' resolution: image = cv2.resize(image, (1280,720)), it worked just fine.

检查图像的分辨率!我尝试使用奇怪形状的图像 (1284x709),但最终得到了 1k 大小的不可读视频。将我的图像调整为最接近的“通用”分辨率后: image = cv2.resize(image, (1280,720)),它工作得很好。