Skip to content

sok.file_operations.subtitle_language

The language of a subtitle file, from its name or from its text.

The name is read first: "fr", "fre", "French", "VOSTFR" and the like say the language. When the name says nothing, the text is read and its language guessed with langdetect, only when the guess is sure: enough dialogue, a high probability, and both halves of the text agree. A subtitle is never given a language S.O.K is unsure of.

The language is written as a two-letter code ("fr"), the form Kodi, Jellyfin and Plex read.

Functions

dialogue

def dialogue(text: str, extension: str) -> List[str]:

The lines spoken in a subtitle, without numbers, timings or tags.

Parameters

NameTypeDescription
textstr
extensionstr
Source

flags_from_name

def flags_from_name(name: str) -> List[str]:

The flags of a name, in the order of FLAGS ("English SDH" → ["sdh"]).

Parameters

NameTypeDescription
namestr
Source

guess_language

def guess_language(lines: List[str]) -> Optional[str]:

The language of some dialogue, only when the guess is sure.

Parameters

NameTypeDescription
linesList[str]
Source

language_from_name

def language_from_name(name: str) -> Optional[str]:

The language a name says, or None ("Movie.fr.forced" → "fr").

"Portuguese (Brazil)" is Brazilian Portuguese, not Portuguese.

Parameters

NameTypeDescription
namestr
Source

language_from_text

def language_from_text(path: Path) -> Optional[str]:

The language of a subtitle file read from its content, or None.

A VobSub .idx says it on its "id:" lines; a text subtitle is guessed from its dialogue. A picture subtitle (VobSub .sub) cannot be read.

Parameters

NameTypeDescription
pathPath
Source

name_words

def name_words(name: str) -> List[str]:

The words of a name, folded: "2_English [SDH]" → ["2", "english", "sdh"].

Parameters

NameTypeDescription
namestr
Source

read_text

def read_text(path: Path) -> Optional[str]:

The text of a subtitle file, or None when its encoding is unsure.

UTF-8 and UTF-16 (with a byte order mark) are read as they are. Other files are read as Windows-1252 only when nearly all their letters are plain ASCII: a Cyrillic or Greek file in an old code page would come out as nonsense and get a wrong language.

Parameters

NameTypeDescription
pathPath
Source

Attributes

NameValue
FLAGS('forced', 'sdh', 'cc', 'hi')
MAX_BYTES1024 * 1024
MAX_SAMPLE20000
MIN_LETTERS600
MIN_LINES30
MIN_PROBABILITY0.95
TEXT_EXTENSIONS('.srt', '.ass', '.ssa', '.vtt', '.sub')
WORD_LANGUAGES{word: code for code, words in _LANGUAGES.items() for word in words}
loggerlogging.getLogger(__name__)
Built with Kiln