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 RemuxErrorBases: OSError
A video could not be converted to MKV; the original is untouched.
SourceFunctions
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
| Name | Type | Default | Description |
|---|---|---|---|
source | str | The video, of a format of CONVERTIBLE_EXTENSIONS. | |
destination | str | The new path of the video, ending with .mkv. | |
backup | bool | False | Whether to keep a file already at the destination as
' |
follows | Optional[str] | None | New path of the file this one goes with. |
mode | str | MOVE | MOVE 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.
convertible
def convertible(path: str) -> bool:Whether a video is of a format converted to MKV.
Parameters
| Name | Type | Description |
|---|---|---|
path | str |
mkv_name
def mkv_name(filename: str) -> str:The name of a video once converted: .mkv for the formats converted.
Parameters
| Name | Type | Description |
|---|---|---|
filename | str |
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.
Sourcesource_duration
def source_duration(path: str) -> Optional[int]:The duration of a video to convert in nanoseconds, or None.
Parameters
| Name | Type | Description |
|---|---|---|
path | str |
Attributes
| Name | Value |
|---|---|
CONVERTIBLE_EXTENSIONS | ('.mp4', '.m4v', '.mov', '.ts') |
DURATION_TOLERANCE_NS | 2000000000 |
MIN_SIZE_RATIO | 0.5 |
MKV | '.mkv' |
TS_CLOCK | 90000 |
TS_PACKET_SIZES | (188, 192) |
TS_SCAN | 4 * 1024 * 1024 |
TS_SYNC | 71 |
TS_WRAP | 1 << 33 |
logger | logging.getLogger(__name__) |