Skip to content

Commit 33d0cc2

Browse files
committed
Add new flag --use-ass-in-mkv
1 parent 057bc2c commit 33d0cc2

3 files changed

Lines changed: 40 additions & 8 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pip install FontCollector
1717
## FontCollector Usage
1818
```console
1919
$ fontcollector --help
20-
usage: fontcollector [-h] --input INPUT [INPUT ...] [-mkv MKV] [--output OUTPUT] [-mkvtoolnix MKVTOOLNIX] [--delete-fonts] [--additional-fonts ADDITIONAL_FONTS [ADDITIONAL_FONTS ...]]
20+
usage: fontcollector [-h] [--input INPUT [INPUT ...]] [-mkv MKV] [--use-ass-in-mkv] [--output OUTPUT] [-mkvtoolnix MKVTOOLNIX] [--delete-fonts] [--additional-fonts ADDITIONAL_FONTS [ADDITIONAL_FONTS ...]]
2121
[--additional-fonts-recursive ADDITIONAL_FONTS_RECURSIVE [ADDITIONAL_FONTS_RECURSIVE ...]] [--exclude-system-fonts] [--collect-draw-fonts] [--dont-convert-variable-to-collection] [--logging [LOGGING]]
2222

2323
FontCollector for Advanced SubStation Alpha file.
@@ -28,6 +28,8 @@ options:
2828
Subtitles file. Must be an ASS file/directory. You can specify more than one .ass file/path.
2929
-mkv MKV
3030
Video where the fonts will be merge. Must be a Matroska file.
31+
--use-ass-in-mkv, -ass-mkv
32+
If specified, it will use the .ass file muxed to the mkv and collect those fonts and mux them to the mkv. If not specified, it will do nothing.
3133
--output OUTPUT, -o OUTPUT
3234
Destination path of the font. If -o and -mkv aren't specified, it will be the current path.
3335
-mkvtoolnix MKVTOOLNIX

font_collector/__main__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import shutil
33
from pathlib import Path
44
from sys import argv
5+
from tempfile import TemporaryDirectory
56

67
from . import _handler
78
from .ass.ass_document import AssDocument
@@ -12,7 +13,7 @@
1213
FontLoader,
1314
FontSelectionStrategyLibass
1415
)
15-
from .mkvtoolnix.mkvpropedit import MKVPropedit
16+
from .mkvtoolnix import MKVPropedit, MKVExtract
1617
from .parse_arguments import parse_arguments
1718

1819
_logger = logging.getLogger(__name__)
@@ -23,6 +24,7 @@ def main() -> None:
2324
ass_files_path,
2425
output_directory,
2526
mkv_path,
27+
use_ass_in_mkv,
2628
delete_fonts,
2729
additional_fonts_path,
2830
additional_fonts_recursive_path,
@@ -52,6 +54,20 @@ def main() -> None:
5254

5355
fonts_file_found.update(collect_subtitle_fonts(subtitle, font_collection, font_strategy, collect_draw_fonts, convert_variable_to_collection, output_directory))
5456
_logger.info("")
57+
58+
if use_ass_in_mkv:
59+
with TemporaryDirectory() as tmp_dir:
60+
assert isinstance(mkv_path, Path)
61+
mkv_ass_files = MKVExtract.get_mkv_ass_files(mkv_path, Path(tmp_dir))
62+
for mkv_ass_file in mkv_ass_files:
63+
subtitle = AssDocument.from_file(mkv_ass_file.filename)
64+
log_msg = f"Loaded successfully the .ass stream at index {mkv_ass_file.mkv_id}"
65+
if mkv_ass_file.track_name:
66+
log_msg += f" - \"{mkv_ass_file.track_name}\""
67+
_logger.info(log_msg)
68+
69+
fonts_file_found.update(collect_subtitle_fonts(subtitle, font_collection, font_strategy, collect_draw_fonts, convert_variable_to_collection, output_directory))
70+
_logger.info("")
5571

5672
if mkv_path is not None:
5773
if delete_fonts:

font_collector/parse_arguments.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from .mkvtoolnix.mkv_utils import MKVUtils
88

99

10-
def __parse_input_file(ass_input: list[Path]) -> list[Path]:
10+
def __parse_input_file(ass_input: Optional[list[Path]]) -> list[Path]:
11+
if ass_input is None:
12+
return []
1113

1214
ass_files_path = []
1315
for input in ass_input:
@@ -33,6 +35,7 @@ def parse_arguments() -> tuple[
3335
Path,
3436
Union[Path, None],
3537
bool,
38+
bool,
3639
Iterable[Path],
3740
Iterable[Path],
3841
bool,
@@ -56,7 +59,6 @@ def parse_arguments() -> tuple[
5659
"-i",
5760
nargs="+",
5861
type=Path,
59-
required=True,
6062
help="""
6163
Subtitles file. Must be an ASS file/directory. You can specify more than one .ass file/path.
6264
""",
@@ -68,6 +70,14 @@ def parse_arguments() -> tuple[
6870
Video where the fonts will be merge. Must be a Matroska file.
6971
""",
7072
)
73+
parser.add_argument(
74+
"--use-ass-in-mkv",
75+
"-ass-mkv",
76+
action="store_true",
77+
help="""
78+
If specified, it will use the .ass file muxed to the mkv and collect those fonts and mux them to the mkv. If not specified, it will do nothing.
79+
""",
80+
)
7181
parser.add_argument(
7282
"--output",
7383
"-o",
@@ -150,12 +160,9 @@ def parse_arguments() -> tuple[
150160

151161
# Parse args
152162
ass_files_path = __parse_input_file(args.input)
153-
154-
if len(ass_files_path) == 0:
155-
raise RuntimeError("The specified file(s)/folder(s) doesn't exist or the folder(s) doesn't contains any .ass file.")
156-
157163
output_directory = args.output
158164
mkv_path = args.mkv
165+
use_ass_in_mkv = args.use_ass_in_mkv
159166
delete_fonts = args.delete_fonts
160167
additional_fonts = args.additional_fonts
161168
additional_fonts_recursive = args.additional_fonts_recursive
@@ -164,6 +171,12 @@ def parse_arguments() -> tuple[
164171
convert_variable_to_collection = args.dont_convert_variable_to_collection
165172
logging_file_path = args.logging
166173

174+
if len(ass_files_path) == 0 and not use_ass_in_mkv:
175+
raise RuntimeError("The specified file(s)/folder(s) doesn't exist or the folder(s) doesn't contains any .ass file.")
176+
177+
if use_ass_in_mkv and mkv_path is None:
178+
raise RuntimeError("You need to add the flag `-mkv` to use the flag `--use-ass-in-mkv`.")
179+
167180
if args.mkvtoolnix:
168181
if not mkv_path:
169182
raise RuntimeError("-mkvtoolnix requires --mkv option.")
@@ -173,6 +186,7 @@ def parse_arguments() -> tuple[
173186
ass_files_path,
174187
output_directory,
175188
mkv_path,
189+
use_ass_in_mkv,
176190
delete_fonts,
177191
additional_fonts,
178192
additional_fonts_recursive,

0 commit comments

Comments
 (0)