簡體   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