From eeab8a4f39eae775261a78daac96013e30e42754 Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Mon, 25 Mar 2024 06:45:07 +0000 Subject: [PATCH] feat(dl): Automatically attach fonts used within SSAv4 subs The fonts must be within the /devine/fonts folder. This folder location can be changed in the config. If a font is missing it will warn the user and continue. Closes #82 --- .gitignore | 2 ++ CONFIG.md | 1 + devine/commands/dl.py | 23 +++++++++++++++++++++++ devine/core/config.py | 1 + 4 files changed, 27 insertions(+) diff --git a/.gitignore b/.gitignore index 58a7672..643061b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ *.pem *.bin *.db +*.ttf +*.otf device_cert device_client_id_blob device_private_key diff --git a/CONFIG.md b/CONFIG.md index 46f51ee..6ae23d7 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -122,6 +122,7 @@ The following directories are available and may be overridden, - `commands` - CLI Command Classes. - `services` - Service Classes. - `vaults` - Vault Classes. +- `fonts` - Font files (ttf or otf). - `downloads` - Downloads. - `temp` - Temporary files or conversions during download. - `cache` - Expiring data like Authorization tokens, or other misc data. diff --git a/devine/commands/dl.py b/devine/commands/dl.py index 1633cc0..b15fe0f 100644 --- a/devine/commands/dl.py +++ b/devine/commands/dl.py @@ -49,6 +49,7 @@ from devine.core.services import Services from devine.core.titles import Movie, Song, Title_T from devine.core.titles.episode import Episode from devine.core.tracks import Audio, Subtitle, Tracks, Video +from devine.core.tracks.attachment import Attachment from devine.core.utilities import get_binary_path, is_close_match, time_elapsed_since from devine.core.utils.click_types import LANGUAGE_RANGE, QUALITY_LIST, SEASON_RANGE, ContextData, MultipleChoice from devine.core.utils.collections import merge_dict @@ -597,6 +598,28 @@ class dl: if subtitle.codec != sub_format: subtitle.convert(sub_format) + with console.status("Checking Subtitles for Fonts..."): + font_names = [] + for subtitle in title.tracks.subtitles: + if subtitle.codec == Subtitle.Codec.SubStationAlphav4: + for line in subtitle.path.read_text("utf8").splitlines(): + if line.startswith("Style: "): + font_names.append(line.removesuffix("Style: ").split(",")[1]) + + font_count = 0 + for font_name in set(font_names): + family_dir = Path(config.directories.fonts, font_name) + if family_dir.exists(): + fonts = family_dir.glob("*.*tf") + for font in fonts: + title.tracks.add(Attachment(font, f"{font_name} ({font.stem})")) + font_count += 1 + else: + self.log.warning(f"Subtitle uses font [text2]{font_name}[/] but it could not be found...") + + if font_count: + self.log.info(f"Attached {font_count} fonts for the Subtitles") + with console.status("Repackaging tracks with FFMPEG..."): has_repacked = False for track in title.tracks: diff --git a/devine/core/config.py b/devine/core/config.py index 2c00756..ee9c647 100644 --- a/devine/core/config.py +++ b/devine/core/config.py @@ -17,6 +17,7 @@ class Config: commands = namespace_dir / "commands" services = namespace_dir / "services" vaults = namespace_dir / "vaults" + fonts = namespace_dir / "fonts" user_configs = Path(app_dirs.user_config_dir) data = Path(app_dirs.user_data_dir) downloads = Path.home() / "Downloads" / "devine"