feat: screenrecorder

This commit is contained in:
sankalp committed 2024-12-10 23:03:17 +05:45
1 parent 8c7206b7b7
commit 43eafe6b3a
1 file changed
+59 -20
+59 -20
View File
@@ -1,31 +1,20 @@
import datetime
import subprocess
from fabric.core.service import Service
from fabric.utils import exec_shell_command
from fabric.core.service import Property, Service, Signal
from fabric.utils import exec_shell_command, exec_shell_command_async
from gi.repository import Gio, GLib
from loguru import logger
def exec_shell_command_async(cmd: str) -> bool:
if isinstance(cmd, str):
try:
GLib.spawn_command_line_async(cmd)
return True
except ValueError:
return False
else:
return False
class ScreenRecorder(Service):
# __gsignals__ = SignalContainer(
@Signal
def recording(self, value: bool) -> None: ...
# )
def __init__(self, **kwargs):
self.screenshot_path = GLib.get_home_dir() + "/Pictures/Screenshots/"
self.screenrecord_path = GLib.get_home_dir() + "/Videos/Screencasting/"
self.recording = False
super().__init__(**kwargs)
def screenshot(self, fullscreen=False, save_copy=True):
@@ -47,18 +36,64 @@ class ScreenRecorder(Service):
logger.error(f"[SCREENSHOT] Failed to run command: {command}")
def screencast_start(self, fullscreen=False):
if self.is_recording:
logger.error(
"[SCREENRECORD] Another instance of wf-recorder is already running"
)
return
time = datetime.datetime.today().strftime("%Y-%m-%d_%H-%M-%S")
file_path = self.screenrecord_path + str(time) + ".mp4"
self._current_screencast_path = file_path
area = ""
if fullscreen:
area = exec_shell_command("slurp")
command = f"wf-recorder -g {area} -f {file_path}"
f"-g '{exec_shell_command('slurp')}'"
command = f"wf-recorder --file={file_path} --pixel-format yuv420p {area}"
exec_shell_command_async(command)
self.emit("recording", True)
def screencast_stop(self):
exec_shell_command_async("killall -INT wf-recorder")
self.recording = False
self.emit("recording", False)
self.send_screencast_notification(self._current_screencast_path)
def send_screencast_notification(self, file_path):
# TODO: Generate a thumbnail
# exec_shell_command_async(f"totem-video-thumbnailer {file_path} {FABRIC_CACHE}")
cmd = ["notify-send"]
cmd.extend(
[
"-A",
"files=Show in Files",
"-A",
"view=View",
"-i",
"camera-video-symbolic",
"-a",
"Fabric Screenshot Utility",
"Screencast Saved",
f"Saved Screencast at {file_path}",
]
)
proc: Gio.Subprocess = Gio.Subprocess.new(cmd, Gio.SubprocessFlags.STDOUT_PIPE)
def do_callback(process: Gio.Subprocess, task: Gio.Task):
try:
_, stdout, stderr = process.communicate_utf8_finish(task)
except Exception:
logger.error(
f"[SCREENCAST] Failed read notification action with error {stderr}"
)
return
match stdout.strip("\n"):
case "files":
exec_shell_command_async(f"xdg-open {self.screenrecord_path}")
case "view":
exec_shell_command_async(f"xdg-open {file_path}")
proc.communicate_utf8_async(None, None, do_callback)
def send_screenshot_notification(self, file_path=None):
cmd = ["notify-send"]
@@ -103,3 +138,7 @@ class ScreenRecorder(Service):
exec_shell_command_async(f"swappy -f {file_path}")
proc.communicate_utf8_async(None, None, do_callback)
@Property(bool, "readable", default_value=False)
def is_recording(self):
return False if len(exec_shell_command("pidof wf-recorder")) == 0 else True