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

时间:2018-01-06 03:07:07

标签: python ffmpeg timestamp webcam

您好我正在尝试使用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捕获15个视频,并在视频的底部中心写入时间戳。

要使用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.

怎么会有这么多的反斜杠?有关如何继续的任何建议请。

谢谢

2 个答案:

答案 0 :(得分:0)

我最终得出了正确的语法。这是一个工作示例

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

语法是

class Car(models.Model):

def fileLocation(instance, filename):
    return 'static/media/cars/{0}/{1}'.format(instance.agent.username, os.path.basename(filename))

make = models.CharField(max_length=100)
model = models.CharField(max_length=100)
year = models.IntegerField()
car_registration = models.CharField(max_length=100, unique=True) 
insurance_exp = models.DateField(max_length=100)
cost_per_day = models.IntegerField()
description = models.TextField(max_length=1000) 
agent = models.ForeignKey(Agent, on_delete=models.CASCADE)
image1 = models.ImageField(upload_to=fileLocation)
image2 = models.ImageField(upload_to=fileLocation)
image3 = models.ImageField(upload_to=fileLocation)
image4 = models.ImageField(upload_to=fileLocation)
image5 = models.ImageField(upload_to=fileLocation)
available = models.BooleanField()
added_date = models.DateField(default = timezone.now()) 

必要时,使用filter_parameter_name值的引号。

希望这有助于某人。

答案 1 :(得分:0)

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

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

add_action('wp_enqueue_scripts' , function(){ 
  wp_localize_script('jquery', 'config_var', URL );
});

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

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