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
This commit is contained in:
rlaphoenix 2024-03-25 06:45:07 +00:00
parent 057e4efb56
commit eeab8a4f39
4 changed files with 27 additions and 0 deletions

2
.gitignore vendored
View File

@ -9,6 +9,8 @@
*.pem *.pem
*.bin *.bin
*.db *.db
*.ttf
*.otf
device_cert device_cert
device_client_id_blob device_client_id_blob
device_private_key device_private_key

View File

@ -122,6 +122,7 @@ The following directories are available and may be overridden,
- `commands` - CLI Command Classes. - `commands` - CLI Command Classes.
- `services` - Service Classes. - `services` - Service Classes.
- `vaults` - Vault Classes. - `vaults` - Vault Classes.
- `fonts` - Font files (ttf or otf).
- `downloads` - Downloads. - `downloads` - Downloads.
- `temp` - Temporary files or conversions during download. - `temp` - Temporary files or conversions during download.
- `cache` - Expiring data like Authorization tokens, or other misc data. - `cache` - Expiring data like Authorization tokens, or other misc data.

View File

@ -49,6 +49,7 @@ from devine.core.services import Services
from devine.core.titles import Movie, Song, Title_T from devine.core.titles import Movie, Song, Title_T
from devine.core.titles.episode import Episode from devine.core.titles.episode import Episode
from devine.core.tracks import Audio, Subtitle, Tracks, Video 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.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.click_types import LANGUAGE_RANGE, QUALITY_LIST, SEASON_RANGE, ContextData, MultipleChoice
from devine.core.utils.collections import merge_dict from devine.core.utils.collections import merge_dict
@ -597,6 +598,28 @@ class dl:
if subtitle.codec != sub_format: if subtitle.codec != sub_format:
subtitle.convert(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..."): with console.status("Repackaging tracks with FFMPEG..."):
has_repacked = False has_repacked = False
for track in title.tracks: for track in title.tracks:

View File

@ -17,6 +17,7 @@ class Config:
commands = namespace_dir / "commands" commands = namespace_dir / "commands"
services = namespace_dir / "services" services = namespace_dir / "services"
vaults = namespace_dir / "vaults" vaults = namespace_dir / "vaults"
fonts = namespace_dir / "fonts"
user_configs = Path(app_dirs.user_config_dir) user_configs = Path(app_dirs.user_config_dir)
data = Path(app_dirs.user_data_dir) data = Path(app_dirs.user_data_dir)
downloads = Path.home() / "Downloads" / "devine" downloads = Path.home() / "Downloads" / "devine"