record: fix multi-monitor and moving across filesystems (#38)

* fix(record): support differing filesystems for recording destination

* fix(record): for multi-monitor-systems wl-screenrec needs a -o argument

* fix(record): replace path.rename with shutil.move

* fix(record): use json option to retrieve hyprland focused monitor

* use generator

---------

Co-authored-by: 2 * r + 2 * t <61896496+soramanew@users.noreply.github.com>
This commit is contained in:
anders130
2025-08-13 08:07:12 +02:00
committed by GitHub
parent d15d5c4399
commit 1fcfb83fba
+19 -4
View File
@@ -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