Skip to content

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:

  1. its name says it ("French AC3 5.1", "VFF", "English SDH");
  2. 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;
  3. 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

NameTypeDefaultDescription
idint
numberOptional[int]
kindstr
languageOptional[str]
namestr''
textboolFalse
codec_idstr''
positionint0
codecstr''
channelsOptional[int]None
Source

Attributes

NameType
channelsOptional[int]
codecstr
codec_idstr
idint
kindstr
languageOptional[str]
namestr
numberOptional[int]
positionint
textbool

from_dictclassmethod

def from_dict(data: Dict[str, Any]) -> Track:

Parameters

NameTypeDescription
dataDict[str, Any]
Source

to_dict

def to_dict() -> Dict[str, Any]:
Source

TrackLanguageError

class TrackLanguageError

Bases: OSError

The languages could not be written; the video stays as it was.

Source

Functions

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

NameTypeDescription
filenamestr
titlesIterable[str]
originalOptional[str]
Source

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

NameTypeDefaultDescription
tracksList[Track]
filenamestr
titlesIterable[str]()
originalOptional[str]None
Source

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

NameTypeDefaultDescription
pathstrThe MKV.
titlesIterable[str]()The titles of the movie or series.
originalOptional[str]NoneIts original language.
release_nameOptional[str]NoneThe name the file had before its rename, whose tags (FRENCH, VOSTFR…) the new name no longer has; the name of path by default.
choicesOptional[Dict[Tuple[str, int], str]]NoneThe languages the user gave, by (kind, position): they win over what S.O.K guesses, for the tracks without a language.
autoboolTrueWhether 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.
Source

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

NameTypeDefaultDescription
namestr
kindstrAUDIO
originalOptional[str]None
Source

missing

def missing(tracks: Iterable[Track]) -> Dict[str, int]:

How many audio and subtitle tracks have no language.

Parameters

NameTypeDescription
tracksIterable[Track]
Source

mkvextract_path

def mkvextract_path() -> Optional[str]:

mkvextract, which gives the text of a subtitle (SOK_MKVEXTRACT).

Source

mkvpropedit_path

def mkvpropedit_path() -> Optional[str]:

mkvpropedit, which writes the languages (SOK_MKVPROPEDIT).

Source

original_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

NameTypeDescription
codeAny
Source

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

NameTypeDefaultDescription
tracksList[Track]
filenamestr
titlesIterable[str]()
originalOptional[str]None
Source

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

NameTypeDefaultDescription
pathstr
timeoutfloatREAD_TIMEOUT
Source

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

NameTypeDefaultDescription
filenamestr
titlesIterable[str]()
Source

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

NameTypeDefaultDescription
pathstr
tracksList[Track]
planDict[int, str]
timeoutfloatEXTRACT_TIMEOUT
Source

Attributes

NameValue
AUDIO'audio'
EXTRACT_TIMEOUT15 * 60
FROM_CHOICE'choice'
FROM_NAME'name'
FROM_ORIGINAL'original'
FROM_RELEASE'release'
FROM_TEXT'text'
KINDS(AUDIO, SUBTITLES)
LANGUAGE_CODEStuple(sorted(set(WORD_LANGUAGES.values())))
MAX_TEXT_BYTES1024 * 1024
MULTI'multi'
MULTI_RELEASE'multi'
ORIGINAL'original'
PICTURE'picture'
READ_TIMEOUT60
SEVERAL_AUDIO'several'
SHORT_NAME3
SUBTITLES'subtitles'
UNDEFINED('', 'und', 'zxx', 'mis')
UNKNOWN'unknown'
loggerlogging.getLogger(__name__)
Built with Kiln