diff options
| author | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-06-14 02:11:10 +1000 |
|---|---|---|
| committer | 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com> | 2025-06-14 02:11:10 +1000 |
| commit | b805f8d67725352817d248562683cb90bf314401 (patch) | |
| tree | 511edf471c11e7b99b0cb4ea43ad275f339c18cc /src/caelestia | |
| parent | wallpaper: fix when no wall (diff) | |
| download | caelestia-cli-b805f8d67725352817d248562683cb90bf314401.tar.gz caelestia-cli-b805f8d67725352817d248562683cb90bf314401.tar.bz2 caelestia-cli-b805f8d67725352817d248562683cb90bf314401.zip | |
feat: impl recording subcommand
Diffstat (limited to 'src/caelestia')
| -rw-r--r-- | src/caelestia/parser.py | 2 | ||||
| -rw-r--r-- | src/caelestia/subcommands/record.py | 113 | ||||
| -rw-r--r-- | src/caelestia/utils/paths.py | 4 |
3 files changed, 117 insertions, 2 deletions
diff --git a/src/caelestia/parser.py b/src/caelestia/parser.py index 02ac3b9..6d0b552 100644 --- a/src/caelestia/parser.py +++ b/src/caelestia/parser.py @@ -68,7 +68,7 @@ def parse_args() -> (argparse.ArgumentParser, argparse.Namespace): # Create parser for record opts record_parser = command_parser.add_parser("record", help="start a screen recording") record_parser.set_defaults(cls=record.Command) - record_parser.add_argument("-r", "--region", action="store_true", help="record a region") + record_parser.add_argument("-r", "--region", nargs="?", const="slurp", help="record a region") record_parser.add_argument("-s", "--sound", action="store_true", help="record audio") # Create parser for clipboard opts diff --git a/src/caelestia/subcommands/record.py b/src/caelestia/subcommands/record.py index 37f9a2b..a4fa51d 100644 --- a/src/caelestia/subcommands/record.py +++ b/src/caelestia/subcommands/record.py @@ -1,4 +1,9 @@ +import subprocess +import time from argparse import Namespace +from datetime import datetime + +from caelestia.utils.paths import recording_notif_path, recording_path, recordings_dir class Command: @@ -8,4 +13,110 @@ class Command: self.args = args def run(self) -> None: - pass + proc = subprocess.run(["pidof", "wl-screenrec"]) + if proc.returncode == 0: + self.stop() + else: + self.start() + + def start(self) -> None: + args = [] + + if self.args.region: + if self.args.region == "slurp": + region = subprocess.check_output(["slurp"], text=True) + else: + region = self.args.region + args += ["-g", region.strip()] + + if self.args.sound: + sources = subprocess.check_output(["pactl", "list", "short", "sources"], text=True).splitlines() + for source in sources: + if "RUNNING" in source: + args += ["--audio", "--audio-device", source.split()[1]] + break + else: + raise ValueError("No audio source found") + + proc = subprocess.Popen( + ["wl-screenrec", *args, "--codec", "hevc", "-f", recording_path], + stderr=subprocess.PIPE, + text=True, + start_new_session=True, + ) + + # Send notif if proc hasn't ended after a small delay + time.sleep(0.1) + if proc.poll() is None: + notif = subprocess.check_output( + ["notify-send", "-p", "-a", "caelestia-cli", "Recording started", "Recording..."], text=True + ).strip() + recording_notif_path.write_text(notif) + else: + subprocess.run( + [ + "notify-send", + "-a", + "caelestia-cli", + "Recording failed", + f"Recording failed to start: {proc.communicate()[1]}", + ] + ) + + def stop(self) -> None: + subprocess.run(["pkill", "wl-screenrec"]) + + # Move to recordings folder + new_path = recordings_dir / f"recording_{datetime.now().strftime('%Y%m%d_%H-%M-%S')}.mp4" + recording_path.rename(new_path) + + # Close start notification + try: + notif = recording_notif_path.read_text() + subprocess.run( + [ + "gdbus", + "call", + "--session", + "--dest=org.freedesktop.Notifications", + "--object-path=/org/freedesktop/Notifications", + "--method=org.freedesktop.Notifications.CloseNotification", + notif, + ] + ) + except IOError: + pass + + action = subprocess.check_output( + [ + "notify-send", + "-a", + "caelestia-cli", + "--action=watch=Watch", + "--action=open=Open", + "--action=delete=Delete", + "Recording stopped", + f"Recording saved in {new_path}", + ], + text=True, + ).strip() + + if action == "watch": + subprocess.Popen(["app2unit", "-O", new_path], start_new_session=True) + elif action == "open": + p = subprocess.run( + [ + "dbus-send", + "--session", + "--dest=org.freedesktop.FileManager1", + "--type=method_call", + "/org/freedesktop/FileManager1", + "org.freedesktop.FileManager1.ShowItems", + f"array:string:file://{new_path}", + "string:", + ] + ) + if p.returncode != 0: + subprocess.Popen(["app2unit", "-O", new_path.parent], start_new_session=True) + elif action == "delete": + new_path.unlink() diff --git a/src/caelestia/utils/paths.py b/src/caelestia/utils/paths.py index 6a98dae..a4ef36f 100644 --- a/src/caelestia/utils/paths.py +++ b/src/caelestia/utils/paths.py @@ -31,6 +31,10 @@ wallpapers_cache_dir = c_cache_dir / "wallpapers" screenshots_dir = Path.home() / "Pictures/Screenshots" screenshots_cache_dir = c_cache_dir / "screenshots" +recordings_dir = Path.home() / "Videos/Recordings" +recording_path = c_state_dir / "record/recording.mp4" +recording_notif_path = c_state_dir / "record/notifid.txt" + def compute_hash(path: Path | str) -> str: sha = hashlib.sha256() |