如何在Windows中使用ffmpeg抓取笔记本电脑摄像头视频 [英] How to grab laptop webcam video with ffmpeg in windows
问题描述
我有一个很小的python程序,使用ffmpeg的子进程捕获来自linux网络摄像头的短视频(至少是具有内置网络摄像头的笔记本电脑)。
现在我正在尝试编写相同的程序来捕获Windows中的网络摄像头,我知道我不能使用在linux中运行良好的通用/ dev / video0,但是我觉得像命名它集成摄像头应该是足够的,但它失败。
这是我的linux代码(可以):
来自子进程导入的$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ video0'
timestamp = datetime.now()。strftime('%Y%m%d-%H%M%S')
filename = timestamp +'something.mkv'#生成更复杂在实际的代码中,但不重要
ffmpeg_cmd ='ffmpeg -t {} -an -i {} -c:v libx264 -preset veryslow -crf 25 {}'。fo rmat(秒,凸轮,文件名).split()
p =调用(ffmpeg_cmd)
返回文件名如果p == 0否则False
如果__name__ =='__main__'
record_webcam(sys.argv [1])$ b $ b
我看过ffmpeg的文档并试图搜索解决方案,但到目前为止,我失去了...
我知道集成相机只能在一些笔记本电脑而不是其他的,它不会捕获连接的其他摄像头,但这对我的用例是足够的...但是如果你想要一个挑战,我也想知道如何将它应用到任何带相机的Windows-pc,而不管它叫什么。
另外,使用像OpenCV这样的python工具,这样做比较容易或者更为推荐吗?
提前谢谢!
编辑:如果任何人根据@Mulvya的评论感兴趣,我会回答自己的问题,但如果有人仍然可以向我解释关于OpenCV的部分,我仍然希望听到。 ..
在这里跟进问题: ffmpeg通过python子进程找不到相机
我做了...基于什么@Mulvya评论,我能够列出所有相机的代码,并提取一个命令,我现在有一个不同的问题,但我会问另一个问题。同时,如果任何人有兴趣通过python和ffmpeg在windows上自动选择第一个可用的相机,我的解决方案是这样的:
Popen,PIPE
list_cmd ='ffmpeg -list_devices true -f dshow -i dummy'.split()
p = Popen(list_cmd,stderr = PIPE)
for iter(p.stderr.readline,''):
if flagcam:
cam = re.search('。*',line.decode(encoding ='UTF- 8'))。group(0)
cam ='video ='+ cam if cam else''
flagcam = False
elif'DirectShow video devices'.encode(encoding ='UTF -8')行:
flagcam = True
elif'立即退出request'.encode(编码='UTF-8')行:
break
变量cam现在保存在Windows上的DirectShow中的凸轮名称
追踪任务在此处,如果有人想帮助编辑: 也解决了
I have a small python program that works very well to capture short videos from webcams in linux (at least for laptops that have built-in webcams) using a sub-process with ffmpeg.
Now i'm trying to write the same program to capture webcams in windows, and i know i can't use the generic "/dev/video0" that works pretty well in linux, but i thought something like naming it "Integrated Camera" should be enough, but it fails.
Here's my linux code (that works):
import sys
from subprocess import call
from datetime import datetime
def record_webcam(seconds):
cam = '/dev/video0'
timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')
filename = timestamp + 'something.mkv' #generated with more complexity in the actual code, but that isn't important
ffmpeg_cmd = 'ffmpeg -t {} -an -i {} -c:v libx264 -preset veryslow -crf 25 {}'.format(seconds, cam, filename).split()
p = call(ffmpeg_cmd)
return filename if p == 0 else False
if __name__ == '__main__':
record_webcam(sys.argv[1])
I have looked at the documentation for ffmpeg and tried to search for solution but so far i'm lost...
I know that "Integrated Camera"s are only available on some laptops and not others, and that it won't capture other cameras connected, but it's enough for my use case... but if you want a challenge I would also like to know how to apply it to any windows-pc with a camera regardless of what it's called.
Also, is it easier or more recommended to do what i'm trying here only with python tools, like OpenCV?
Thanks in advance! Edit: I answered my own question with a partial solution if anyone is interested based on a comment from @Mulvya, but if anyone can still explain to me the part about OpenCV I would still like to hear it...
Follow up question here: ffmpeg through python subprocess fails to find camera
I did it... based on what @Mulvya commented, i was able to list all cameras withing the code and extract a command from them, i now have a different issue about that, but i will ask another question for it. In the meanwhile, if anyone is interested in automatically selecting the first available camera on windows through python and ffmpeg, my solution is this:
import re
from subprocess import Popen, PIPE
list_cmd = 'ffmpeg -list_devices true -f dshow -i dummy'.split()
p = Popen(list_cmd, stderr=PIPE)
for line in iter(p.stderr.readline,''):
if flagcam:
cam = re.search('".*"',line.decode(encoding='UTF-8')).group(0)
cam = 'video=' + cam if cam else ''
flagcam = False
elif 'DirectShow video devices'.encode(encoding='UTF-8') in line:
flagcam = True
elif 'Immediate exit requested'.encode(encoding='UTF-8') in line:
break
the variable "cam" now holds the name of the cam as it is in DirectShow on windows
Follow up question here if anyone wants to help EDIT: also solved
这篇关于如何在Windows中使用ffmpeg抓取笔记本电脑摄像头视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!