Skip to content

sok.file_operations.remux

Put a video in an MKV file while renaming it, without losing quality.

S.O.K Server can convert the MP4, M4V, MOV and TS videos it renames (the convert_to_mkv setting). Only the container changes ("remux"): mkvmerge, of MKVToolNix, copies the image, the sound and the subtitles as they are, in about the time of a copy. It is a new file, so never with hard links: a hard link would leave the torrent file the old one and take twice the room.

The MKV is written next to its destination under a temporary name, checked (the same tracks of each kind, the same duration), then put in place. Only then does a move delete the original; a copy keeps it. mkvmerge gives the duration of an MKV only: the original's is read by mutagen (MP4, M4V, MOV) or from the timestamps of its first and last packets (TS); a move whose original has no duration that can be read is refused. A conversion that fails or is not right leaves the original untouched and raises RemuxError. AVI files are not converted: their sound often goes out of sync.

RemuxError

class RemuxError

Bases: OSError

A video could not be converted to MKV; the original is untouched.

Source

Functions

convert_to_mkv

def convert_to_mkv(
source: str,
destination: str,
backup: bool = False,
follows: Optional[str] = None,
mode: str = MOVE,
) -> Optional[str]:

Convert a video to an MKV at its destination, like move_file moves it.

Parameters

NameTypeDefaultDescription
sourcestrThe video, of a format of CONVERTIBLE_EXTENSIONS.
destinationstrThe new path of the video, ending with .mkv.
backupboolFalseWhether to keep a file already at the destination as '.backup'.
followsOptional[str]NoneNew path of the file this one goes with.
modestrMOVEMOVE deletes the original once the MKV is checked; COPY keeps it. LINK is refused: the MKV is a new file.

Returns

  • Optional[str]Path of the created backup, or None if no backup was made.

Raises

  • RemuxErrormkvmerge is missing, fails, or makes a file that is not right. The original and the destination are left as they were.
Source

convertible

def convertible(path: str) -> bool:

Whether a video is of a format converted to MKV.

Parameters

NameTypeDescription
pathstr
Source

mkv_name

def mkv_name(filename: str) -> str:

The name of a video once converted: .mkv for the formats converted.

Parameters

NameTypeDescription
filenamestr
Source

mkvmerge_path

def mkvmerge_path() -> Optional[str]:

The mkvmerge program, or None when MKVToolNix is not installed.

The SOK_MKVMERGE environment variable can give its path.

Source

source_duration

def source_duration(path: str) -> Optional[int]:

The duration of a video to convert in nanoseconds, or None.

Parameters

NameTypeDescription
pathstr
Source

Attributes

NameValue
CONVERTIBLE_EXTENSIONS('.mp4', '.m4v', '.mov', '.ts')
DURATION_TOLERANCE_NS2000000000
MIN_SIZE_RATIO0.5
MKV'.mkv'
TS_CLOCK90000
TS_PACKET_SIZES(188, 192)
TS_SCAN4 * 1024 * 1024
TS_SYNC71
TS_WRAP1 << 33
loggerlogging.getLogger(__name__)
Built with Kiln