Vidoe2Jpg
probe_video_dimensions(video_path:Union[str, os.PathLike])
Uses ffprobe to get the width and height of the first video stream.
Parameters:
video_path
(Union[str, os.PathLike]): Path to the video file.
Returns:
Tuple[int, int]
: Tuple of (width, height) in pixels.Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_probe_video_dimensions
prepare_output_directory(output_pattern:Path, wipe:bool)
Ensures output directory exists and wipes existing files if requested. Returns the directory Path.
Parameters:
output_pattern
(Path): Pattern for the output files, must include directory.wipe
(bool): If True, delete existing files in the output directory.
Returns:
Path
: The output directory path.Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_prepare_output_directory
build_vf_filter(fps:int, height:int, min_height:int, grayscale:bool)
Constructs the ffmpeg -vf filter string, adding padding if height < min_height, and converting to grayscale if requested.
Parameters:
fps
(int): Frames per second to extract.height
(int): Height of the video stream.min_height
(int): Minimum height for padding.grayscale
(bool): If True, convert frames to grayscale.
Returns:
str
: The constructed filter string for ffmpeg.Todo:
- Add support Rotation and maybe save the rotated video
Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_build_vf_filter
run_ffmpeg_extraction(input_path:Path, output_pattern:Path, vf_filter:str, use_cuda:bool, grayscale:bool)
Executes ffmpeg with the given filters and codec settings.
Parameters:
input_path
(Path): Path to the input video file.output_pattern
(Path): Pattern for saving frames (with %06d).vf_filter
(str): The video filter string for ffmpeg.use_cuda
(bool): If True, use CUDA for decoding.grayscale
(bool): If True, convert frames to grayscale.
Raises:
FileNotFoundError
: if ffmpeg or CUDA decoder is not found.RuntimeError
: if ffmpeg extraction fails.
Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_run_ffmpeg_extraction
health_check_frames(directory:Path, ext:str)
Attempts to open and verify each image file in directory with Pillow. Raises RuntimeError if any are corrupted.
Parameters:
directory
(Path): Directory containing the extracted frames.ext
(str): File extension of the frames (e.g., 'jpg', 'png').
Returns:
list
: list of corrupted frame paths.Raises:
RuntimeError
: If Pillow is not installed or if any frame files are corrupted.
Warning:
If any frame files are corrupted, listing their paths.
Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_health_check_frames
ffmpeg_frame_extractor(input_video_path:Union[str, os.PathLike], output_frame_pattern:Optional[Union[str, os.PathLike]], fps:int, wipe:bool, use_cuda:bool, grayscale:bool, health_check:bool, min_height:int)
High-level frame extractor that orchestrates probing, directory setup, filter construction, FFmpeg extraction, and optional post-check.
Parameters:
input_video_path
(Union[str, os.PathLike]): Path to the input video file.output_frame_pattern
(Optional[Union[str, os.PathLike]]): Pattern for saving frames (with %06d).fps
(int): Frames per second to extract.wipe
(bool): Clear existing frames.use_cuda
(bool): Enable CUDA decoding.grayscale
(bool): Convert frames to grayscale.health_check
(bool): Verify each frame after extraction.min_height
(int): Minimum frame height; pad if below.
Returns:
None
: Frames are saved to the specified output pattern.Raises:
FileNotFoundError
: If input video does not exist.RuntimeError
: If probing fails or extraction fails.
Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_ffmpeg_frame_extractor
Recursively scans a directory for .mp4 files up to a specified depth.
Parameters:
root_dir
( Union[str, os.PathLike]): The root directory to start scanning.max_depth
(int): Maximum depth to scan (default is 2).
Returns:
list
: A sorted list of paths to .mp4 files found within the specified depth.Raises:
ValueError
: If root_dir is not a directory.Exception
: If an error occurs while scanning.
Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_get_mp4_files
check_AllVideosHaveSameDimensions(video_root:str, max_depth:int)
Checks if all videos in the provided list have the same dimensions. Returns True if they do, False otherwise.
Parameters:
video_root
(str): Root directory to search for video files.max_depth
(int): Maximum depth to search for video files.
Returns:
bool
: True if all videos have the same dimensions, False otherwise.Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_check_AllVideosHaveSameDimensions
fix_corrupted_frames(corrupted:list[os.PathLike], grey:bool)
Detects corrupted frames via health_check_frames and interpolates replacements using the nearest valid neighboring frames.
Parameters:
corrupted
(list[os.PathLike]): List of paths to corrupted frame files.grey
(bool): If True, convert the interpolated frames to grayscale.
Returns:
None
: Replaces corrupted frames with interpolated images..Raises:
RuntimeError
: If Pillow is not installed or if interpolation fails.RuntimeError
: If neighboring frames for interpolation are missing.
Labels:
src_PyThon_FFMpeg_Vidoe2Jpg_fix_corrupted_frames