Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ def parse_args():
default=False,
help="Extract bitmap (default: false)",
)

parser.add_argument(
"--extractor-config-extract-disable-sdh",
dest="extractor_config_extract_sdh",
action="store_false",
default=True,
help="Disable sdh subtitles extraction",
)

parser.add_argument(
"--extractor-config-overwrite",
action="store_true",
Expand Down Expand Up @@ -162,6 +171,7 @@ def parse_args():
EXTRACTOR_EXCLUDE_FILE = config.extractor_exclude_file
EXTRACTOR_EXCLUDE_APPEND = config.extractor_exclude_append
EXTRACTOR_EXTRACT_BITMAP = config.extractor_extract_bitmap
EXTRACTOR_CONFIG_EXTRACT_SDH = config.extractor_config_extract_sdh
EXTRACTOR_CONFIG_OVERWRITE = config.extractor_config_overwrite
EXTRACTOR_CONFIG_DESIRED_FORMATS = config.extractor_config_desired_formats
EXTRACTOR_CONFIG_LANGUAGES = config.extractor_config_languages
Expand Down
25 changes: 23 additions & 2 deletions extract/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
Configuration management for subtitle extraction.
"""

import logging
from dataclasses import dataclass

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from extract.prober import StreamInfo

logger = logging.getLogger(__name__)


@dataclass
class ExtractorConfig:
Expand All @@ -15,6 +23,19 @@ class ExtractorConfig:
# target languages
languages: list[str] | tuple[str] = ("all",)
unknown_language_as: str = "unknown"
extract_sdh: bool = True

def is_stream_wanted(self, stream: "StreamInfo") -> bool:

if self.extract_sdh == False and stream.is_sdh():
logger.debug(f"Skipping unwanted SDH stream ({stream.index})")
return False

is_wanted_lang = "all" in self.languages or stream.language in self.languages

if not is_wanted_lang:
logger.debug(
f"Skipping unwanted language '{stream.language}' for stream {stream.index}"
)

def is_language_wanted(self, language: str) -> bool:
return "all" in self.languages or language in self.languages
return is_wanted_lang
6 changes: 2 additions & 4 deletions extract/extractors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ def should_extract_stream(
return False
logger.debug(f"Overwriting existing file: {output_path}")

if not self.config.is_language_wanted(stream.language):
logger.debug(
f"Skipping unwanted language '{stream.language}' for stream {stream.index}"
)
if not self.config.is_stream_wanted(stream):
logger.debug(f"Skipping unwanted stream {stream.index}")
return False

# Check codec support
Expand Down
26 changes: 25 additions & 1 deletion extract/prober.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
logger = logging.getLogger(__name__)


# {
# "streams": [
# {
# "index": 2,
# "codec_name": "subrip",
# "codec_type": "subtitle",
# "disposition": {
# "hearing_impaired": 1,
# "default": 0,
# "forced": 0
# },
# "tags": {
# "language": "eng"
# }
# }
# ]
# }


class StreamInfo:
"""Represents information about a subtitle stream."""

Expand All @@ -22,7 +41,9 @@ def __init__(self, stream_data: dict[str, Any]):
self.index = stream_data.get("index")
self.codec_name = stream_data.get("codec_name")
self.codec_type = stream_data.get("codec_type")
self.data.setdefault("tags", {})

self.data["tags"] = stream_data.get("tags") or {}
self.data["disposition"] = stream_data.get("disposition") or {}

@property
def language(self) -> str:
Expand All @@ -41,6 +62,9 @@ def title(self) -> str:
def disposition(self) -> dict[str, int]:
return self.data.get("disposition", {})

def is_sdh(self) -> bool:
return bool(self.disposition.get("hearing_impaired", 0))

def is_forced(self) -> bool:
return bool(self.disposition.get("forced", 0))

Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def main(mainpath: str):
"desired_formats": config.EXTRACTOR_CONFIG_DESIRED_FORMATS,
"languages": config.EXTRACTOR_CONFIG_LANGUAGES,
"unknown_language_as": config.EXTRACTOR_CONFIG_UNKNOWN_LANGUAGE_AS,
"extract_sdh": config.EXTRACTOR_CONFIG_EXTRACT_SDH,
},
}
)
Expand Down
Loading