mmeutils.fileio package

Submodules

mmeutils.fileio.ffmpeg module

FFmpeg Launcher Module

mmeutils.fileio.ffmpeg.get_ffmpeg_bin(ignore_local: bool = False) str

Returns the full path to an ffmpeg binary.

If no ffmpeg binary can be found locally pyffmpeg will be used (which downloads a local copy into the user profile).

Parameters:

ignore_local (bool) – Do not use/search local ffmpeg copies (excludes pyffmpeg)

Returns:

The full path to an ffmpeg binary.

Return type:

str

mmeutils.fileio.ffmpeg.run_ffmpeg(args: List[str]) bool

Locates and runs an ffmpeg binary with supplied arguments.

Parameters:

args (List[str]) – The arguments for ffmpeg.

Returns:

True on success, False on error.

Return type:

bool

mmeutils.fileio.mp4 module

MPEG-4 Binary File Manipulation

Kudos to Alfred Gutierrez’ (alfg) and Sanjeev Pandey’s well-summarizing articles:

https://dev.to/alfg/a-quick-dive-into-mp4-57fo https://sanjeev-pandey.medium.com/understanding-the-mpeg-4-moov-atom-pseudo-streaming-in-mp4-93935e1b9e9a

class mmeutils.fileio.mp4.MP4Box(size_bytes: bytes, fourcc_bytes: bytes, position: int)

Bases: object

Represents an MPEG-4 binary box/atom object.

static convert_to_fourcc(fourcc_bytes: bytes) str

Converts FourCC file header bytes to an ASCII string.

Parameters:

fourcc_bytes (bytes) – The FourCC bytes from the header.

Returns:

An ASCII string.

Return type:

str

mmeutils.fileio.mp4.get_boxes(reader: BufferedReader) Iterable[MP4Box]

Fetches all box (atom) elements from an MPEG-4 file.

Parameters:

reader (BufferedReader) – A buffered reader of the MPEG-4 file to process.

Returns:

Yields (generates) MP4Box objects.

Return type:

Iterable[MP4Box]

mmeutils.fileio.mp4.hash_mp4box(algorithm: HASH, reader: BufferedReader, box: MP4Box) None

Hashes an MPEG-4 box atom.

The hash will be updated so you can use one algorithm object to chain function calls for different boxes yielding a total hash in the object.

algorithm must be a hashlib hash algorithm.

Parameters:
  • algorithm (_hashlib.HASH) – The hashlib algorithm to use.

  • reader (BufferedReader) – A buffered reader of the MPEG-4 file to process.

  • box (MP4Box) – The box atom within the file to calculate the hash for.

mmeutils.fileio.mp4.hash_mp4file(algorithm, file_name: str | Path, print: Callable | None = None, use_broken_algo: bool = False) str

Hashes an MPEG-4 file selectively.

MPEG-4 files may have different metadata and slightly different timing info although the embedded streams are the same on a binary level. This function tries to account for that fact by only hashing the essential file parts.

Parameters:
  • algorithm (_hashlib.HASH) – The hashlib algorithm to use.

  • file_name (str | Path) – The path/file name of the MPEG-4 file to hash.

  • print (Optional[Callable]) – A print callback for text output of file and hash info.

  • use_broken_algo (bool) – Use a broken algorithm to calculate the hash. This function miscalculated the hash in the past due to a logic error but legacy and remedy code may rely on it.

Returns:

The (selective) hash of the file as string.

Return type:

str

Module contents

File Input/Output Utility Functions

mmeutils.fileio.read_text_file_to_string(file_name: str | bytes | PathLike) str

Reads text file contents into a single string.

Lines will be normalized ie. stripped and empty lines discarded. Lines will then be joined using the line-separation character (n).

Parameters:

file_name (str | Path) – Name/path of the file to process.

Returns:

A string of all the non-empty lines joined by n.

Return type:

str

mmeutils.fileio.validate_json_file(file_name: str | bytes | PathLike) bool

Validate that a JSON file is neither empty nor invalid.

Empty means all whitespace, an empty object or an empty array. Invalid means it cannot be parsed as valid JSON. And the file must exist in the first place.

Parameters:

file_name (str | Path) – Name/path of the JSON file to check.

Returns:

True if file exists and has valid non-empty JSON content. False otherwise.

Return type:

bool