diff --git a/home-manager/plasma.nix b/home-manager/plasma.nix index 23fb8af..91ff76c 100644 --- a/home-manager/plasma.nix +++ b/home-manager/plasma.nix @@ -36,7 +36,7 @@ hotkeys.commands = { "spectacle-screenshot" = { name = "Spectacle Screenshot"; - command = "/etc/nixos/scripts/py/spectacle-screenshot.py"; + command = "/etc/nixos/scripts/py/spectacle-screenshot.py --format=webp"; comment = "Take a screenshot of a region on the screen and automatically upload it to Zipline"; keys = [ "Print" @@ -45,7 +45,7 @@ }; "spectacle-recording" = { name = "Spectacle Recording"; - command = "/etc/nixos/scripts/py/spectacle-screenshot.py --record"; + command = "/etc/nixos/scripts/py/spectacle-screenshot.py --record --format=webm"; comment = "Record a region on the screen and automatically upload it to Zipline"; keys = [ "Shift+Print" diff --git a/scripts/nix/python.nix b/scripts/nix/python.nix index 9fda1e1..a33c067 100644 --- a/scripts/nix/python.nix +++ b/scripts/nix/python.nix @@ -3,13 +3,21 @@ let config = { }; overlays = [ ]; }; + pythonPackages = with pkgs.python312Packages; [ + requests + pyperclip + pillow + pyside6 + ]; in pkgs.mkShell { - packages = with pkgs; [ - libnotify - python312 - python312Packages.requests - python312Packages.pyperclip - python312Packages.pillow - ]; + packages = + with pkgs; + pkgs.lib.lists.unique ( + [ + libnotify + python312 + ] + ++ pythonPackages + ); } diff --git a/scripts/py/spectacle-screenshot.py b/scripts/py/spectacle-screenshot.py index e9f9d94..66a1d20 100755 --- a/scripts/py/spectacle-screenshot.py +++ b/scripts/py/spectacle-screenshot.py @@ -9,10 +9,14 @@ from pathlib import Path from shutil import which from common.common import notify # type: ignore +from PySide6.QtGui import QImageWriter # type: ignore def spectacle_screenshot( - url: str | None = None, record: bool = False, file_path: Path | None = None + url: str | None = None, + record: bool = False, + file_path: Path | None = None, + format: str | None = None, ) -> None: try: if not which("spectacle"): @@ -23,10 +27,21 @@ def spectacle_screenshot( 'File already exists. Please provide a different file path, or use the zipline function to upload the file.\nExample: zipline "{file_path}"' ) + accepted_formats = [str(f) for f in QImageWriter.supportedImageFormats()] + accepted_formats.extend(("webm", "mp4")) + print(accepted_formats) + if not format: + format = "mp4" if record else "png" + if format.lower() not in accepted_formats: + raise ValueError( + f"Invalid format. Accepted formats are: {', '.join(accepted_formats)}" + ) + else: + format = f".{format}" + if not file_path: use_temp_file = True - suffix = ".webm" if record else ".png" - temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix) + temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=format) file_path = Path(temp_file.name) temp_file.close() else: @@ -96,6 +111,11 @@ if __name__ == "__main__": help="The URL of the Zipline instance to upload the screenshot or recording to. Defaults to 'https://csw.im'.", default="https://csw.im", ) + parser.add_argument( + "--format", + help="The format of the screenshot. Defaults to 'png' if `--record` is not set, and 'mp4' if `--record` is set.", + default=None, + ) parser.add_argument( "--record", help="If this is set, Spectacle will record the region instead of taking a screenshot.", @@ -113,4 +133,5 @@ if __name__ == "__main__": url=args.url, record=args.record, file_path=args.file_path, + format=args.format, )