From eb5453f9100f1f94426fefd046b8255318acfa6b Mon Sep 17 00:00:00 2001 From: Jacob Chapman <7908073+chapmanjacobd@users.noreply.github.com> Date: Wed, 23 Oct 2024 07:20:02 +0000 Subject: [PATCH] process-media: filter exts if supporting programs not installed --- xklb/mediafiles/process_media.py | 20 +++++++++++++------- xklb/mediafiles/process_text.py | 4 ++++ xklb/usage.py | 6 ++++++ xklb/utils/web.py | 3 +++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/xklb/mediafiles/process_media.py b/xklb/mediafiles/process_media.py index 4775e288..d6c41637 100644 --- a/xklb/mediafiles/process_media.py +++ b/xklb/mediafiles/process_media.py @@ -1,5 +1,6 @@ import argparse, math, os, sqlite3 from contextlib import suppress +from shutil import which from xklb import usage from xklb.mediadb import db_history @@ -88,6 +89,17 @@ def parse_args() -> argparse.Namespace: def collect_media(args) -> list[dict]: + FFMPEG_INSTALLED = which("ffmpeg") or which("ffmpeg.exe") + IM7_INSTALLED = which("magick") + CALIBRE_INSTALLED = which("ebook-convert") + + default_exts = ( + (consts.AUDIO_ONLY_EXTENSIONS if FFMPEG_INSTALLED else set()) + | (consts.VIDEO_EXTENSIONS if FFMPEG_INSTALLED else set()) + | (consts.IMAGE_EXTENSIONS - set(("avif",)) if IM7_INSTALLED else set()) + | (consts.CALIBRE_EXTENSIONS if CALIBRE_INSTALLED else set()) + ) + if args.database: db_history.create(args) @@ -96,13 +108,7 @@ def collect_media(args) -> list[dict]: except sqlite3.OperationalError: media = list(args.db.query(*sqlgroups.fs_sql(args, args.limit))) else: - media = arg_utils.gen_d( - args, - consts.AUDIO_ONLY_EXTENSIONS - | consts.VIDEO_EXTENSIONS - | consts.IMAGE_EXTENSIONS - set(("avif",)) - | consts.CALIBRE_EXTENSIONS, - ) + media = arg_utils.gen_d(args, default_exts) media = [d if "size" in d else file_utils.get_filesize(d) for d in media] return media diff --git a/xklb/mediafiles/process_text.py b/xklb/mediafiles/process_text.py index 6a2b024a..2797719d 100644 --- a/xklb/mediafiles/process_text.py +++ b/xklb/mediafiles/process_text.py @@ -154,6 +154,10 @@ def process_path(args, path): def process_text(): args = parse_args() + if not shutil.which("ebook-convert"): + print("Calibre is required for process-text") + raise SystemExit(1) + for path in gen_paths(args, consts.CALIBRE_EXTENSIONS): if not path.startswith("http"): path = str(Path(path).resolve()) diff --git a/xklb/usage.py b/xklb/usage.py index c173b0ec..dae626f8 100644 --- a/xklb/usage.py +++ b/xklb/usage.py @@ -1845,5 +1845,11 @@ def play(action) -> str: library process-media --invalid --no-valid --delete-unplayable video.db + If not installed, related file extensions will be skipped during scan: + + - FFmpeg is required for shrinking video and audio + - ImageMagick is required for shrinking images + - Calibre is required for shrinking eBooks + Inspired somewhat by https://nikkhokkho.sourceforge.io/?page=FileOptimizer """ diff --git a/xklb/utils/web.py b/xklb/utils/web.py index 7fb33147..eb172dc2 100644 --- a/xklb/utils/web.py +++ b/xklb/utils/web.py @@ -888,6 +888,9 @@ def __new__(cls, *args): def __init__(self, path): self._path = str(path) + def __fspath__(self): + return str(self) + @property def parent(self): scheme, netloc, path, params, query, fragment = urlparse(str(self))