Set User Agent for FFmpeg calls (#4555)

* Set User Agent for FFmpeg calls

* Allow to use shell-like string args

* Add test for arg as string with space
This commit is contained in:
Felipe Santos 2022-11-30 19:53:45 -03:00 committed by GitHub
parent 5ad391977e
commit 4523c9b06d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 6 deletions

View File

@ -60,6 +60,9 @@
"editor.formatOnPaste": false, "editor.formatOnPaste": false,
"editor.formatOnSave": true, "editor.formatOnSave": true,
"editor.formatOnType": true, "editor.formatOnType": true,
"python.testing.pytestEnabled": false,
"python.testing.unittestEnabled": true,
"python.testing.unittestArgs": ["-v", "-s", "./frigate/test"],
"files.trimTrailingWhitespace": true, "files.trimTrailingWhitespace": true,
"eslint.workingDirectories": ["./web"], "eslint.workingDirectories": ["./web"],
"[json][jsonc]": { "[json][jsonc]": {

3
.gitignore vendored
View File

@ -2,7 +2,8 @@
*.pyc *.pyc
*.swp *.swp
debug debug
.vscode .vscode/*
!.vscode/launch.json
config/config.yml config/config.yml
models models
*.mp4 *.mp4

12
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Launch Frigate",
"type": "python",
"request": "launch",
"module": "frigate",
"justMyCode": true
}
]
}

View File

@ -30,7 +30,7 @@ build: version amd64 arm64 armv7
push: build push: build
docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):${GITHUB_REF_NAME}-$(COMMIT_HASH) . docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):${GITHUB_REF_NAME}-$(COMMIT_HASH) .
run_tests: frigate run_tests: local
docker run --rm --entrypoint=python3 frigate:latest -u -m unittest docker run --rm --entrypoint=python3 frigate:latest -u -m unittest
docker run --rm --entrypoint=python3 frigate:latest -u -m mypy --config-file frigate/mypy.ini frigate docker run --rm --entrypoint=python3 frigate:latest -u -m mypy --config-file frigate/mypy.ini frigate

View File

@ -142,7 +142,7 @@ birdseye:
# Optional: ffmpeg configuration # Optional: ffmpeg configuration
ffmpeg: ffmpeg:
# Optional: global ffmpeg args (default: shown below) # Optional: global ffmpeg args (default: shown below)
global_args: -hide_banner -loglevel warning global_args: -hide_banner -loglevel warning -user_agent "FFmpeg Frigate"
# Optional: global hwaccel args (default: shown below) # Optional: global hwaccel args (default: shown below)
# NOTE: See hardware acceleration docs for your specific device # NOTE: See hardware acceleration docs for your specific device
hwaccel_args: [] hwaccel_args: []

View File

@ -32,6 +32,7 @@ from frigate.ffmpeg_presets import (
parse_preset_output_record, parse_preset_output_record,
parse_preset_output_rtmp, parse_preset_output_rtmp,
) )
from frigate.version import VERSION
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -357,7 +358,13 @@ class BirdseyeCameraConfig(BaseModel):
) )
FFMPEG_GLOBAL_ARGS_DEFAULT = ["-hide_banner", "-loglevel", "warning"] FFMPEG_GLOBAL_ARGS_DEFAULT = [
"-hide_banner",
"-loglevel",
"warning",
"-user_agent",
f"FFmpeg Frigate/{VERSION}",
]
FFMPEG_INPUT_ARGS_DEFAULT = [ FFMPEG_INPUT_ARGS_DEFAULT = [
"-avoid_negative_ts", "-avoid_negative_ts",
"make_zero", "make_zero",

View File

@ -1,5 +1,5 @@
import unittest import unittest
from frigate.config import FrigateConfig from frigate.config import FFMPEG_INPUT_ARGS_DEFAULT, FrigateConfig
from frigate.ffmpeg_presets import parse_preset_input from frigate.ffmpeg_presets import parse_preset_input
@ -93,6 +93,16 @@ class TestFfmpegPresets(unittest.TestCase):
" ".join(frigate_config.cameras["back"].ffmpeg_cmds[0]["cmd"]) " ".join(frigate_config.cameras["back"].ffmpeg_cmds[0]["cmd"])
) )
def test_ffmpeg_input_args_as_string(self):
argsString = " ".join(FFMPEG_INPUT_ARGS_DEFAULT) + ' -some "arg with space"'
argsList = FFMPEG_INPUT_ARGS_DEFAULT + ["-some", "arg with space"]
self.default_ffmpeg["cameras"]["back"]["ffmpeg"]["input_args"] = argsString
frigate_config = FrigateConfig(**self.default_ffmpeg)
frigate_config.cameras["back"].create_ffmpeg_cmds()
assert set(argsList).issubset(
frigate_config.cameras["back"].ffmpeg_cmds[0]["cmd"]
)
def test_ffmpeg_input_not_preset(self): def test_ffmpeg_input_not_preset(self):
self.default_ffmpeg["cameras"]["back"]["ffmpeg"]["input_args"] = "-some inputs" self.default_ffmpeg["cameras"]["back"]["ffmpeg"]["input_args"] = "-some inputs"
frigate_config = FrigateConfig(**self.default_ffmpeg) frigate_config = FrigateConfig(**self.default_ffmpeg)

View File

@ -1,6 +1,7 @@
import copy import copy
import datetime import datetime
import logging import logging
import shlex
import subprocess as sp import subprocess as sp
import json import json
import re import re
@ -888,7 +889,7 @@ def vainfo_hwaccel() -> sp.CompletedProcess:
def get_ffmpeg_arg_list(arg: Any) -> list: def get_ffmpeg_arg_list(arg: Any) -> list:
"""Use arg if list or convert to list format.""" """Use arg if list or convert to list format."""
return arg if isinstance(arg, list) else arg.split(" ") return arg if isinstance(arg, list) else shlex.split(arg)
class FrameManager(ABC): class FrameManager(ABC):