使用ffmpegpython库捕获网络摄像头

2024-09-28 22:10:11 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在尝试使用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过滤器工作,下面是我的脚本:

^{pr2}$

错误是

[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.

怎么会有这么多反斜杠?请给我一些建议。在

谢谢你


Tags: text参数视频错误fontserrorttffilter
2条回答

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

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

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")

和13;
和13;

导入子进程:用于调用FFMPEG命令。在

subprocess是python提供的内置模块

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

#!/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)

语法是

^{pr2}$

如有必要,请为filter_parameter_name值使用引号。在

希望这对某人有帮助。在

相关问题 更多 >