Utils Module¶
Utility functions and helper classes.
Command Runner¶
command_runner
¶
Command execution utilities for external tools.
This module provides a wrapper for executing external commands like FFprobe, FFmpeg, and ExifTool with proper error handling and output capture.
SDS Reference: SDS-U01-001
CommandResult
dataclass
¶
Result of a command execution.
Attributes:
| Name | Type | Description |
|---|---|---|
returncode |
int
|
Exit code of the command (0 = success). |
stdout |
str
|
Standard output from the command. |
stderr |
str
|
Standard error output from the command. |
success |
bool
|
Whether the command completed successfully. |
CommandNotFoundError
¶
Bases: Exception
Raised when required external command is not found.
This exception provides helpful installation instructions for common tools.
Attributes:
| Name | Type | Description |
|---|---|---|
command |
The command that was not found. |
Source code in src/video_converter/utils/command_runner.py
CommandTimeoutError
¶
Bases: Exception
Raised when command execution exceeds timeout.
Attributes:
| Name | Type | Description |
|---|---|---|
command |
The command that timed out. |
|
timeout |
The timeout value in seconds. |
Source code in src/video_converter/utils/command_runner.py
CommandExecutionError
¶
Bases: Exception
Raised when command execution fails.
Source code in src/video_converter/utils/command_runner.py
CommandRunner
¶
Wrapper for executing external commands.
This class provides both synchronous and asynchronous methods for running external commands with proper error handling.
Example
runner = CommandRunner() result = runner.run(["ffprobe", "-version"]) print(result.stdout)
check_command_exists
staticmethod
¶
Check if a command exists in PATH.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The command name to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the command exists, False otherwise. |
Source code in src/video_converter/utils/command_runner.py
ensure_command_exists
staticmethod
¶
Ensure a command exists, raising an error if not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The command name to check. |
required |
Raises:
| Type | Description |
|---|---|
CommandNotFoundError
|
If the command is not found. |
Source code in src/video_converter/utils/command_runner.py
run
¶
Run a command synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[str]
|
Command and arguments to execute. |
required |
timeout
|
float | None
|
Maximum time to wait for command (seconds). |
60.0
|
check
|
bool
|
If True, raise exception on non-zero exit code. |
False
|
capture_output
|
bool
|
If True, capture stdout and stderr. |
True
|
Returns:
| Type | Description |
|---|---|
CommandResult
|
CommandResult containing the execution result. |
Raises:
| Type | Description |
|---|---|
CommandNotFoundError
|
If the command is not found. |
CommandExecutionError
|
If check=True and command fails. |
TimeoutExpired
|
If command times out. |
Source code in src/video_converter/utils/command_runner.py
run_async
async
¶
Run a command asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[str]
|
Command and arguments to execute. |
required |
timeout
|
float | None
|
Maximum time to wait for command (seconds). |
60.0
|
check
|
bool
|
If True, raise exception on non-zero exit code. |
False
|
Returns:
| Type | Description |
|---|---|
CommandResult
|
CommandResult containing the execution result. |
Raises:
| Type | Description |
|---|---|
CommandNotFoundError
|
If the command is not found. |
CommandExecutionError
|
If check=True and command fails. |
TimeoutError
|
If command times out. |
Source code in src/video_converter/utils/command_runner.py
run_with_callback
¶
Run a command with real-time output streaming.
This method allows monitoring command output in real-time, which is useful for tracking progress of long-running operations like video encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[str]
|
Command and arguments to execute. |
required |
on_output
|
Callable[[str], None]
|
Callback function called for each output line. |
required |
timeout
|
float | None
|
Maximum time to wait for command (seconds). |
600.0
|
check
|
bool
|
If True, raise exception on non-zero exit code. |
False
|
read_stderr
|
bool
|
If True, read from stderr (FFmpeg outputs to stderr). |
True
|
Returns:
| Type | Description |
|---|---|
CommandResult
|
CommandResult containing the final execution result. |
Raises:
| Type | Description |
|---|---|
CommandNotFoundError
|
If the command is not found. |
CommandExecutionError
|
If check=True and command fails. |
CommandTimeoutError
|
If command times out. |
Example
def on_progress(line: str): ... if "frame=" in line: ... print(f"Progress: {line}") runner = CommandRunner() result = runner.run_with_callback( ... ["ffmpeg", "-i", "input.mp4", "output.mp4"], ... on_output=on_progress, ... read_stderr=True, ... )
Source code in src/video_converter/utils/command_runner.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
FFprobeRunner
¶
Specialized runner for FFprobe commands.
Provides convenient methods for common FFprobe operations with JSON output parsing.
Example
Initialize FFprobe runner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_runner
|
CommandRunner | None
|
CommandRunner instance to use. If None, creates a new one. |
None
|
Source code in src/video_converter/utils/command_runner.py
probe
¶
Probe a video file and return information as a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the video file. |
required |
show_format
|
bool
|
Include format information. |
True
|
show_streams
|
bool
|
Include stream information. |
True
|
timeout
|
float
|
Maximum time to wait (seconds). |
30.0
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing video information. |
Raises:
| Type | Description |
|---|---|
CommandNotFoundError
|
If FFprobe is not installed. |
CommandExecutionError
|
If probing fails. |
JSONDecodeError
|
If output cannot be parsed. |
FileNotFoundError
|
If the video file doesn't exist. |
Source code in src/video_converter/utils/command_runner.py
probe_async
async
¶
Probe a video file asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the video file. |
required |
show_format
|
bool
|
Include format information. |
True
|
show_streams
|
bool
|
Include stream information. |
True
|
timeout
|
float
|
Maximum time to wait (seconds). |
30.0
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing video information. |
Source code in src/video_converter/utils/command_runner.py
quick_check
¶
Quickly check if a video file is valid.
This performs a minimal check to see if the file can be opened and basic information can be read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the video file. |
required |
timeout
|
float
|
Maximum time to wait (seconds). |
10.0
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the file is valid, False otherwise. |
Source code in src/video_converter/utils/command_runner.py
ExifToolRunner
¶
Specialized runner for ExifTool commands.
Provides convenient methods for extracting and modifying metadata from media files using ExifTool.
Example
runner = ExifToolRunner() metadata = runner.read_metadata(Path("video.mp4")) print(metadata.get("CreateDate")) '2024:01:15 10:30:00'
Initialize ExifTool runner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_runner
|
CommandRunner | None
|
CommandRunner instance to use. If None, creates a new one. |
None
|
Source code in src/video_converter/utils/command_runner.py
read_metadata
¶
Read metadata from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file. |
required |
timeout
|
float
|
Maximum time to wait (seconds). |
30.0
|
tags
|
list[str] | None
|
Specific tags to read. If None, reads all tags. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing metadata key-value pairs. |
Raises:
| Type | Description |
|---|---|
CommandNotFoundError
|
If ExifTool is not installed. |
CommandExecutionError
|
If reading fails. |
FileNotFoundError
|
If the file doesn't exist. |
Source code in src/video_converter/utils/command_runner.py
read_metadata_async
async
¶
Read metadata from a file asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file. |
required |
timeout
|
float
|
Maximum time to wait (seconds). |
30.0
|
tags
|
list[str] | None
|
Specific tags to read. If None, reads all tags. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing metadata key-value pairs. |
Source code in src/video_converter/utils/command_runner.py
write_metadata
¶
Write metadata to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file. |
required |
metadata
|
dict[str, Any]
|
Dictionary of tag-value pairs to write. |
required |
timeout
|
float
|
Maximum time to wait (seconds). |
30.0
|
overwrite_original
|
bool
|
If True, modify file in place without backup. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if metadata was written successfully. |
Raises:
| Type | Description |
|---|---|
CommandNotFoundError
|
If ExifTool is not installed. |
CommandExecutionError
|
If writing fails. |
FileNotFoundError
|
If the file doesn't exist. |
Source code in src/video_converter/utils/command_runner.py
copy_metadata
¶
Copy metadata from source file to destination file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Path
|
Source file to copy metadata from. |
required |
dest
|
Path
|
Destination file to copy metadata to. |
required |
timeout
|
float
|
Maximum time to wait (seconds). |
30.0
|
overwrite_original
|
bool
|
If True, modify dest in place without backup. |
True
|
all_tags
|
bool
|
If True, copy all tags. Otherwise, only common tags. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if metadata was copied successfully. |
Raises:
| Type | Description |
|---|---|
CommandNotFoundError
|
If ExifTool is not installed. |
FileNotFoundError
|
If either file doesn't exist. |
Source code in src/video_converter/utils/command_runner.py
quick_check
¶
Quickly check if ExifTool can read a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the file. |
required |
timeout
|
float
|
Maximum time to wait (seconds). |
10.0
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the file is readable, False otherwise. |
Source code in src/video_converter/utils/command_runner.py
run_command
¶
Run an external command.
This is a convenience function that creates a CommandRunner instance and executes the command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
list[str]
|
Command and arguments to execute. |
required |
timeout
|
float | None
|
Maximum time to wait for command (seconds). |
60.0
|
check
|
bool
|
If True, raise exception on non-zero exit code. |
False
|
on_output
|
Callable[[str], None] | None
|
Optional callback for real-time output streaming. |
None
|
Returns:
| Type | Description |
|---|---|
CommandResult
|
CommandResult containing the execution result. |
Example
result = run_command(["ffmpeg", "-version"]) print(result.stdout)
Source code in src/video_converter/utils/command_runner.py
run_ffprobe
¶
Run FFprobe on a video file and return parsed JSON output.
This is a convenience function for quick video analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the video file. |
required |
timeout
|
float
|
Maximum time to wait (seconds). |
30.0
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing video information. |
Source code in src/video_converter/utils/command_runner.py
run_exiftool
¶
Run ExifTool on a file and return metadata.
This is a convenience function for quick metadata extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the file. |
required |
timeout
|
float
|
Maximum time to wait (seconds). |
30.0
|
tags
|
list[str] | None
|
Specific tags to read. If None, reads all tags. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing metadata key-value pairs. |
Example
metadata = run_exiftool("video.mp4") print(metadata.get("CreateDate")) '2024:01:15 10:30:00'
Source code in src/video_converter/utils/command_runner.py
Progress Parser¶
progress_parser
¶
FFmpeg progress parsing utilities.
This module provides utilities for parsing FFmpeg stderr output to extract progress information during video conversion.
SDS Reference: SDS-U01-002 SRS Reference: SRS-205 (Real-time Progress Monitoring)
Example
parser = FFmpegProgressParser(total_duration=120.0) progress = parser.parse_line( ... "frame= 720 fps=180 q=32.0 size= 15360kB " ... "time=00:00:24.00 bitrate=5242.9kbits/s speed=6.0x" ... ) print(f"Progress: {progress.percentage:.1f}%") Progress: 20.0%
FFmpegProgress
dataclass
¶
FFmpegProgress(frame=0, fps=0.0, quality=0.0, size_kb=0, time_seconds=0.0, bitrate_kbps=0.0, speed=0.0, percentage=0.0)
Progress information extracted from FFmpeg output.
Attributes:
| Name | Type | Description |
|---|---|---|
frame |
int
|
Current frame number. |
fps |
float
|
Frames per second being processed. |
quality |
float
|
Quality value (q parameter). |
size_kb |
int
|
Current output size in kilobytes. |
time_seconds |
float
|
Current position in seconds. |
bitrate_kbps |
float
|
Current bitrate in kbits/s. |
speed |
float
|
Encoding speed multiplier (e.g., 6.0 means 6x realtime). |
percentage |
float
|
Progress percentage (0.0-100.0). |
FFmpegProgressParser
¶
Parser for FFmpeg stderr progress output.
This parser extracts progress information from FFmpeg's stderr output, which includes frame count, time position, speed, and other metrics.
Attributes:
| Name | Type | Description |
|---|---|---|
total_duration |
Total duration of the video in seconds. |
Initialize the progress parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_duration
|
float
|
Total duration of the video in seconds. Used to calculate percentage progress. |
0.0
|
Source code in src/video_converter/utils/progress_parser.py
parse_line
¶
Parse a single line of FFmpeg stderr output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
A line from FFmpeg's stderr output. |
required |
Returns:
| Type | Description |
|---|---|
FFmpegProgress | None
|
FFmpegProgress object if progress info was found, None otherwise. |
Source code in src/video_converter/utils/progress_parser.py
get_last_progress
¶
Get the last parsed progress.
Returns:
| Type | Description |
|---|---|
FFmpegProgress
|
The most recently parsed progress, or empty progress if none parsed. |
parse_time_to_seconds
staticmethod
¶
Parse FFmpeg time string to seconds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_str
|
str
|
Time string in format HH:MM:SS.cs or similar. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Time in seconds. |
Source code in src/video_converter/utils/progress_parser.py
File Utils¶
file_utils
¶
File utilities for path and size management.
This module provides utility functions for file operations including path manipulation, size calculations, temporary file management, and disk space checks.
SDS Reference: SDS-U01-002
Example
from video_converter.utils.file_utils import ( ... expand_path, ... format_size, ... get_temp_path, ... check_disk_space, ... ) path = expand_path("~/Videos/Converted") print(path) /Users/username/Videos/Converted print(format_size(1536000000)) 1.43 GB
InsufficientSpaceError
¶
Bases: Exception
Raised when there is not enough disk space for an operation.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
The path where space was checked. |
|
required |
Required space in bytes. |
|
available |
Available space in bytes. |
Source code in src/video_converter/utils/file_utils.py
AtomicWriteError
¶
Bases: Exception
Raised when atomic write operation fails.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
The target path for the write operation. |
|
reason |
Description of why the write failed. |
Source code in src/video_converter/utils/file_utils.py
expand_path
¶
Expand user paths and resolve to absolute path.
Expands ~ to user's home directory and resolves relative paths to absolute paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path string or Path object to expand. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Fully expanded and resolved absolute Path. |
Example
expand_path("~/Videos/Converted") PosixPath('/Users/username/Videos/Converted') expand_path("./relative/path") PosixPath('/current/working/dir/relative/path')
Source code in src/video_converter/utils/file_utils.py
format_size
¶
Format bytes into human-readable size string.
Converts raw byte count to a human-readable format with appropriate unit suffix (B, KB, MB, GB, TB, PB).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_bytes
|
int | float
|
Size in bytes. |
required |
precision
|
int
|
Number of decimal places. Default is 2. |
2
|
Returns:
| Type | Description |
|---|---|
str
|
Human-readable size string (e.g., "1.43 GB"). |
Example
format_size(1023) '1023 B' format_size(1024) '1.00 KB' format_size(1536000000) '1.43 GB'
Source code in src/video_converter/utils/file_utils.py
parse_size
¶
Parse human-readable size string to bytes.
Converts a human-readable size string back to bytes. Supports B, KB, MB, GB, TB, PB units (case-insensitive).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_str
|
str
|
Size string to parse (e.g., "1.5 GB", "500MB"). |
required |
Returns:
| Type | Description |
|---|---|
int
|
Size in bytes. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the size string is invalid. |
Example
parse_size("1.5 GB") 1610612736 parse_size("500MB") 524288000
Source code in src/video_converter/utils/file_utils.py
get_temp_dir
¶
Get the temporary directory for video converter operations.
Creates a dedicated subdirectory in the system temp directory for video converter temporary files.
Returns:
| Type | Description |
|---|---|
Path
|
Path to the temporary directory. |
Source code in src/video_converter/utils/file_utils.py
get_temp_path
¶
Generate a unique temporary file path.
Creates a temporary file path in the video converter temp directory. The path is automatically registered for cleanup on exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str | Path
|
Original filename to base the temp name on. |
required |
suffix
|
str | None
|
Optional suffix to append (e.g., ".mp4"). |
None
|
unique
|
bool
|
If True, adds a unique identifier to prevent conflicts. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the temporary file location. |
Example
get_temp_path("vacation.mp4") PosixPath('/tmp/video_converter/vacation_a1b2c3d4.mp4') get_temp_path("video", suffix=".hevc") PosixPath('/tmp/video_converter/video_e5f6g7h8.hevc')
Source code in src/video_converter/utils/file_utils.py
create_temp_directory
¶
Create a temporary directory for video converter operations.
Creates a unique temporary directory that is automatically registered for cleanup on exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str | None
|
Optional prefix for the directory name. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the created temporary directory. |
Example
temp_dir = create_temp_directory("conversion") print(temp_dir) /tmp/video_converter/conversion_a1b2c3d4
Source code in src/video_converter/utils/file_utils.py
check_disk_space
¶
Check available disk space at the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to check disk space for. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Available disk space in bytes. |
Example
available = check_disk_space("/Users/username") print(format_size(available)) '128.5 GB'
Source code in src/video_converter/utils/file_utils.py
ensure_disk_space
¶
Ensure sufficient disk space is available.
Checks if there is enough disk space at the given path. For video conversion, it's recommended to have at least 2x the input file size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to check disk space for. |
required |
required_bytes
|
int
|
Minimum required space in bytes. |
required |
multiplier
|
float
|
Safety multiplier for required space. Default is 2.0. |
2.0
|
Raises:
| Type | Description |
|---|---|
InsufficientSpaceError
|
If there is not enough disk space. |
Example
ensure_disk_space("/output", input_size, multiplier=2.0)
Source code in src/video_converter/utils/file_utils.py
safe_move
¶
Safely move a file to a new location.
Moves a file with proper error handling. If the destination already exists, behavior depends on the overwrite flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
str | Path
|
Source file path. |
required |
dst
|
str | Path
|
Destination file path. |
required |
overwrite
|
bool
|
If True, overwrite existing destination. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the destination file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If source file doesn't exist. |
FileExistsError
|
If destination exists and overwrite is False. |
OSError
|
If move operation fails. |
Example
safe_move("/tmp/video.mp4", "/output/video.mp4") PosixPath('/output/video.mp4')
Source code in src/video_converter/utils/file_utils.py
safe_copy
¶
Safely copy a file to a new location.
Copies a file with proper error handling, preserving metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
str | Path
|
Source file path. |
required |
dst
|
str | Path
|
Destination file path. |
required |
overwrite
|
bool
|
If True, overwrite existing destination. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the destination file. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If source file doesn't exist. |
FileExistsError
|
If destination exists and overwrite is False. |
OSError
|
If copy operation fails. |
Example
safe_copy("/input/video.mp4", "/backup/video.mp4") PosixPath('/backup/video.mp4')
Source code in src/video_converter/utils/file_utils.py
safe_delete
¶
Safely delete a file.
Deletes a file with proper error handling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the file to delete. |
required |
missing_ok
|
bool
|
If True, don't raise error if file doesn't exist. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if file was deleted, False if it didn't exist. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist and missing_ok is False. |
OSError
|
If deletion fails. |
Example
safe_delete("/tmp/video.mp4") True
Source code in src/video_converter/utils/file_utils.py
atomic_write
¶
Context manager for atomic file writes.
Writes to a temporary file first, then atomically moves it to the target location. This prevents file corruption if the write is interrupted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Target file path. |
required |
mode
|
str
|
File open mode. Default is "wb" (write binary). |
'wb'
|
Yields:
| Type | Description |
|---|---|
Path
|
Path to the temporary file to write to. |
Raises:
| Type | Description |
|---|---|
AtomicWriteError
|
If the atomic move fails. |
Example
with atomic_write("/output/video.mp4") as temp_path: ... # Write to temp_path ... shutil.copy("input.mp4", temp_path)
File is now atomically moved to /output/video.mp4¶
Source code in src/video_converter/utils/file_utils.py
get_file_size
¶
Get the size of a file in bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the file. |
required |
Returns:
| Type | Description |
|---|---|
int
|
File size in bytes. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist. |
Example
size = get_file_size("/path/to/video.mp4") print(format_size(size)) '1.5 GB'
Source code in src/video_converter/utils/file_utils.py
get_directory_size
¶
Get the total size of all files in a directory.
Recursively calculates the total size of all files in a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the directory. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Total size in bytes. |
Raises:
| Type | Description |
|---|---|
NotADirectoryError
|
If path is not a directory. |
Example
size = get_directory_size("/path/to/videos") print(format_size(size)) '15.3 GB'
Source code in src/video_converter/utils/file_utils.py
cleanup_temp_files
¶
Clean up all registered temporary files and directories.
Removes all temporary files and directories that were created using get_temp_path() and create_temp_directory().
Returns:
| Type | Description |
|---|---|
int
|
Number of items cleaned up. |
Source code in src/video_converter/utils/file_utils.py
ensure_directory
¶
Ensure a directory exists, creating it if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the directory. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the directory. |
Example
ensure_directory("~/Videos/Converted") PosixPath('/Users/username/Videos/Converted')
Source code in src/video_converter/utils/file_utils.py
is_video_file
¶
Check if a path points to a video file based on extension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the file has a video extension. |
Example
is_video_file("/path/to/video.mp4") True is_video_file("/path/to/image.jpg") False
Source code in src/video_converter/utils/file_utils.py
generate_output_path
¶
Generate an output path for a converted video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_path
|
str | Path
|
Path to the input video. |
required |
output_dir
|
str | Path | None
|
Directory for output. If None, uses input directory. |
None
|
suffix
|
str
|
Suffix to add to filename. Default is "_hevc". |
'_hevc'
|
extension
|
str | None
|
New file extension. If None, keeps original. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Generated output path. |
Example
generate_output_path("/input/video.mp4") PosixPath('/input/video_hevc.mp4') generate_output_path("/input/video.mp4", "/output", suffix="") PosixPath('/output/video.mp4')
Source code in src/video_converter/utils/file_utils.py
Dependency Checker¶
dependency_checker
¶
Dependency checker for required external tools.
This module validates that all required external tools are installed and provides installation instructions for missing dependencies.
SDS Reference: SDS-U01-002
DependencyStatus
¶
Bases: Enum
Status of a dependency check.
DependencyInfo
dataclass
¶
DependencyInfo(name, required_version, current_version, status, install_instruction, description='')
Information about a single dependency.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name of the dependency. |
required_version |
str | None
|
Minimum required version (None if any version is OK). |
current_version |
str | None
|
Currently installed version (None if not found). |
status |
DependencyStatus
|
Status of the dependency check. |
install_instruction |
str
|
How to install this dependency. |
description |
str
|
Brief description of what this dependency is for. |
DependencyCheckResult
dataclass
¶
Result of checking all dependencies.
Attributes:
| Name | Type | Description |
|---|---|---|
dependencies |
list[DependencyInfo]
|
List of all checked dependencies. |
all_satisfied |
bool
|
Whether all dependencies are satisfied. |
DependencyChecker
¶
Check for required external dependencies.
This class validates that all required tools are installed and provides installation instructions for missing dependencies.
Example
checker = DependencyChecker() result = checker.check_all() if not result.all_satisfied: ... for dep in result.missing: ... print(f"Missing: {dep.name}") ... print(f"Install: {dep.install_instruction}")
Initialize dependency checker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command_runner
|
CommandRunner | None
|
CommandRunner instance to use. If None, creates a new one. |
None
|
Source code in src/video_converter/utils/dependency_checker.py
check_all
¶
Check all dependencies.
Returns:
| Type | Description |
|---|---|
DependencyCheckResult
|
DependencyCheckResult containing status of all dependencies. |
Source code in src/video_converter/utils/dependency_checker.py
check_macos
¶
Check macOS version.
Returns:
| Type | Description |
|---|---|
DependencyInfo
|
DependencyInfo for macOS. |
Source code in src/video_converter/utils/dependency_checker.py
check_python
¶
Check Python version.
Returns:
| Type | Description |
|---|---|
DependencyInfo
|
DependencyInfo for Python. |
Source code in src/video_converter/utils/dependency_checker.py
check_ffmpeg
¶
Check FFmpeg installation and version.
Returns:
| Type | Description |
|---|---|
DependencyInfo
|
DependencyInfo for FFmpeg. |
Source code in src/video_converter/utils/dependency_checker.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
check_videotoolbox
¶
Check VideoToolbox/hevc_videotoolbox encoder availability.
Returns:
| Type | Description |
|---|---|
DependencyInfo
|
DependencyInfo for VideoToolbox encoder. |
Source code in src/video_converter/utils/dependency_checker.py
check_exiftool
¶
Check ExifTool installation and version.
Returns:
| Type | Description |
|---|---|
DependencyInfo
|
DependencyInfo for ExifTool. |
Source code in src/video_converter/utils/dependency_checker.py
check_osxphotos
¶
Check osxphotos package installation.
Returns:
| Type | Description |
|---|---|
DependencyInfo
|
DependencyInfo for osxphotos. |
Source code in src/video_converter/utils/dependency_checker.py
format_report
¶
Format dependency check result as a readable report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
DependencyCheckResult
|
The dependency check result. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string report. |
Source code in src/video_converter/utils/dependency_checker.py
compare_versions
¶
Compare two version strings.
Supports semantic versioning (e.g., "5.0", "5.1.2", "6.0-beta").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version1
|
str
|
First version string. |
required |
version2
|
str
|
Second version string. |
required |
Returns:
| Type | Description |
|---|---|
int
|
-1 if version1 < version2 |
int
|
0 if version1 == version2 |
int
|
1 if version1 > version2 |
Example
compare_versions("5.0", "5.1") -1 compare_versions("6.0", "5.1") 1 compare_versions("5.1", "5.1") 0