forked from Shinonome/caelestia-cli
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:
@@ -1,3 +1,5 @@
|
|||||||
|
import json
|
||||||
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import time
|
import time
|
||||||
from argparse import Namespace
|
from argparse import Namespace
|
||||||
@@ -14,12 +16,14 @@ class Command:
|
|||||||
self.args = args
|
self.args = args
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
proc = subprocess.run(["pidof", "wl-screenrec"])
|
if self.proc_running():
|
||||||
if proc.returncode == 0:
|
|
||||||
self.stop()
|
self.stop()
|
||||||
else:
|
else:
|
||||||
self.start()
|
self.start()
|
||||||
|
|
||||||
|
def proc_running(self) -> bool:
|
||||||
|
return subprocess.run(["pidof", "wl-screenrec"], stdout=subprocess.DEVNULL).returncode == 0
|
||||||
|
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
args = []
|
args = []
|
||||||
|
|
||||||
@@ -30,6 +34,11 @@ class Command:
|
|||||||
region = self.args.region
|
region = self.args.region
|
||||||
args += ["-g", region.strip()]
|
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:
|
if self.args.sound:
|
||||||
sources = subprocess.check_output(["pactl", "list", "short", "sources"], text=True).splitlines()
|
sources = subprocess.check_output(["pactl", "list", "short", "sources"], text=True).splitlines()
|
||||||
for source in sources:
|
for source in sources:
|
||||||
@@ -56,12 +65,17 @@ class Command:
|
|||||||
notify("Recording failed", f"Recording failed to start: {proc.communicate()[1]}")
|
notify("Recording failed", f"Recording failed to start: {proc.communicate()[1]}")
|
||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
|
# Start killing recording process
|
||||||
subprocess.run(["pkill", "wl-screenrec"])
|
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
|
# Move to recordings folder
|
||||||
new_path = recordings_dir / f"recording_{datetime.now().strftime('%Y%m%d_%H-%M-%S')}.mp4"
|
new_path = recordings_dir / f"recording_{datetime.now().strftime('%Y%m%d_%H-%M-%S')}.mp4"
|
||||||
recordings_dir.mkdir(exist_ok=True, parents=True)
|
recordings_dir.mkdir(exist_ok=True, parents=True)
|
||||||
recording_path.rename(new_path)
|
shutil.move(recording_path, new_path)
|
||||||
|
|
||||||
# Close start notification
|
# Close start notification
|
||||||
try:
|
try:
|
||||||
@@ -75,7 +89,8 @@ class Command:
|
|||||||
"--object-path=/org/freedesktop/Notifications",
|
"--object-path=/org/freedesktop/Notifications",
|
||||||
"--method=org.freedesktop.Notifications.CloseNotification",
|
"--method=org.freedesktop.Notifications.CloseNotification",
|
||||||
notif,
|
notif,
|
||||||
]
|
],
|
||||||
|
stdout=subprocess.DEVNULL,
|
||||||
)
|
)
|
||||||
except IOError:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user