我正在尝试通过Python和FFMPEG从网络摄像头保存帧,但是视频变成了快速的方式
我从我的网络摄像头中获得CV2
图像的流,并希望将其保存到视频文件中。在使用cv2.videowriter()
播放了一点之后,事实证明,使用ffmpeg
将提供更多选项,并且 - 显然,在此处的一些线程之后 - 。所以我给了 vidgear python python python python库似乎效果很好。
但是有一个接收器:我的网络摄像头提供了可变的帧率,大部分时间在10到30 fps之间。保存这些帧时,视频文件变得太快了,例如快速观看。在视频中,一个实时分钟仅变成几秒钟。
我尝试使用FFMPEG的-Framerate
和/或-r
参数的各种组合,但没有运气。这是我现在正在使用的命令:
ffmpeg -y -f rawvideo -vcodec rawvideo -s 1920x1080 -pix_fmt bgra -framerate 25.0 -i - -vcodec libx265 -crf 25 -r 25 -preset fast <output_video_file>
对于记录,我正在创建trightgear
类,从vidgear库中创建类似的类别:
video_params = {
"-vcodec": "libx265",
"-crf": 25,
"-input_framerate": 25,
"-r": 25,
}
WriteGear(output_filename=video_file, logging=True, **video_params)
有什么想法我在这里做错了什么,我需要如何称呼ffmpeg?
I get a stream of cv2
images from my webcam and want to save it to a video file. After playing a bit with cv2.VideoWriter()
it turned out that using ffmpeg
would provide more options and - apparently, following a few threads here on SO - lead to better results. So I gave the VidGear Python library a try, and it seems to work fine.
There is one catch though: My webcam provides a variable framerate, most of the time between 10 and 30 FPS. When saving these frames the video file becomes way too fast, like watching in fast-forward. One real-time minute becomes only a few seconds in the video.
I tried to play with various combinations of the ffmpeg's -framerate
and/or -r
parameters, but without luck. Here is the command I am using right now:
ffmpeg -y -f rawvideo -vcodec rawvideo -s 1920x1080 -pix_fmt bgra -framerate 25.0 -i - -vcodec libx265 -crf 25 -r 25 -preset fast <output_video_file>
For the records, I am creating the WriteGear
class from the VidGear library like this:
video_params = {
"-vcodec": "libx265",
"-crf": 25,
"-input_framerate": 25,
"-r": 25,
}
WriteGear(output_filename=video_file, logging=True, **video_params)
Any ideas what I am doing wrong here and how I need to call ffmpeg?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我现在通过使用
cv2.videowriter()
而不是vidgear库解决了我的问题。不知道为什么,但是我无法以适当的方式使后者工作 - 要么是我不正确使用它,要么库被损坏(对于我的用例)。无论哪种方式,在使用OPENCV解决方案之后,事实证明,使用
videoWriter
类并从Web Cam中提供FPS值,现在一切正常。对于记录,不过要注意的一件事:即使为我的平台找到了正确的编解码器并仔细检查要保存的图像的尺寸,我仍然最终得到了一个“空”(1K).mp4视频文件。原因是我网络摄像头的所有帧都带有4个频道,但是
videoWriter
类显然期望带有3个频道的帧,默默地丢弃了所有其他“无效”帧。使用以下代码,我能够转换我的4通道图像:Ok, I solved my issue now by using
cv2.VideoWriter()
instead of the VidGear library. Don't know why, but I was not able to get the latter working in a proper way - either it was me not using it correctly, or the library is broken (for my use case).Either way, after playing around with the OpenCV solution, it turned out that using the
VideoWriter
class and providing the FPS value from the web cam everything works quite well now.For the records, one thing to note though: Even after finding a correct codec for my platform and double-checking the dimensions of the images to be saved, I still ended up with an "empty" (1k) .mp4 video file. The reason was that all frames from my webcam came with 4 channels, but the
VideoWriter
class apparently expects frames with 3 channels instead, silently dropping all other, "invalid" frames. With the following code I was able to convert my 4-channel images: