preppers
Contains functions, objects that prepare shared request values for extraction.
ExtractionRange
dataclass
Container for variations of the user's requested extraction range.
The range is represented in seconds, as timestamps, and frame numbers. Validations and preparations are run on init.
Fields:
`duration_seconds` (float):
Duration of the video in seconds.
`frame_count` (int):
Number of frames in the video.
`start_time` (float | int | str):
Start time of the extraction in seconds or as a timestamp (`HH:MM:SS`).
`stop_time` (float | int | str):
Stop time of the extraction in seconds or as a timestamp (`HH:MM:SS`).
`fps` (float):
Frames per second to use for extraction.
Attributes:
`start_second` (float):
Start time of the extraction in seconds.
`stop_second` (float):
Stop time of the extraction in seconds.
`start_timestamp` (str):
Start time of the extraction as a timestamp (`HH:MM:SS`).
`stop_timestamp` (str):
Stop time of the extraction as a timestamp (`HH:MM:SS`).
`start_frame` (int):
Start time of the extraction as a frame number.
`stop_frame` (int):
Stop time of the extraction as a frame number.
Usage:
>>> from videoxt.preppers import ExtractionRange
>>> extraction_range = ExtractionRange(
... duration_seconds=20,
... frame_count=600,
... start_time=10,
... stop_time=0,
... fps=30,
... )
>>> extraction_range.start_second
0
>>> extraction_range.stop_second
10
>>> extraction_range.start_timestamp
'0:00:00'
>>> extraction_range.stop_timestamp
'0:00:10'
>>> extraction_range.start_frame
0
>>> extraction_range.stop_frame
300
to_dict
Return a dictionary of the validated extraction start and stop points.
Source code in videoxt/preppers.py
prepare_extraction_range
prepare_extraction_range(duration_seconds, frame_count, prepared_start_time=None, prepared_stop_time=None, prepared_fps=None)
Return a dictionary representing a validated extraction start and stop points.
The dictionary contains variations of the requested start and stop times as seconds (float), timestamps (str), and frame numbers (int); including the requested values.
Args:
`duration_seconds` (float):
Duration of the video in seconds.
`frame_count` (int):
Number of frames in the video.
`prepared_start_time` (float | int | str | None):
Prepared start time of the extraction in seconds or as a timestamp
(`HH:MM:SS`).
`request_stop_time` (float | int | str | None):
Prepared stop time of the extraction in seconds or as a timestamp
(`HH:MM:SS`).
`prepared_fps` (float | None):
The prepared frames per second used to calculate the frame numbers.
Returns:
`dict[str, Any]`: Dictionary of validated extraction start and stop points.
Raises:
`PreparationError`: If the prepared start and stop times or fps are None.
Source code in videoxt/preppers.py
prepare_dimensions
Return dimensions to use for extraction.
Args:
`video_dimensions` (tuple[int, int]):
Dimensions of the video.
`request_dimensions` (tuple[int, int] | None):
Optional dimensions that override the video's dimensions.
`request_resize` (float | None):
Requested resize factor to apply to the video's dimensions.
Returns:
`tuple[int, int]`: Dimensions to use for extraction.
Source code in videoxt/preppers.py
prepare_destpath
prepare_destpath(video_filepath, request_filename=None, request_destdir=None, prepared_suffix=None, prepared_overwrite=None)
Construct and return the destination path for the extracted file.
Args:
`video_filepath` (Path):
Path to the video file.
`request_filename` (str | None):
Optional filename to use. Defaults to None.
`request_destdir` (Path | None):
Optional directory to save the processed file. Defaults to None.
`prepared_suffix` (str):
Required file suffix to use (with or without the leading '.').
`prepared_overwrite` (bool):
If True, allows overwriting an existing file with the same name.
Defaults to False.
Returns:
`Path`: Path to the destination file.
Raises:
`PreparationError`: If the prepared suffix is None.
Notes:
- Uses `request_filename` with the specified `suffix` if provided; otherwise,
uses video file's name.
- Saves the file in `request_destdir` if provided; otherwise, saves in the
video's directory.
- Handles overwriting: if `overwrite` is True, may overwrite existing file with
the same name; if False, ensures a unique filename before saving.
Source code in videoxt/preppers.py
prepare_start_time
Return the start time requested or 0 if not provided.
Args:
`request_start_time` (float | int | str | None):
Optional start time requested. Defaults to None.
Returns:
`float | int | str`: The start time requested or 0 if not provided.
Source code in videoxt/preppers.py
prepare_stop_time
Return the stop time requested or the video's duration if not provided.
Args:
`video_duration_seconds` (float):
Duration of the video in seconds.
`request_stop_time` (float | int | str | None):
Optional stop time requested. Defaults to None.
Returns:
`float | int | str`: The stop time requested or the video's duration if not
provided.
Source code in videoxt/preppers.py
prepare_fps
Return the frames per second requested or the video's fps if not provided.
Args:
`video_fps` (float):
Frames per second of the video.
`request_fps` (float | None):
Optional frames per second requested. Defaults to None.
Returns:
`float`: The frames per second requested or the video's fps if not provided.
Source code in videoxt/preppers.py
prepare_destpath_frames
Return the directory extracted images will be saved in.
If no specific destdir is requested, the images will be saved in a directory
created in the same location as the video file. The directory will share the
video file's name and have a '_frames' suffix.
Returns:
`Path`: The directory for saving images extracted from the video.
Source code in videoxt/preppers.py
prepare_images_expected
Return the number of images (frames) expected to be written to disk.
Args:
`start_frame` (int | None):
The frame number to start extraction from.
`stop_frame` (int | None):
The frame number to stop extraction at.
`capture_rate` (int | None):
The number of frames to increment by.
Returns:
`int`: The number of images expected to be extracted.
Raises:
`PreparationError`:
- If the start frame, stop frame or capture rate is None
- If the capture rate is zero.