cv2.VideoWriter 不会使用 fourcc h.264 写入文件(使用 logitech c920,python 2.7,windows 8)
cv2.VideoWriter will not write file using fourcc h.264 (with logitech c920, python 2.7, windows 8)
我是 python (2.7) 和 opencv (3.0)(以及视频 streaming/writing 的新手)所以请原谅。
我使用罗技 c920 作为我的网络摄像头,它可以流式传输以 h264 格式压缩的视频,因此我正在尝试编写一个简单的应用程序来设置 VideoCapture 实例的 4 个属性(fourcc 为 h264;宽度为 1920;高度到 1080;fps 到 30),然后将视频录制到名为 test.mp4 的上一级目录中,并在我的屏幕上显示录制内容。这是代码:
import sys
import cv2 as cv
cap = cv.VideoCapture(0)
fourcc = cv.VideoWriter_fourcc('H','2','6','4')
cap.set(6, fourcc)
cap.set(3,1920)
cap.set(4,1080)
cap.set(5, 30)
vid = cv.VideoWriter('../test.mp4', fourcc, 20.0, (640,480))
print vid.isOpened() #returns false :(
while (cap.isOpened()):
ret, frame = cap.read()
if (ret == True):
#gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
vid.write(frame)
cv.imshow('window', frame)
if (cv.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
vid.release()
cv.destroyWindow('window')
cv.imshow('window',frame) 工作正常,属性都设置好了;但是, vid.isOpened() return false 很明显我在上面做错了什么。如果我为 fourcc 传递 -1,我可以从编解码器列表中选择,并且 i420 可用并表示(对于罗技相机)和 vid.isOpened() returns true 如果我更改文件从 mp4 到 avi 的扩展(我想这意味着 i420 不能存储为 .avi ?),但是,test.avi 总是很大而且看起来很原始,几秒钟的测试视频有 100MB,并且不会打开。
任何帮助都会很好,非常感谢
- 请记住,OpenCV 会自动解压缩来自网络摄像头的传入 H264 流。您不能传递压缩流。您必须重新压缩。
- 由于-1 的编解码器 ID 会提示您选择编解码器,因此我假设您使用的是 Windows。在Windows上,OpenCV使用ffmpeg or "Video for Windows" backends. Unfortunately, it seems you cannot tell when OpenCV uses which backend. The dialog asking to select an encoder looks like the VfW dialog. There are no sane h264 VfW encoders, you probably did not install a hackish one like x264vfw,所以这里不能选择h264。
- AVI container does not support b-Frames。 H264 默认使用 b 帧。您不能从 OpenCV 中更改这些编码器设置。因此,您不应为 H264 编码视频选择 AVI。
- 为了保存到另一个容器,应该使用ffmpeg。我只能猜测 OpenCV 明智地使用后端并基于容器。您应该能够将 h264 编码为 MKV 或 MP4。请记住,会发生某种 "FOURCC to ffmpeg-Encoder-ID" translation。
如果您通过 pip install opencv-python 安装了此包,则 x264 不支持编码,因为它受 GPL 许可。升级 FFmpeg 无济于事,因为 opencv-python 附带了自己的 FFmpeg。
您必须手动编译 OpenCV 以获得对 H264 编码的支持。
Github 上的相关问题:https://github.com/skvark/opencv-python/issues/100#issuecomment-394159998
所以到目前为止我发现的解决方法不是那么有效,但它适用于 Ubuntu 16.04。定期写视频后,我用H264编码器再次转换,最终视频大小比我单独用opencv得到的小很多。
解决方法如下:
import os # We will use it to access the terminal
# Write the video normally using mp4v and make the extension to be '.mp4'
# Note: the output using "mp4v" coder may be efficient for you, if so, you do not need to add the command below
cv2.VideoWriter('Video.mp4',cv2.VideoWriter_fourcc(*"mp4v"), NewFPS, (width,height))
# When your video is ready, just run the following command
# You can actually just write the command below in your terminal
os.system("ffmpeg -i Video.mp4 -vcodec libx264 Video2.mp4")
我是 python (2.7) 和 opencv (3.0)(以及视频 streaming/writing 的新手)所以请原谅。
我使用罗技 c920 作为我的网络摄像头,它可以流式传输以 h264 格式压缩的视频,因此我正在尝试编写一个简单的应用程序来设置 VideoCapture 实例的 4 个属性(fourcc 为 h264;宽度为 1920;高度到 1080;fps 到 30),然后将视频录制到名为 test.mp4 的上一级目录中,并在我的屏幕上显示录制内容。这是代码:
import sys
import cv2 as cv
cap = cv.VideoCapture(0)
fourcc = cv.VideoWriter_fourcc('H','2','6','4')
cap.set(6, fourcc)
cap.set(3,1920)
cap.set(4,1080)
cap.set(5, 30)
vid = cv.VideoWriter('../test.mp4', fourcc, 20.0, (640,480))
print vid.isOpened() #returns false :(
while (cap.isOpened()):
ret, frame = cap.read()
if (ret == True):
#gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
vid.write(frame)
cv.imshow('window', frame)
if (cv.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
vid.release()
cv.destroyWindow('window')
cv.imshow('window',frame) 工作正常,属性都设置好了;但是, vid.isOpened() return false 很明显我在上面做错了什么。如果我为 fourcc 传递 -1,我可以从编解码器列表中选择,并且 i420 可用并表示(对于罗技相机)和 vid.isOpened() returns true 如果我更改文件从 mp4 到 avi 的扩展(我想这意味着 i420 不能存储为 .avi ?),但是,test.avi 总是很大而且看起来很原始,几秒钟的测试视频有 100MB,并且不会打开。
任何帮助都会很好,非常感谢
- 请记住,OpenCV 会自动解压缩来自网络摄像头的传入 H264 流。您不能传递压缩流。您必须重新压缩。
- 由于-1 的编解码器 ID 会提示您选择编解码器,因此我假设您使用的是 Windows。在Windows上,OpenCV使用ffmpeg or "Video for Windows" backends. Unfortunately, it seems you cannot tell when OpenCV uses which backend. The dialog asking to select an encoder looks like the VfW dialog. There are no sane h264 VfW encoders, you probably did not install a hackish one like x264vfw,所以这里不能选择h264。
- AVI container does not support b-Frames。 H264 默认使用 b 帧。您不能从 OpenCV 中更改这些编码器设置。因此,您不应为 H264 编码视频选择 AVI。
- 为了保存到另一个容器,应该使用ffmpeg。我只能猜测 OpenCV 明智地使用后端并基于容器。您应该能够将 h264 编码为 MKV 或 MP4。请记住,会发生某种 "FOURCC to ffmpeg-Encoder-ID" translation。
如果您通过 pip install opencv-python 安装了此包,则 x264 不支持编码,因为它受 GPL 许可。升级 FFmpeg 无济于事,因为 opencv-python 附带了自己的 FFmpeg。
您必须手动编译 OpenCV 以获得对 H264 编码的支持。
Github 上的相关问题:https://github.com/skvark/opencv-python/issues/100#issuecomment-394159998
所以到目前为止我发现的解决方法不是那么有效,但它适用于 Ubuntu 16.04。定期写视频后,我用H264编码器再次转换,最终视频大小比我单独用opencv得到的小很多。
解决方法如下:
import os # We will use it to access the terminal
# Write the video normally using mp4v and make the extension to be '.mp4'
# Note: the output using "mp4v" coder may be efficient for you, if so, you do not need to add the command below
cv2.VideoWriter('Video.mp4',cv2.VideoWriter_fourcc(*"mp4v"), NewFPS, (width,height))
# When your video is ready, just run the following command
# You can actually just write the command below in your terminal
os.system("ffmpeg -i Video.mp4 -vcodec libx264 Video2.mp4")