mendevi.utils

Provide simple tools.

Functions

best_profile(height, width)

Return the closest profile name.

compute_video_hash(videos, *[, fast])

Compute the checksum of the video.

cp_shm(file, threshold)

Copy the file into /dev/shm is there is enougth free space, otherwise in /tmp.

get_pix_fmt(*args)

Alias to cutcutcodec func.

get_project_root()

Return the absolute project root folder.

get_rate_video(*args)

Alias to cutcutcodec func.

get_resolution(*args)

Alias to cutcutcodec func.

hash_to_signature(checksum)

Convert the md5 binary hash value into an urlsafe string.

signature_to_hash(signature)

Convert the string signature into the md5 checksum.

unfold_video_files(paths)

Explore recursively the folders to find the video path.

uniform(data)

Return a uniform floating-point number between [0 and 1[.

Details

mendevi.utils.best_profile(height: Integral, width: Integral) str[source]

Return the closest profile name.

Examples

>>> from mendevi.utils import best_profile
>>> best_profile(1080, 1920)
'fhd'
>>>
mendevi.utils.compute_video_hash(videos: str | bytes | Path | Iterable[str | bytes | Path], *, fast: bool = True) bytes | dict[Path, bytes][source]

Compute the checksum of the video.

For \(n\) hash of \(b\) bits, the proba of the colision \(C\) is \(p(C) = 1 - \left(\frac{2^k-1}{2^k}\right)^{\frac{n(n-1)}{2}}\).

The md5 hash uses \(b = 128\) bits. If we add one video per second durring 10 years, the proba of colision is about \(p(C) \approx 1.46*10^{-22}\).

That’s why the md5 hash is used to identify the video files.

Parameters

videospathlike or list[pathlike]

The single or set of video you want to compute the signature.

fastboolean, default=True

If the hash appears in the file name, it is returned immediately. If False, the actual checksum is systematically recalculated.

Returns

signatures

The md5 checksum of the video file. In the case of a multiple file, a dictionary containing the file and the hash is returned rather a single hash. If the file does not exists, return None.

mendevi.utils.cp_shm(file: Path, threshold: float) Path[source]

Copy the file into /dev/shm is there is enougth free space, otherwise in /tmp.

mendevi.utils.get_pix_fmt(*args: tuple) str[source]

Alias to cutcutcodec func.

mendevi.utils.get_project_root() Path[source]

Return the absolute project root folder.

Examples

>>> from mendevi.utils import get_project_root
>>> root = get_project_root()
>>> root.is_dir()
True
>>> root.name
'mendevi'
>>> sorted(p.name for p in root.iterdir())
['__init__.py', '__main__.py', ...]
>>>
mendevi.utils.get_rate_video(*args: tuple) Fraction[source]

Alias to cutcutcodec func.

mendevi.utils.get_resolution(*args: tuple) tuple[int, int][source]

Alias to cutcutcodec func.

mendevi.utils.hash_to_signature(checksum: bytes) str[source]

Convert the md5 binary hash value into an urlsafe string.

Bijection of signature_to_hash().

Parameters

checksumbytes

The 128 bit binary hash value.

Returns

signaturestr

The 26 ascii [2-7a-z] symbols string of the converted checksum.

Examples

>>> from mendevi.utils import hash_to_signature
>>> hash_to_signature(b"\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\t\x98\xec\xf8B~")
'd41d8cd98f00b204e9800998ecf8427e'
>>>
mendevi.utils.signature_to_hash(signature: str) bytes[source]

Convert the string signature into the md5 checksum.

Bijection of hash_to_signature().

Parameters

signaturestr

The 26 ascii [2-7a-z] symbols string of the converted checksum.

Returns

checksumbytes

The 128 bit binary hash value.

Examples

>>> from mendevi.utils import signature_to_hash
>>> signature_to_hash("d41d8cd98f00b204e9800998ecf8427e")
b'\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\t\x98\xec\xf8B~'
>>> signature_to_hash("2qoyzwmpaczaj2mabgmoz6ccpy")  # mendevi < 1.2.2
b'\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\t\x98\xec\xf8B~'
>>>
mendevi.utils.unfold_video_files(paths: Iterable[str | bytes | Path]) Iterable[Path][source]

Explore recursively the folders to find the video path.

Parameters

pathslist[pathlike]

All the folders, files, glob or recursive glob expression.

Yields

filenamepathlib.Path

The path of the video.

mendevi.utils.uniform(data: object) float[source]

Return a uniform floating-point number between [0 and 1[.

This can be very useful for reliably rejecting data based on a specified probability.