Skip to content

TimeUnitConverter

Utility class for converting between time units.

Source code in video_timestamps/time_unit_converter.py
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class TimeUnitConverter:
    """Utility class for converting between time units."""

    @staticmethod
    def time_base_to_time_scale(time_base: Fraction) -> Fraction:
        """
        Convert a time base to a time scale.

        Parameters:
            time_base: The time base to convert.

        Returns:
            The corresponding time scale.
        """
        return 1 / time_base


    @staticmethod
    def time_scale_to_time_base(time_scale: Fraction) -> Fraction:
        """
        Convert a time scale to a time base.

        Parameters:
            time_scale: The time scale to convert.

        Returns:
            The corresponding time base.
        """
        return 1 / time_scale


    @staticmethod
    def timestamp_scale_to_time_scale(timestamp_scale: int) -> Fraction:
        """
        Convert a timestamp scale to a time scale.

        Parameters:
            timestamp_scale: The timestamp scale (e.g., nanoseconds per tick).

        Returns:
            The corresponding time scale.
        """
        return Fraction(pow(10, 9), timestamp_scale)


    @staticmethod
    def time_scale_to_timestamp_scale(time_scale: Fraction) -> int:
        """
        Convert a time scale to a timestamp scale.

        Parameters:
            time_scale: The time scale to convert.

        Returns:
            The corresponding timestamp scale.
        """
        timestamp_scale = Fraction(pow(10, 9), time_scale)

        if sys.version_info >= (3, 12):
            is_integer = timestamp_scale.is_integer()
        else:
            is_integer = timestamp_scale.denominator == 1

        if not is_integer:
            raise ValueError(f"The timescale {time_scale} cannot be converted to a timestamp scale because the result {timestamp_scale} isn't a integer.")

        return int(timestamp_scale)


    @staticmethod
    def timestamp_scale_to_time_base(timestamp_scale: int) -> Fraction:
        """
        Convert a timestamp scale to a time base.

        Parameters:
            timestamp_scale: The timestamp scale.

        Returns:
            The corresponding time base.
        """
        return Fraction(timestamp_scale, pow(10, 9))


    @staticmethod
    def time_base_to_timestamp_scale(time_base: Fraction) -> int:
        """
        Convert a time base to a timestamp scale.

        Parameters:
            time_base: The time base to convert.

        Returns:
            The corresponding timestamp scale.
        """
        timestamp_scale = time_base * pow(10, 9)

        if sys.version_info >= (3, 12):
            is_integer = timestamp_scale.is_integer()
        else:
            is_integer = timestamp_scale.denominator == 1

        if not is_integer:
            raise ValueError(f"The timebase {time_base} cannot be converted to a timestamp scale because the result {timestamp_scale} isn't a integer.")

        return int(timestamp_scale)

time_base_to_time_scale(time_base) staticmethod

Convert a time base to a time scale.

Parameters:

Name Type Description Default
time_base Fraction

The time base to convert.

required

Returns:

Type Description
Fraction

The corresponding time scale.

Source code in video_timestamps/time_unit_converter.py
10
11
12
13
14
15
16
17
18
19
20
21
@staticmethod
def time_base_to_time_scale(time_base: Fraction) -> Fraction:
    """
    Convert a time base to a time scale.

    Parameters:
        time_base: The time base to convert.

    Returns:
        The corresponding time scale.
    """
    return 1 / time_base

time_scale_to_time_base(time_scale) staticmethod

Convert a time scale to a time base.

Parameters:

Name Type Description Default
time_scale Fraction

The time scale to convert.

required

Returns:

Type Description
Fraction

The corresponding time base.

Source code in video_timestamps/time_unit_converter.py
24
25
26
27
28
29
30
31
32
33
34
35
@staticmethod
def time_scale_to_time_base(time_scale: Fraction) -> Fraction:
    """
    Convert a time scale to a time base.

    Parameters:
        time_scale: The time scale to convert.

    Returns:
        The corresponding time base.
    """
    return 1 / time_scale

timestamp_scale_to_time_scale(timestamp_scale) staticmethod

Convert a timestamp scale to a time scale.

Parameters:

Name Type Description Default
timestamp_scale int

The timestamp scale (e.g., nanoseconds per tick).

required

Returns:

Type Description
Fraction

The corresponding time scale.

Source code in video_timestamps/time_unit_converter.py
38
39
40
41
42
43
44
45
46
47
48
49
@staticmethod
def timestamp_scale_to_time_scale(timestamp_scale: int) -> Fraction:
    """
    Convert a timestamp scale to a time scale.

    Parameters:
        timestamp_scale: The timestamp scale (e.g., nanoseconds per tick).

    Returns:
        The corresponding time scale.
    """
    return Fraction(pow(10, 9), timestamp_scale)

time_scale_to_timestamp_scale(time_scale) staticmethod

Convert a time scale to a timestamp scale.

Parameters:

Name Type Description Default
time_scale Fraction

The time scale to convert.

required

Returns:

Type Description
int

The corresponding timestamp scale.

Source code in video_timestamps/time_unit_converter.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
@staticmethod
def time_scale_to_timestamp_scale(time_scale: Fraction) -> int:
    """
    Convert a time scale to a timestamp scale.

    Parameters:
        time_scale: The time scale to convert.

    Returns:
        The corresponding timestamp scale.
    """
    timestamp_scale = Fraction(pow(10, 9), time_scale)

    if sys.version_info >= (3, 12):
        is_integer = timestamp_scale.is_integer()
    else:
        is_integer = timestamp_scale.denominator == 1

    if not is_integer:
        raise ValueError(f"The timescale {time_scale} cannot be converted to a timestamp scale because the result {timestamp_scale} isn't a integer.")

    return int(timestamp_scale)

timestamp_scale_to_time_base(timestamp_scale) staticmethod

Convert a timestamp scale to a time base.

Parameters:

Name Type Description Default
timestamp_scale int

The timestamp scale.

required

Returns:

Type Description
Fraction

The corresponding time base.

Source code in video_timestamps/time_unit_converter.py
76
77
78
79
80
81
82
83
84
85
86
87
@staticmethod
def timestamp_scale_to_time_base(timestamp_scale: int) -> Fraction:
    """
    Convert a timestamp scale to a time base.

    Parameters:
        timestamp_scale: The timestamp scale.

    Returns:
        The corresponding time base.
    """
    return Fraction(timestamp_scale, pow(10, 9))

time_base_to_timestamp_scale(time_base) staticmethod

Convert a time base to a timestamp scale.

Parameters:

Name Type Description Default
time_base Fraction

The time base to convert.

required

Returns:

Type Description
int

The corresponding timestamp scale.

Source code in video_timestamps/time_unit_converter.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
@staticmethod
def time_base_to_timestamp_scale(time_base: Fraction) -> int:
    """
    Convert a time base to a timestamp scale.

    Parameters:
        time_base: The time base to convert.

    Returns:
        The corresponding timestamp scale.
    """
    timestamp_scale = time_base * pow(10, 9)

    if sys.version_info >= (3, 12):
        is_integer = timestamp_scale.is_integer()
    else:
        is_integer = timestamp_scale.denominator == 1

    if not is_integer:
        raise ValueError(f"The timebase {time_base} cannot be converted to a timestamp scale because the result {timestamp_scale} isn't a integer.")

    return int(timestamp_scale)