嗨,我正在尝试使用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库,我只想让这个绘图过滤器正常工作,下面是我的脚本:
#!/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.怎么会有这么多反斜杠?请给我任何关于如何进行的建议。
谢谢
发布于 2018-01-26 07:17:57
我最终找到了正确的语法。下面是一个有用的例子
#!/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值的引号。
希望这能帮上忙。
发布于 2018-08-25 09:25:33
步骤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")
导入子进程:用于调用FFMPEG命令。
子流程是python提供的内置模块。
https://stackoverflow.com/questions/48123899
复制相似问题