add format option to spectacle-screenshot

This commit is contained in:
cswimr 2025-02-02 11:07:41 -06:00
parent 210d45a02e
commit 426206d4b3
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087
3 changed files with 41 additions and 12 deletions

View file

@ -36,7 +36,7 @@
hotkeys.commands = { hotkeys.commands = {
"spectacle-screenshot" = { "spectacle-screenshot" = {
name = "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"; comment = "Take a screenshot of a region on the screen and automatically upload it to Zipline";
keys = [ keys = [
"Print" "Print"
@ -45,7 +45,7 @@
}; };
"spectacle-recording" = { "spectacle-recording" = {
name = "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"; comment = "Record a region on the screen and automatically upload it to Zipline";
keys = [ keys = [
"Shift+Print" "Shift+Print"

View file

@ -3,13 +3,21 @@ let
config = { }; config = { };
overlays = [ ]; overlays = [ ];
}; };
pythonPackages = with pkgs.python312Packages; [
requests
pyperclip
pillow
pyside6
];
in in
pkgs.mkShell { pkgs.mkShell {
packages = with pkgs; [ packages =
libnotify with pkgs;
python312 pkgs.lib.lists.unique (
python312Packages.requests [
python312Packages.pyperclip libnotify
python312Packages.pillow python312
]; ]
++ pythonPackages
);
} }

View file

@ -9,10 +9,14 @@ from pathlib import Path
from shutil import which from shutil import which
from common.common import notify # type: ignore from common.common import notify # type: ignore
from PySide6.QtGui import QImageWriter # type: ignore
def spectacle_screenshot( 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: ) -> None:
try: try:
if not which("spectacle"): 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}"' '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: if not file_path:
use_temp_file = True use_temp_file = True
suffix = ".webm" if record else ".png" temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=format)
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
file_path = Path(temp_file.name) file_path = Path(temp_file.name)
temp_file.close() temp_file.close()
else: 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'.", help="The URL of the Zipline instance to upload the screenshot or recording to. Defaults to 'https://csw.im'.",
default="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( parser.add_argument(
"--record", "--record",
help="If this is set, Spectacle will record the region instead of taking a screenshot.", 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, url=args.url,
record=args.record, record=args.record,
file_path=args.file_path, file_path=args.file_path,
format=args.format,
) )