From 4523c9b06d3cbaa91f4d36533052c4746ed31fd5 Mon Sep 17 00:00:00 2001 From: Felipe Santos Date: Wed, 30 Nov 2022 19:53:45 -0300 Subject: [PATCH] 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 --- .devcontainer/devcontainer.json | 3 +++ .gitignore | 3 ++- .vscode/launch.json | 12 ++++++++++++ Makefile | 2 +- docs/docs/configuration/index.md | 2 +- frigate/config.py | 9 ++++++++- frigate/test/test_ffmpeg_presets.py | 12 +++++++++++- frigate/util.py | 3 ++- 8 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 81ddfc91f..7c91f4f6f 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -60,6 +60,9 @@ "editor.formatOnPaste": false, "editor.formatOnSave": true, "editor.formatOnType": true, + "python.testing.pytestEnabled": false, + "python.testing.unittestEnabled": true, + "python.testing.unittestArgs": ["-v", "-s", "./frigate/test"], "files.trimTrailingWhitespace": true, "eslint.workingDirectories": ["./web"], "[json][jsonc]": { diff --git a/.gitignore b/.gitignore index 6fe616b5c..8c4e982cf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,8 @@ *.pyc *.swp debug -.vscode +.vscode/* +!.vscode/launch.json config/config.yml models *.mp4 diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 000000000..fe709c92d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Launch Frigate", + "type": "python", + "request": "launch", + "module": "frigate", + "justMyCode": true + } + ] +} diff --git a/Makefile b/Makefile index 431b82ed0..3ec99eb16 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ build: version amd64 arm64 armv7 push: build 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 mypy --config-file frigate/mypy.ini frigate diff --git a/docs/docs/configuration/index.md b/docs/docs/configuration/index.md index 26f089695..a550f4de6 100644 --- a/docs/docs/configuration/index.md +++ b/docs/docs/configuration/index.md @@ -142,7 +142,7 @@ birdseye: # Optional: ffmpeg configuration ffmpeg: # 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) # NOTE: See hardware acceleration docs for your specific device hwaccel_args: [] diff --git a/frigate/config.py b/frigate/config.py index ca6ead667..249377c00 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -32,6 +32,7 @@ from frigate.ffmpeg_presets import ( parse_preset_output_record, parse_preset_output_rtmp, ) +from frigate.version import VERSION 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 = [ "-avoid_negative_ts", "make_zero", diff --git a/frigate/test/test_ffmpeg_presets.py b/frigate/test/test_ffmpeg_presets.py index 42e380fab..4ace9c496 100644 --- a/frigate/test/test_ffmpeg_presets.py +++ b/frigate/test/test_ffmpeg_presets.py @@ -1,5 +1,5 @@ import unittest -from frigate.config import FrigateConfig +from frigate.config import FFMPEG_INPUT_ARGS_DEFAULT, FrigateConfig 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"]) ) + 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): self.default_ffmpeg["cameras"]["back"]["ffmpeg"]["input_args"] = "-some inputs" frigate_config = FrigateConfig(**self.default_ffmpeg) diff --git a/frigate/util.py b/frigate/util.py index c74a4b685..1c2a2533f 100755 --- a/frigate/util.py +++ b/frigate/util.py @@ -1,6 +1,7 @@ import copy import datetime import logging +import shlex import subprocess as sp import json import re @@ -888,7 +889,7 @@ def vainfo_hwaccel() -> sp.CompletedProcess: def get_ffmpeg_arg_list(arg: Any) -> list: """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):