diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/caelestia/subcommands/record.py | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/caelestia/subcommands/record.py b/src/caelestia/subcommands/record.py index 69c961e..b7f0f65 100644 --- a/src/caelestia/subcommands/record.py +++ b/src/caelestia/subcommands/record.py @@ -1,3 +1,5 @@ +import json +import shutil import subprocess import time from argparse import Namespace @@ -14,12 +16,14 @@ class Command: self.args = args def run(self) -> None: - proc = subprocess.run(["pidof", "wl-screenrec"]) - if proc.returncode == 0: + if self.proc_running(): self.stop() else: self.start() + def proc_running(self) -> bool: + return subprocess.run(["pidof", "wl-screenrec"], stdout=subprocess.DEVNULL).returncode == 0 + def start(self) -> None: args = [] @@ -30,6 +34,11 @@ class Command: region = self.args.region args += ["-g", region.strip()] + monitors = json.loads(subprocess.check_output(["hyprctl", "monitors", "-j"])) + focused_monitor = next(monitor for monitor in monitors if monitor["focused"]) + if focused_monitor: + args += ["-o", focused_monitor["name"]] + if self.args.sound: sources = subprocess.check_output(["pactl", "list", "short", "sources"], text=True).splitlines() for source in sources: @@ -56,12 +65,17 @@ class Command: notify("Recording failed", f"Recording failed to start: {proc.communicate()[1]}") def stop(self) -> None: + # Start killing recording process subprocess.run(["pkill", "wl-screenrec"]) + # Wait for recording to finish to avoid corrupted video file + while self.proc_running(): + time.sleep(0.1) + # Move to recordings folder new_path = recordings_dir / f"recording_{datetime.now().strftime('%Y%m%d_%H-%M-%S')}.mp4" recordings_dir.mkdir(exist_ok=True, parents=True) - recording_path.rename(new_path) + shutil.move(recording_path, new_path) # Close start notification try: @@ -75,7 +89,8 @@ class Command: "--object-path=/org/freedesktop/Notifications", "--method=org.freedesktop.Notifications.CloseNotification", notif, - ] + ], + stdout=subprocess.DEVNULL, ) except IOError: pass |