Skip to content

Commit

Permalink
Set User Agent for FFmpeg calls (#4555)
Browse files Browse the repository at this point in the history
* Set User Agent for FFmpeg calls

* Allow to use shell-like string args

* Add test for arg as string with space
  • Loading branch information
felipecrs committed Nov 30, 2022
1 parent 5ad3919 commit 4523c9b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/devcontainer.json
Expand Up @@ -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]": {
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -2,7 +2,8 @@
*.pyc
*.swp
debug
.vscode
.vscode/*
!.vscode/launch.json
config/config.yml
models
*.mp4
Expand Down
12 changes: 12 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,12 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Launch Frigate",
"type": "python",
"request": "launch",
"module": "frigate",
"justMyCode": true
}
]
}
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/configuration/index.md
Expand Up @@ -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: []
Expand Down
9 changes: 8 additions & 1 deletion frigate/config.py
Expand Up @@ -32,6 +32,7 @@
parse_preset_output_record,
parse_preset_output_rtmp,
)
from frigate.version import VERSION

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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",
Expand Down
12 changes: 11 additions & 1 deletion 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


Expand Down Expand Up @@ -93,6 +93,16 @@ def test_ffmpeg_input_preset(self):
" ".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)
Expand Down
3 changes: 2 additions & 1 deletion frigate/util.py
@@ -1,6 +1,7 @@
import copy
import datetime
import logging
import shlex
import subprocess as sp
import json
import re
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4523c9b

Please sign in to comment.