diff --git a/pyproject.toml b/pyproject.toml index 9cf9fb2..2aa3392 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,7 +11,7 @@ dependencies = [ ] [project.scripts] -rewind = "rewind.clip:main" +rewind = "rewind.cli:main" rewind-daemon = "rewind.daemon:main" [tool.setuptools.package-data] diff --git a/rewind/clip.py b/rewind/cli.py similarity index 72% rename from rewind/clip.py rename to rewind/cli.py index be2e8d5..d975a15 100755 --- a/rewind/clip.py +++ b/rewind/cli.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 import sys import argparse -from rewind.video import clip +from rewind.core import clip, mark def build_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser( @@ -19,6 +19,14 @@ def build_parser() -> argparse.ArgumentParser: help="Number of seconds to include in the clip (default: 30)" ) + mark = sub.add_parser("mark", help="Mark the current time in the recording for later reference") + mark.add_argument( + "-n", "--name", + type=str, + required=True, + help="Name of the marker" + ) + return parser def main(argv=None) -> int: @@ -27,6 +35,8 @@ def main(argv=None) -> int: if args.command == "save": clip(args.seconds) + elif args.command == "mark": + mark(args.name) else: parser.error("Unknown command") diff --git a/rewind/video.py b/rewind/core.py similarity index 81% rename from rewind/video.py rename to rewind/core.py index 61e2440..bb35eaa 100644 --- a/rewind/video.py +++ b/rewind/core.py @@ -2,6 +2,7 @@ import os import datetime import subprocess +import json from rewind.paths import load_state """ @@ -63,6 +64,27 @@ def clip(seconds_from_end: float) -> None: combine_last_x_ts_files(seconds_from_end, output_file_name) print(f"Created clip: {output_file_name}") +def mark(name: str) -> None: + if not name: + raise ValueError("Marker name cannot be empty") + + # writes marker to json file (not state) + markers_file = os.path.join(os.path.dirname(__file__), "markers.json") + if os.path.exists(markers_file): + with open(markers_file, "r") as f: + markers = json.load(f) + else: + markers = [] + + markers.append({ + "name": name, + "timestamp": datetime.datetime.now().timestamp() + }) + + with open(markers_file, "w") as f: + json.dump(markers, f, indent=4) + print(f"Added marker: {name}") + def get_duration(file_path: str) -> float: result = subprocess.run( ["ffprobe", "-v", "error", "-show_entries", diff --git a/rewind/daemon.py b/rewind/daemon.py index 4bf3ed3..112ab45 100755 --- a/rewind/daemon.py +++ b/rewind/daemon.py @@ -6,7 +6,7 @@ import time import obsws_python as obs import subprocess -from rewind.video import get_duration +from rewind.core import get_duration from rewind.paths import load_state, write_state, load_config from watchdog.observers import Observer from watchdog.events import FileSystemEventHandler @@ -81,7 +81,13 @@ def main() -> None: time.sleep(5) config = load_config() - con = open_obs_connection(config["obs"]["host"], config["obs"]["port"], config["obs"]["password"]) + + con = None + for _ in range(10): + con = open_obs_connection(config["obs"]["host"], config["obs"]["port"], config["obs"]["password"]) + if con is not None: + break + time.sleep(2) recording_dir = con.get_record_directory().record_directory start_recording(con)