Skip to content

muxtools.utils.types

__all__ module-attribute

__all__ = ['PathLike', 'Paths', 'Trim', 'TrackType', 'AudioFrame', 'AudioStats', 'AudioInfo', 'Chapter', 'LooseChapter', 'DitherType', 'LossyWavQuality', 'VideoMeta', 'TimeScale', 'FileMixin']

PathLike module-attribute

PathLike = Union[Path, str, None]

Trim module-attribute

Trim = tuple[int | None, int | None]

Paths module-attribute

Paths = Union[PathLike, list[PathLike]]

LooseChapter module-attribute

LooseChapter = tuple[timedelta | int, Optional[str]]

Chapter type where the time can be a frame number or timedelta.

The chapters class will normalize these to just timedeltas.

Chapter module-attribute

Chapter = tuple[timedelta, Optional[str]]

TimeSourceT module-attribute

TimeSourceT = PathLike | Fraction | float | list | VideoMeta | ABCTimestamps | tuple[Path | str, int]

The source of timestamps/timecodes.

For actual timestamps, this can be a timestamps (v1/v2/v4) file, a video file or a list of integers.

You can also pass a tuple of some form of path to a video file and a relative video track index.

For FPS based timestamps, this can be a Fraction object, a float or even a string representing a fraction.

Like '24000/1001'.

Can also be an already instantiated Timestamps class from the videotimestamps library.

None will usually fallback to 24000/1001 but exact behavior might differ based on the target function.

TimeScaleT module-attribute

TimeScaleT = TimeScale | Fraction | int | None

Unit of time (in seconds) in terms of which frame timestamps are represented.

While you can pass an int, the needed type is always a Fraction and will be converted via Fraction(your_int).

None will usually fallback to a generic mkv timescale but exact behavior might differ based on the target function.

TrackType

Bases: IntEnum

Source code in muxtools/utils/types.py
class TrackType(IntEnum):
    VIDEO = 1
    AUDIO = 2
    SUB = 3
    ATTACHMENT = 4
    CHAPTERS = 5
    MKV = 6

VIDEO class-attribute instance-attribute

VIDEO = 1

AUDIO class-attribute instance-attribute

AUDIO = 2

SUB class-attribute instance-attribute

SUB = 3

ATTACHMENT class-attribute instance-attribute

ATTACHMENT = 4

CHAPTERS class-attribute instance-attribute

CHAPTERS = 5

MKV class-attribute instance-attribute

MKV = 6

VideoMeta dataclass

VideoMeta
Source code in muxtools/utils/types.py
@dataclass
class VideoMeta:
    pts: list[int]
    fps: Fraction
    timescale: Fraction
    source: str

    def to_json(self) -> str:
        return json.dumps(asdict(self), cls=FractionEncoder, indent=4)

    @staticmethod
    def from_json(file: PathLike) -> "VideoMeta":
        from .files import ensure_path_exists

        with open(ensure_path_exists(file, VideoMeta), "r", encoding="utf-8") as f:
            meta_json = json.loads(f.read(), object_hook=fraction_hook)
            return VideoMeta(**meta_json)

pts instance-attribute

pts: list[int]

fps instance-attribute

fps: Fraction

timescale instance-attribute

timescale: Fraction

source instance-attribute

source: str

to_json

to_json
Source code in muxtools/utils/types.py
def to_json(self) -> str:
    return json.dumps(asdict(self), cls=FractionEncoder, indent=4)

from_json staticmethod

from_json
Source code in muxtools/utils/types.py
@staticmethod
def from_json(file: PathLike) -> "VideoMeta":
    from .files import ensure_path_exists

    with open(ensure_path_exists(file, VideoMeta), "r", encoding="utf-8") as f:
        meta_json = json.loads(f.read(), object_hook=fraction_hook)
        return VideoMeta(**meta_json)

TimeScale

Bases: IntEnum

Convenience enum for some common timescales.

Source code in muxtools/utils/types.py
class TimeScale(IntEnum):
    """Convenience enum for some common timescales."""

    MKV = 1000
    """Typical matroska timescale"""
    MATROSKA = MKV
    """Alias for MKV"""
    M2TS = 90000
    """Typical m2ts timescale"""

MKV class-attribute instance-attribute

MKV = 1000

Typical matroska timescale

MATROSKA class-attribute instance-attribute

MATROSKA = MKV

Alias for MKV

M2TS class-attribute instance-attribute

M2TS = 90000

Typical m2ts timescale

DitherType

Bases: IntEnum

FFMPEG Dither Methods, see https://ffmpeg.org/ffmpeg-resampler.html#Resampler-Options

