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

@ -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,
)