diff --git a/src/simple_recorder/cli.py b/src/simple_recorder/cli.py index b1b9ad0..2a52ee9 100644 --- a/src/simple_recorder/cli.py +++ b/src/simple_recorder/cli.py @@ -8,6 +8,7 @@ from .errors import SimpleRecorderError from .gui import SimpleRecorderWindow from .pause import Pause from .resume import Resume +from .split import Split from .start import Start from .stop import Stop @@ -37,8 +38,11 @@ def theme_parser(value: str) -> str: return value +SUBCOMMANDS = Start | Stop | Pause | Resume | Split | Directory + + class SimpleRecorder(Command): - subcommand: Start | Stop | Pause | Resume | Directory | None = None + subcommand: SUBCOMMANDS | None = None host: str = arg(default="localhost", env="OBS_HOST", help="OBS WebSocket host") port: int = arg(default=4455, env="OBS_PORT", help="OBS WebSocket port") password: str | None = arg( @@ -52,7 +56,6 @@ class SimpleRecorder(Command): ) debug: bool = arg( default=False, - env="DEBUG", help="Enable debug logging", hidden=True, ) diff --git a/src/simple_recorder/gui.py b/src/simple_recorder/gui.py index 866468f..60de96e 100644 --- a/src/simple_recorder/gui.py +++ b/src/simple_recorder/gui.py @@ -7,6 +7,7 @@ from .directory import Directory from .errors import SimpleRecorderError from .pause import Pause from .resume import Resume +from .split import Split from .start import Start from .stop import Stop @@ -94,6 +95,7 @@ class SimpleRecorderWindow(fsg.Window): self["Stop Recording"].bind("", " || RETURN") self["Pause Recording"].bind("", " || RETURN") self["Resume Recording"].bind("", " || RETURN") + self["Split Recording"].bind("", " || RETURN") self["-FILENAME-"].bind("", " || KEYPRESS") self["-FILENAME-"].update(select=True) @@ -177,6 +179,19 @@ class SimpleRecorderWindow(fsg.Window): else: self["-OUTPUT-RECORDER-"].update("", text_color="white") + case ["Split Recording"] | ["Split Recording", "RETURN"]: + try: + await Split( + host=self.host, port=self.port, password=self.password + ).run() + self["-OUTPUT-RECORDER-"].update( + "Recording split successfully", text_color="green" + ) + except SimpleRecorderError as e: + self["-OUTPUT-RECORDER-"].update( + f"Error: {e.raw_message}", text_color="red" + ) + case ["Add Chapter", "RIGHT_CLICK"]: _ = fsg.popup_get_text( "Enter chapter name:", @@ -184,7 +199,7 @@ class SimpleRecorderWindow(fsg.Window): default_text="unnamed", ) - case ["Split Recording" | "Add Chapter"]: + case ["Add Chapter"]: self["-OUTPUT-RECORDER-"].update( "This feature is not implemented yet", text_color="orange" ) diff --git a/src/simple_recorder/split.py b/src/simple_recorder/split.py new file mode 100644 index 0000000..30df6e5 --- /dev/null +++ b/src/simple_recorder/split.py @@ -0,0 +1,40 @@ +import logging + +import obsws_python as obsws +from clypi import Command, arg +from typing_extensions import override + +from .errors import SimpleRecorderError + +logging.basicConfig(level=logging.disable()) + + +class Split(Command): + """Split the current recording into a new file.""" + + host: str = arg(inherited=True) + port: int = arg(inherited=True) + password: str = arg(inherited=True) + + @override + async def run(self): + """Run the split command.""" + try: + with obsws.ReqClient( + host=self.host, port=self.port, password=self.password, timeout=3 + ) as client: + resp = client.get_record_status() + if not resp.output_active: + raise SimpleRecorderError("No active recording to split.") + + client.split_record_file() + print("Recording split successfully.") + except (ConnectionRefusedError, TimeoutError): + raise SimpleRecorderError("Failed to connect to OBS. Is it running?") + except obsws.error.OBSSDKRequestError as e: + if e.code == 702: + raise SimpleRecorderError( + "Unable to split file, please check your OBS settings." + ) + else: + raise SimpleRecorderError(f"Error: {e.code} - {e.message}") diff --git a/src/simple_recorder/stop.py b/src/simple_recorder/stop.py index bc0d54f..cbc6e6c 100644 --- a/src/simple_recorder/stop.py +++ b/src/simple_recorder/stop.py @@ -3,7 +3,6 @@ from clypi import Command, arg from typing_extensions import override from .errors import SimpleRecorderError -from .styler import highlight class Stop(Command): @@ -24,6 +23,6 @@ class Stop(Command): raise SimpleRecorderError("Recording is not active.") client.stop_record() - print(highlight("Recording stopped successfully.")) + print("Recording stopped successfully.") except (ConnectionRefusedError, TimeoutError): raise SimpleRecorderError("Failed to connect to OBS. Is it running?")