Skip to content

Commit

Permalink
process-media: filter exts if supporting programs not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
chapmanjacobd committed Oct 23, 2024
1 parent b075ae5 commit eb5453f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
20 changes: 13 additions & 7 deletions xklb/mediafiles/process_media.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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

Expand Down
4 changes: 4 additions & 0 deletions xklb/mediafiles/process_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 6 additions & 0 deletions xklb/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
3 changes: 3 additions & 0 deletions xklb/utils/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit eb5453f

Please sign in to comment.