utils
Utility functions and classes used throughout the library.
CustomJSONEncoder
Bases: JSONEncoder
A custom JSON encoder for types that are not JSON serializable.
default
Return a JSON serializable representation of the object.
Source code in videoxt/utils.py
ToJsonMixin
dataclass
A mixin for dataclasses that can be represented as JSON.
json
Return a JSON string representation of the dataclass.
Args:
`skip_private_keys` (bool):
If True, private keys (keys starting with '_') are not included in the
JSON string.
Returns:
`str`: JSON string representation of the dataclass.
Source code in videoxt/utils.py
verbose_print
Print the public keys of the JSON to console with a title.
Source code in videoxt/utils.py
DataclassType
Bases: Protocol
Protocol representing dataclass attributes for type-hinting purposes.
timestamp_to_seconds
Convert a timestamp string to the total number of seconds (float) it represents.
Accepts the formats "HH:MM:SS", "H:MM:SS", "MM:SS", "M:SS", "SS" or "S". Microseconds are truncated.
Usage:
>>> from videoxt.utils import timestamp_to_seconds
>>> timestamp_to_seconds("1:01")
61.0
>>> timestamp_to_seconds("1:01.987654321")
61.0
>>> timestamp_to_seconds("10:00")
600.0
>>> timestamp_to_seconds("1:00:00")
3600.0
Args:
`timestamp` (str):
A timestamp string in the format "HH:MM:SS", "H:MM:SS", "MM:SS", "M:SS",
"SS" or "S"
Returns:
`float`: The number of seconds converted from the timestamp string.
Source code in videoxt/utils.py
seconds_to_timestamp
Convert seconds to a time duration string in the format "HH:MM:SS" or "H:MM:SS".
Microseconds are truncated.
Usage:
>>> from videoxt.utils import seconds_to_timestamp
>>> seconds_to_timestamp(60)
'0:01:00'
>>> seconds_to_timestamp(60.987654321)
'0:01:00'
>>> seconds_to_timestamp(600)
'0:10:00'
>>> seconds_to_timestamp(3600)
'1:00:00'
Args:
`seconds` (float): The number of seconds to convert to a timestamp.
Returns:
`str`: A timestamp string in the format "HH:MM:SS" or "H:MM:SS".
Source code in videoxt/utils.py
timedelta_to_timestamp
Convert a timedelta to a time duration string in the format "HH:MM:SS".
Microseconds are truncated.
Usage:
>>> from videoxt.utils import timedelta_to_timestamp
>>> timedelta_to_timestamp(timedelta(seconds=59))
'00:00:59'
>>> timedelta_to_timestamp(timedelta(seconds=61.987654321))
'00:01:01'
>>> timedelta_to_timestamp(timedelta(seconds=600))
'00:10:00'
Args:
`duration` (datetime.timedelta): The duration to convert to a timestamp.
Returns:
`str`: A timestamp string in the format "HH:MM:SS".
Raises:
`ValueError`: If the duration is negative.
Source code in videoxt/utils.py
append_enumeration
Return an enumerated file name with the given index and optional tag.
The returned string is intended to be appended to a file name or directory name. If the tag is None, only the index is used to enumerate the file name starting from 1. If a tag is provided, the tag is appended to the file name before the index. Mimics the behavior of Windows when creating a file or directory with the same name as an existing file or directory.
Usage:
>>> from videoxt.utils import append_enumeration
>>> append_enumeration(1)
' (1)'
>>> append_enumeration(2)
' (2)'
>>> append_enumeration(1, tag="_vxt")
'_vxt'
>>> append_enumeration(2, tag="_vxt")
'_vxt (2)'
Args:
`index` (int):
The enumeration index. Must be greater than 0.
`tag` (str | None):
The tag to enumerate. If None, only the index is used.
Returns:
`str`: The enumerated string to append to a file name or directory name.
Source code in videoxt/utils.py
enumerate_dir
Return a non-existent, potentially enumerated directory path.
See videoxt.utils.append_enumeration for more information on how the directory
name is enumerated with or without a tag.
Usage:
>>> from videoxt.utils import
>>> from pathlib import Path
>>> path = Path("test_dir") # Assume 'test_dir' doesn't exist.
>>> enumerate_dir(path, tag="_frames")
Path('test_dir')
>>> enumerate_dir(path, tag="_frames")
Path('test_dir_frames')
>>> enumerate_dir(path, tag="_frames")
Path('test_dir_frames (2)')
Args:
`directory`:
The path to the directory to potentially enumerate.
`tag` (str | None):
The tag to enumerate. If None, only the index is used.
Returns:
`Path`: The path to a non-existent directory.
Source code in videoxt/utils.py
enumerate_filepath
Return a non-existent, potentially enumerated file path.
See videoxt.utils.append_enumeration for more information on how the file name
is enumerated with or without a tag.
Usage:
>>> from videoxt.utils import enumerate_filepath
>>> from pathlib import Path
>>> filepath = Path('test.mp4') # Assume 'test.mp4' doesn't exist.
>>> enumerate_filepath(filepath, tag="_vxt")
Path('test.mp4')
>>> enumerate_filepath(filepath, tag="_vxt")
Path('test_vxt.mp4')
>>> enumerate_filepath(filepath, tag="_vxt")
Path('test_vxt (2).mp4')
Args:
`filepath` (Path): The path to the file to potentially enumerate.
Returns:
`Path`: The path to a non-existent file.
Source code in videoxt/utils.py
calculate_duration
Return a timedelta representing the duration of a video using frame count and fps.
Args:
`frame_count` (int):
The total number of frames in the video.
`fps` (float):
The frames per second of the video.
Returns:
`datetime.timedelta`: The duration of the video.
Source code in videoxt/utils.py
convert_bytes
Convert n bytes (int) to a human readable string.
Usage:
>>> from videoxt.utils import convert_bytes
>>> convert_bytes(1)
'1.00 bytes'
>>> convert_bytes(1024)
'1.00 KB'
>>> convert_bytes(1024**2)
'1.00 MB'
>>> convert_bytes(1024**3)
'1.00 GB'
>>> convert_bytes(1024**4)
'1.00 TB'
>>> convert_bytes(1024**5)
'1.00 PB'
Args:
`n` (int): The number of bytes to convert.
Returns:
`str`: A human readable string representing the number of bytes.
Source code in videoxt/utils.py
remove_private_keys
Return a copy of the dictionary without private keys (keys starting with '_').
Args:
`d` (dict): The dictionary you want to remove private keys from.
Returns:
`dict[str, Any]`: A new dictionary containing public keys only.
Source code in videoxt/utils.py
parse_kwargs
Return the keys and values in kwargs present in the obj dataclass attributes.
Usage:
>>> from videoxt.utils import parse_kwargs
>>> from dataclasses import dataclass
>>> @dataclass
... class Test:
... a: int
... b: int
>>> parse_kwargs({"a": 1, "b": 2, "c": 3}, Test)
{'a': 1, 'b': 2}
Args:
`kwargs` (dict[str, Any]):
A dictionary to filter down to keys present in the `obj` attributes.
`obj` (dataclasses.dataclass):
A dataclass to filter the `kwargs` dictionary with.
Returns:
`dict`: A dictionary of keyword arguments present in the `obj` attributes.