Source code in muxtools/utils/types.py
class DitherType(IntEnum):
    """
    FFMPEG Dither Methods, see https://ffmpeg.org/ffmpeg-resampler.html#Resampler-Options
    """

    RECTANGULAR = 1
    TRIANGULAR = 2  # Allegedly SoX's default
    TRIANGULAR_HP = 3
    LIPSHITZ = 4
    SHIBATA = 5  # Foobar uses this for example
    LOW_SHIBATA = 6
    HIGH_SHIBATA = 7
    F_WEIGHTED = 8
    MODIFIED_E_WEIGHTED = 9
    IMPROVED_E_WEIGHTED = 10

RECTANGULAR class-attribute instance-attribute

RECTANGULAR = 1

TRIANGULAR class-attribute instance-attribute

TRIANGULAR = 2

TRIANGULAR_HP class-attribute instance-attribute

TRIANGULAR_HP = 3

LIPSHITZ class-attribute instance-attribute

LIPSHITZ = 4

SHIBATA class-attribute instance-attribute

SHIBATA = 5

LOW_SHIBATA class-attribute instance-attribute

LOW_SHIBATA = 6

HIGH_SHIBATA class-attribute instance-attribute

HIGH_SHIBATA = 7

F_WEIGHTED class-attribute instance-attribute

F_WEIGHTED = 8

MODIFIED_E_WEIGHTED class-attribute instance-attribute

MODIFIED_E_WEIGHTED = 9

IMPROVED_E_WEIGHTED class-attribute instance-attribute

IMPROVED_E_WEIGHTED = 10

qAAC_MODE

Bases: IntEnum

Source code in muxtools/utils/types.py
class qAAC_MODE(IntEnum):
    TVBR = 1
    CVBR = 2
    ABR = 3
    CBR = 4

TVBR class-attribute instance-attribute

TVBR = 1

CVBR class-attribute instance-attribute

CVBR = 2

ABR class-attribute instance-attribute

ABR = 3

CBR class-attribute instance-attribute

CBR = 4

LossyWavQuality

Bases: IntEnum

LossyWAV Quality presets, see https://wiki.hydrogenaud.io/index.php?title=LossyWAV#Quality_presets

TL;DR: Insane the least lossy, ExtraPortable the most lossy.

Source code in muxtools/utils/types.py
class LossyWavQuality(IntEnum):
    """
    LossyWAV Quality presets, see https://wiki.hydrogenaud.io/index.php?title=LossyWAV#Quality_presets

    TL;DR: Insane the least lossy, ExtraPortable the most lossy.
    """

    INSANE = 1
    EXTREME = 2
    HIGH = 3
    STANDARD = 4
    ECONOMIC = 5
    PORTABLE = 6
    EXTRAPORTABLE = 7

INSANE class-attribute instance-attribute

INSANE = 1

EXTREME class-attribute instance-attribute

EXTREME = 2

HIGH class-attribute instance-attribute

HIGH = 3

STANDARD class-attribute instance-attribute

STANDARD = 4

ECONOMIC class-attribute instance-attribute

ECONOMIC = 5

PORTABLE class-attribute instance-attribute

PORTABLE = 6

EXTRAPORTABLE class-attribute instance-attribute

EXTRAPORTABLE = 7

ValidInputType

Bases: IntEnum

Source code in muxtools/utils/types.py
class ValidInputType(IntEnum):
    FLAC = 1
    W64 = 3
    RF64 = 4
    W64_OR_FLAC = 6
    RF64_OR_FLAC = 7

    def allows_flac(self) -> bool:
        return "_OR_FLAC" in str(self.name)

    def remove_flac(self):
        match self:
            case ValidInputType.RF64_OR_FLAC:
                return ValidInputType.RF64
            case ValidInputType.W64_OR_FLAC:
                return ValidInputType.W64

        return ValidInputType.RF64

FLAC class-attribute instance-attribute

FLAC = 1

W64 class-attribute instance-attribute

W64 = 3

RF64 class-attribute instance-attribute

RF64 = 4

W64_OR_FLAC class-attribute instance-attribute

W64_OR_FLAC = 6

RF64_OR_FLAC class-attribute instance-attribute

RF64_OR_FLAC = 7

allows_flac

allows_flac
Source code in muxtools/utils/types.py
def allows_flac(self) -> bool:
    return "_OR_FLAC" in str(self.name)

remove_flac

remove_flac
Source code in muxtools/utils/types.py
def remove_flac(self):
    match self:
        case ValidInputType.RF64_OR_FLAC:
            return ValidInputType.RF64
        case ValidInputType.W64_OR_FLAC:
            return ValidInputType.W64

    return ValidInputType.RF64

FileMixin

FileMixin
Source code in muxtools/utils/types.py
def __init__(
    self,
    file: Path,
    container_delay: int = 0,
    source: PathLike | None = None,
    tags: dict[str, str] | None = None,
):
    self.file = file
    self.container_delay = container_delay
    self.source = source
    self.tags = tags

