Skip to content

Commit

Permalink
start to support built-in profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
time4tea committed Dec 30, 2023
1 parent 530604b commit 7562bd1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 28 deletions.
66 changes: 46 additions & 20 deletions gopro_overlay/ffmpeg_profile.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
from typing import Mapping

from gopro_overlay.config import Config
from gopro_overlay.ffmpeg_overlay import FFMPEGOptions

builtin_profiles = {}


class FFMPEGProfiles:

def load_ffmpeg_profile(config: Config, profile: str):
config_file = config.load("FFMPEG Profile", "ffmpeg-profiles.json")
def __init__(self, config: Config):
self.config = config

profile_json = config_file.content
def load_profile(self, name: str) -> FFMPEGOptions:
config_file = self.config.maybe("ffmpeg-profiles.json")

if profile not in profile_json:
raise ValueError(f"Can't find key {profile} in {config_file.location}")
if config_file.exists():
if name in config_file.content:
try:
return self.load_profile_content(config_file.content, name)
except ValueError as e:
raise ValueError(f"{config_file.location}: {e}") from None

selected = profile_json[profile]
if name in builtin_profiles:
profile = builtin_profiles[name]
return FFMPEGOptions(input=profile["input"], output=profile["output"], filter_spec=profile["filter"])

if config_file.exists():
raise ValueError(f"Can't find key {name} in {config_file.location}, and it is also not a built-in profile")
else:
raise ValueError(f"{name} is not a built-in profile, and no config file found at {config_file.location}")

if "input" in selected and type(selected["input"]) == list:
input_options = selected["input"]
else:
raise ValueError(f"Can't find input option list for key {profile} in {config_file.location}")
def load_profile_content(self, content: Mapping, name: str) -> FFMPEGOptions:

if "output" in selected and type(selected["output"]) == list:
output_options = selected["output"]
else:
raise ValueError(f"Can't find output option list for key {profile} in {config_file.location}")
selected = content[name]

filter_spec = None
if "input" in selected and isinstance(selected["input"], list):
input_options = selected["input"]
else:
raise ValueError(f"Can't find input option list for key {name}")

if "filter" in selected:
if type(selected["filter"]) == str:
filter_spec = selected["filter"]
if "output" in selected and isinstance(selected["output"], list):
output_options = selected["output"]
else:
raise ValueError("'filter' specified, but wasn't a string")
raise ValueError(f"Can't find output option list for key {name}")

filter_spec = None

if "filter" in selected:
if isinstance(selected["filter"], str):
filter_spec = selected["filter"]
else:
raise ValueError("'filter' specified, but wasn't a string")

return FFMPEGOptions(input=input_options, output=output_options, filter_spec=filter_spec)


return FFMPEGOptions(input=input_options, output=output_options, filter_spec=filter_spec)
def load_ffmpeg_profile(config: Config, name: str) -> FFMPEGOptions:
return FFMPEGProfiles(config).load_profile(name)
26 changes: 18 additions & 8 deletions tests/test_ffmpeg_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@
"simple": {
"input": ["i1", "i2"],
"output": ["o1", "o2"]
},
"filter": {
"input": ["a"],
"output": ["b"],
"filter": "c"
}
}


def test_loading_a_profile():
with tempfile.TemporaryDirectory() as tempdir:
config = Path(tempdir) / "ffmpeg-profiles.json"
config.write_text(json.dumps(simple))
class TestProfiles:

def test_loading_a_profile(self):
with tempfile.TemporaryDirectory() as tempdir:
config = Path(tempdir) / "ffmpeg-profiles.json"
config.write_text(json.dumps(simple))

loader = Config(Path(tempdir))
profile = load_ffmpeg_profile(loader, "simple")
loader = Config(Path(tempdir))
profile = load_ffmpeg_profile(loader, "simple")

assert profile.input == ["i1", "i2"]
assert profile.output == ["o1", "o2"]
assert profile.input == ["i1", "i2"]
assert profile.output == ["o1", "o2"]

profile_2 = load_ffmpeg_profile(loader, "filter")
assert profile_2.filter_complex == "c"

0 comments on commit 7562bd1

Please sign in to comment.