Skip to content

sok.file_operations.subtitles

Subtitles that follow their video.

A subtitle whose name starts with the name of a video goes with it: Movie.srt, Movie.fr.srt and Movie.en.forced.srt follow Movie.mkv. It is renamed like the video and keeps what follows the video's name, so Movie.fr.srt becomes "Dune (2021).fr.srt" next to "Dune (2021).mkv".

When that name says no language, the language read from the text is added when it is sure: Movie.srt in French becomes "Dune (2021).fr.srt".

The subtitles of a "Subs" folder next to the video go with it too: Subs/2_English.srt for the only video of the folder, or Subs/

Functions

find_subs_folder

def find_subs_folder(
video: Path,
video_extensions: Iterable[str],
) -> List[Path]:

The subtitles of a video in a "Subs" folder next to it.

Subs/

Parameters

NameTypeDescription
videoPath
video_extensionsIterable[str]

Returns

  • List[Path]The subtitle files, in the order of their leading number
  • List[Path]("2_English.srt" before "10_French.srt").
Source

find_subtitles

def find_subtitles(
video: Path,
video_extensions: Iterable[str],
) -> List[Tuple[Path, str]]:

The subtitles of a video, next to it.

When two videos of the folder can claim a subtitle (Movie.mkv and Movie.Part2.mkv for Movie.Part2.srt), the longest name wins.

Parameters

NameTypeDescription
videoPathThe video file.
video_extensionsIterable[str]Extensions of the videos, to find the other ones.

Returns

  • List[Tuple[Path, str]](subtitle, suffix) pairs; the suffix is what follows the video's
  • List[Tuple[Path, str]]name, such as ".fr.srt".
Source

move_subtitles

def move_subtitles(
video: Path,
new_video: str,
video_extensions: Iterable[str],
skip_duplicates: bool = False,
backup: bool = False,
mode: str = MOVE,
dry_run: bool = False,
) -> Dict[str, List[Dict[str, Any]]]:

Move the subtitles of a video next to its new place, under its new name.

Those beside the video and those of its Subs folder go, with their language added when the name lacks it and the text says it surely (see plan_subtitles). The history records each subtitle as following the video, so undoing the video puts them back too.

Parameters

NameTypeDefaultDescription
videoPathWhere the video was.
new_videostrWhere the video went.
video_extensionsIterable[str]Extensions of the videos.
skip_duplicatesboolFalseLeave a subtitle in place when its new name exists.
backupboolFalseKeep a replaced subtitle as '.backup'.
modestrMOVEMOVE, LINK or COPY, like the video.
dry_runboolFalseOnly say where they would go.

Returns

  • Dict[str, List[Dict[str, Any]]]"moved", "skipped" and "errors" lists, like the organize reports.
Source

plan_subtitles

def plan_subtitles(
video: Path,
new_video: str,
video_extensions: Iterable[str],
) -> List[Tuple[str, str]]:

Where the subtitles of a video go when it goes to new_video.

Parameters

NameTypeDescription
videoPath
new_videostr
video_extensionsIterable[str]

Returns

  • List[Tuple[str, str]](source, target) pairs of paths; two targets never collide.
Source

Attributes

NameValue
SUBS_FOLDERS('subs', 'subtitles')
SUBTITLE_EXTENSIONS('.srt', '.ass', '.ssa', '.sub', '.idx', '.vtt')
loggerlogging.getLogger(__name__)
Built with Kiln