file instance-attribute

file: Path = file

container_delay class-attribute instance-attribute

container_delay: int = container_delay

source class-attribute instance-attribute

source: PathLike | None = source

tags class-attribute instance-attribute

tags: dict[str, str] | None = tags

AudioFrame dataclass

AudioFrame

A dataclass representing ffmpeg's ashowdata filter output.

Parameters:

Name Type Description Default
n int

The (sequential) number of the input frame, starting from 0

required
pts int

The presentation timestamp of the input frame, in time base units the time base depends on the filter input pad, and is usually 1/sample_rate

required
pts_time float

The presentation timestamp of the input frame in seconds

required
num_samples int

Number of samples in a frame (can also be refered to as frame length or size)

required
Source code in muxtools/utils/types.py
@dataclass
class AudioFrame:
    """
    A dataclass representing ffmpeg's `ashowdata` filter output.

    :param n:           The (sequential) number of the input frame, starting from 0
    :param pts:         The presentation timestamp of the input frame, in time base units
                        the time base depends on the filter input pad, and is usually 1/sample_rate

    :param pts_time:    The presentation timestamp of the input frame in seconds
    :param num_samples: Number of samples in a frame (can also be refered to as frame length or size)
    """

    n: int
    pts: int
    pts_time: float
    num_samples: int

n instance-attribute

n: int

pts instance-attribute

pts: int

pts_time instance-attribute

pts_time: float

num_samples instance-attribute

num_samples: int

AudioStats dataclass

AudioStats

A dataclass representing ffmpeg's astats filter output.

Too many attributes to document to be honest.

See https://ffmpeg.org/ffmpeg-filters.html#astats-1

Source code in muxtools/utils/types.py
@dataclass
class AudioStats:
    """
    A dataclass representing ffmpeg's `astats` filter output.

    Too many attributes to document to be honest.

    See https://ffmpeg.org/ffmpeg-filters.html#astats-1
    """

    dc_offset: float = 0.0
    min_level: float = 0.0
    max_level: float = 0.0
    min_difference: float = 0.0
    max_difference: float = 0.0
    mean_difference: float = 0.0
    rms_difference: float = 0.0
    peak_level_db: float = 0.0
    rms_level_db: float = 0.0
    rms_peak_db: float = 0.0
    rms_trough_db: float = 0.0
    flat_factor: float = 0.0
    peak_count: float = 0.0
    noise_floor_db: float = 0.0
    noise_floor_count: float = 0.0
    entropy: float = 0.0
    bit_depth: str = ""
    number_of_samples: int = 0

dc_offset class-attribute instance-attribute

dc_offset: float = 0.0

min_level class-attribute instance-attribute

min_level: float = 0.0

max_level class-attribute instance-attribute

max_level: float = 0.0

min_difference class-attribute instance-attribute

min_difference: float = 0.0

max_difference class-attribute instance-attribute

max_difference: float = 0.0

mean_difference class-attribute instance-attribute

mean_difference: float = 0.0

rms_difference class-attribute instance-attribute

rms_difference: float = 0.0

peak_level_db class-attribute instance-attribute

peak_level_db: float = 0.0

rms_level_db class-attribute instance-attribute

rms_level_db: float = 0.0

rms_peak_db class-attribute instance-attribute

rms_peak_db: float = 0.0

rms_trough_db class-attribute instance-attribute

rms_trough_db: float = 0.0

flat_factor class-attribute instance-attribute

flat_factor: float = 0.0

peak_count class-attribute instance-attribute

peak_count: float = 0.0

noise_floor_db class-attribute instance-attribute

noise_floor_db: float = 0.0

noise_floor_count class-attribute instance-attribute

noise_floor_count: float = 0.0

entropy class-attribute instance-attribute

entropy: float = 0.0

bit_depth class-attribute instance-attribute

bit_depth: str = ''

number_of_samples class-attribute instance-attribute

number_of_samples: int = 0

AudioInfo dataclass

AudioInfo
Source code in muxtools/utils/types.py
@dataclass
class AudioInfo:
    stats: AudioStats | None = None
    frames: list[AudioFrame] | None = None

    def num_samples(self) -> int:
        if not self.frames:
            return 0
        for frame in self.frames:
            if frame.num_samples:
                return frame.num_samples

        return 0

stats class-attribute instance-attribute

stats: AudioStats | None = None

frames class-attribute instance-attribute

frames: list[AudioFrame] | None = None

num_samples

num_samples
Source code in muxtools/utils/types.py
def num_samples(self) -> int:
    if not self.frames:
        return 0
    for frame in self.frames:
        if frame.num_samples:
            return frame.num_samples

    return 0