繁体   English   中英

使用ffmpeg-python库捕获网络摄像头

[英]Capture webcam using ffmpeg-python library

嗨,我正尝试使用ffmpeg-python包装器库( https://github.com/kkroening/ffmpeg-python )用python捕获网络摄像头流,我有一个有效的ffmpeg命令,它是:

ffmpeg -f v4l2 -video_size 352x288 -i /dev/video0 -vf "drawtext='fontfile=fonts/FreeSerif.ttf: text=%{pts} : \
x=(w-tw)/2: y=h-(2*lh): fontcolor=white: box=1: boxcolor=0x00000000@1'" -an -y -t 15 videotests/out_localtime8.mp4

这将以352x288的分辨率捕获15s的视频,并在视频的底部中心写入一个时间戳。

为了使用ffmpeg-python库,我只是试图仅使drawtext过滤器起作用,这是我的脚本:

#!/usr/bin/env python

import ffmpeg
stream = ffmpeg.input('videotests/example.mov')
stream = ffmpeg.filter_(stream,'drawtext',("fontfile=fonts/FreeSerif.ttf:text=%{pts}"))
stream = ffmpeg.output(stream, 'videotests/output4.mp4')
ffmpeg.run(stream)

错误是

[Parsed_drawtext_0 @ 0x561f59d494e0] Either text, a valid file or a timecode must be provided
[AVFilterGraph @ 0x561f59d39080] Error initializing filter 'drawtext' with args 'fontfile\\\=fonts/FreeSerif.ttf\\\:text\\\=%{pts}'
Error initializing complex filters.
Invalid argument

以上似乎至少达到了ffmpeg,但是参数的格式不正确,如何更正它们?

另外,当我尝试将参数拆分为仅传递其中一个参数时,会收到另一个错误,如下所示:

stream = ffmpeg.filter_(stream,'drawtext',('text=%{pts}'))

错误是

subprocess.CalledProcessError: Command '['ffmpeg', '-i', 'videotests/example.mov', '-filter_complex', "[0]drawtext=(\\\\\\\\\\\\\\'text\\\\\\\\\\\\=%{pts}\\\\\\\\\\\\\\'\\,)[s0]", '-map', '[s0]', 'videotests/output4.mp4']' returned non-zero exit status 1.

为什么会有这么多的反斜杠? 请提供任何有关如何进行的建议。

谢谢

我最终得出了正确的语法。 这是一个有效的例子

#!/usr/bin/env python

import ffmpeg
stream = ffmpeg.input('videotests/example.mov')
stream = ffmpeg.filter_(stream,'drawtext',fontfile="fonts/hack/Hack-Regular.ttf",text="%{pts}",box='1', boxcolor='0x00000000@1', fontcolor='white')
stream = ffmpeg.output(stream, 'videotests/output6.mp4')
ffmpeg.run(stream)

语法是

ffmpeg.filter_(<video stream name>,'<filter name>',filter_parameter_name='value',<filter_parameter_name>=value)

必要时在filter_parameter_name值中使用引号。

希望这对某人有帮助。

步骤1:为ffmpeg设置环境变量。

第2步:下面的代码将有助于使用ffmpeg在python中捕获图像和视频及其当前日期和时间。

 import subprocess from datetime import datetime import time class Webcam: def Image(self): try: user = int(input("How many Images to be captured:")) except ValueError: print("\\nPlease only use integers") for i in range (user): subprocess.call("ffmpeg -f vfwcap -vstats_file c:/test/log"+ datetime.now().strftime("_%Y%m%d_%H%M%S") +".txt -t 10 -r 25 -i 0 c:/test/sample"+ datetime.now().strftime("_%Y%m%d_%H%M%S") +".jpg") time.sleep(3) def Video(self): try: user = int(input("How many videos to be captured:")) except ValueError: print("\\nPlease only use integers") for i in range (user): subprocess.call("ffmpeg -f vfwcap -vstats_file c:/test/log"+ datetime.now().strftime("_%Y%m%d_%H%M%S") +".txt -t 10 -r 25 -i 0 c:/test/sample"+ datetime.now().strftime("_%Y%m%d_%H%M%S") +".avi") time.sleep(5) Web=Webcam() print ("press 1 to capture image") print ("Press 2 to capture video") choose = int(input("Enter choice:")) if choose == 1: Web.Image() elif choose == 2: Web.Video() else: print ("wrong choose") 

import子进程:用于调用FFMPEG命令。

子进程是python提供的内置模块

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM