sok.file_operations.track_languages
The languages of the audio and subtitle tracks inside the videos.
A track without a language ("und") makes Jellyfin, Plex or a TV pick the wrong sound or subtitles. S.O.K Server reads the tracks of each video it analyzes (mkvmerge of MKVToolNix, which reads MKV, MP4 and TS alike) and says how many have no language. When the fill_track_languages setting is on, a rename writes the language of those it is sure of into the MKV, with mkvpropedit: only the header changes, the file is not written again.
A track gets a language only when it is sure, in this order:
- its name says it ("French AC3 5.1", "VFF", "English SDH");
- a text subtitle (SRT, ASS) is read, with the strict guess of the external subtitles (subtitle_language.guess_language); a picture subtitle (PGS, VobSub) has only its name;
- the only audio track of a video gets the language the file name gives after the year or the episode (FRENCH, VFF, GERMAN…). With no such word, or VOSTFR (original version), it gets the original language of the movie or series (TMDB, TVDB). A MULTI file, or one with several audio tracks, keeps what its track names say: never a guess.
Never with hard links (the file is the torrent's), and in an MKV only: an MP4 or a TS gets its languages when it is converted to MKV (remux.py).
Trackdataclass
class Track( id: int, number: Optional[int], kind: str, language: Optional[str], name: str = '', text: bool = False, codec_id: str = '', position: int = 0, codec: str = '', channels: Optional[int] = None,)An audio or subtitle track of a video, as mkvmerge reads it.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
id | int | ||
number | Optional[int] | ||
kind | str | ||
language | Optional[str] | ||
name | str | '' | |
text | bool | False | |
codec_id | str | '' | |
position | int | 0 | |
codec | str | '' | |
channels | Optional[int] | None |
Attributes
| Name | Type |
|---|---|
channels | Optional[int] |
codec | str |
codec_id | str |
id | int |
kind | str |
language | Optional[str] |
name | str |
number | Optional[int] |
position | int |
text | bool |
from_dictclassmethod
def from_dict(data: Dict[str, Any]) -> Track:Parameters
| Name | Type | Description |
|---|---|---|
data | Dict[str, Any] |
to_dict
def to_dict() -> Dict[str, Any]:TrackLanguageError
class TrackLanguageErrorBases: OSError
The languages could not be written; the video stays as it was.
SourceFunctions
audio_from_release
def audio_from_release( filename: str, titles: Iterable[str], original: Optional[str],) -> Optional[str]:The language of the only audio track of a release, or None.
One language word gives it; none, or VOSTFR, gives the original language; MULTI or two languages give nothing.
Parameters
| Name | Type | Description |
|---|---|---|
filename | str | |
titles | Iterable[str] | |
original | Optional[str] |
explain_languages
def explain_languages( tracks: List[Track], filename: str, titles: Iterable[str] = (), original: Optional[str] = None,) -> Dict[int, Tuple[Optional[str], str]]:What S.O.K does with each track that has no language, by its mkvmerge id: (language, reason), the language None when it is not sure.
Reasons: FROM_NAME (the track name), FROM_RELEASE (the file name), FROM_ORIGINAL (the original language), FROM_TEXT (a text subtitle, read while renaming); without a language: MULTI_RELEASE, SEVERAL_AUDIO, PICTURE (a picture subtitle without a name that says it), UNKNOWN.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
tracks | List[Track] | ||
filename | str | ||
titles | Iterable[str] | () | |
original | Optional[str] | None |
fill_languages
def fill_languages( path: str, titles: Iterable[str] = (), original: Optional[str] = None, release_name: Optional[str] = None, choices: Optional[Dict[Tuple[str, int], str]] = None, auto: bool = True,) -> Dict[str, List[str]]:Write the languages S.O.K is sure of into the tracks of an MKV.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
path | str | The MKV. | |
titles | Iterable[str] | () | The titles of the movie or series. |
original | Optional[str] | None | Its original language. |
release_name | Optional[str] | None | The name the file had before its rename, whose tags (FRENCH, VOSTFR…) the new name no longer has; the name of path by default. |
choices | Optional[Dict[Tuple[str, int], str]] | None | The languages the user gave, by (kind, position): they win over what S.O.K guesses, for the tracks without a language. |
auto | bool | True | Whether to write what S.O.K guesses (fill_track_languages); without it, only the choices are written. |
Returns
Dict[str, List[str]]The languages written, per kind: {"audio": ["fr"], "subtitles":Dict[str, List[str]]["en"]}; empty when there was nothing to write.
Raises
TrackLanguageErrormkvpropedit is missing or failed, or the file does not have the languages afterwards.
language_from_track_name
def language_from_track_name( name: str, kind: str = AUDIO, original: Optional[str] = None,) -> Optional[str]:The language a track name says, only when it says one.
A code ("FR", "ENG") counts in a short name only: "Commentary: is it true?" is not Italian. For a sound, "VO" or "Original" is the original language; for a subtitle, "VOSTFR" is French.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
name | str | ||
kind | str | AUDIO | |
original | Optional[str] | None |
missing
def missing(tracks: Iterable[Track]) -> Dict[str, int]:How many audio and subtitle tracks have no language.
Parameters
| Name | Type | Description |
|---|---|---|
tracks | Iterable[Track] |
mkvextract_path
def mkvextract_path() -> Optional[str]:mkvextract, which gives the text of a subtitle (SOK_MKVEXTRACT).
Sourcemkvpropedit_path
def mkvpropedit_path() -> Optional[str]:mkvpropedit, which writes the languages (SOK_MKVPROPEDIT).
Sourceoriginal_language
def original_language(code: Any) -> Optional[str]:The original language of a search result as a short code: TMDB gives "en", TVDB "eng".
Parameters
| Name | Type | Description |
|---|---|---|
code | Any |
plan_languages
def plan_languages( tracks: List[Track], filename: str, titles: Iterable[str] = (), original: Optional[str] = None,) -> Dict[int, str]:The languages S.O.K is sure of for the tracks that have none, by their mkvmerge id; without reading the subtitles (see subtitle_texts).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
tracks | List[Track] | ||
filename | str | ||
titles | Iterable[str] | () | |
original | Optional[str] | None |
read_tracks
def read_tracks( path: str, timeout: float = READ_TIMEOUT,) -> Optional[List[Track]]:The audio and subtitle tracks of a video, or None when mkvmerge is missing or cannot read it.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
path | str | ||
timeout | float | READ_TIMEOUT |
release_words
def release_words(filename: str, titles: Iterable[str] = ()) -> List[str]:The tags of a release name: its words after the last year or episode, or without one, its words that are not those of a title.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
filename | str | ||
titles | Iterable[str] | () |
subtitle_texts
def subtitle_texts( path: str, tracks: List[Track], plan: Dict[int, str], timeout: float = EXTRACT_TIMEOUT,) -> Dict[int, str]:The languages read from the text of the subtitle tracks that have none and that the plan has not given one.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
path | str | ||
tracks | List[Track] | ||
plan | Dict[int, str] | ||
timeout | float | EXTRACT_TIMEOUT |
Attributes
| Name | Value |
|---|---|
AUDIO | 'audio' |
EXTRACT_TIMEOUT | 15 * 60 |
FROM_CHOICE | 'choice' |
FROM_NAME | 'name' |
FROM_ORIGINAL | 'original' |
FROM_RELEASE | 'release' |
FROM_TEXT | 'text' |
KINDS | (AUDIO, SUBTITLES) |
LANGUAGE_CODES | tuple(sorted(set(WORD_LANGUAGES.values()))) |
MAX_TEXT_BYTES | 1024 * 1024 |
MULTI | 'multi' |
MULTI_RELEASE | 'multi' |
ORIGINAL | 'original' |
PICTURE | 'picture' |
READ_TIMEOUT | 60 |
SEVERAL_AUDIO | 'several' |
SHORT_NAME | 3 |
SUBTITLES | 'subtitles' |
UNDEFINED | ('', 'und', 'zxx', 'mis') |
UNKNOWN | 'unknown' |
logger | logging.getLogger(__name__) |