Converters Module¶
Video encoding converters supporting hardware and software modes.
Overview¶
The converters module provides H.264 to H.265 conversion using:
- Hardware Converter: Apple VideoToolbox (fast, efficient)
- Software Converter: libx265 (high quality, flexible)
Base Converter¶
base
¶
Base converter interface for video encoding.
This module defines the abstract base class for all video converters, establishing a consistent interface for hardware and software encoders.
SDS Reference: SDS-V01-001 SRS Reference: SRS-201 (H.265 Encoding)
Example
from video_converter.converters.base import BaseConverter from video_converter.core import ConversionRequest, ConversionMode
class MyConverter(BaseConverter): ... def build_command(self, request: ConversionRequest) -> list[str]: ... return ["ffmpeg", "-i", str(request.input_path), ...] ... converter = MyConverter() request = ConversionRequest( ... input_path=Path("input.mov"), ... output_path=Path("output.mp4"), ... ) result = await converter.convert(request)
ConversionError
¶
Bases: Exception
Exception raised when video conversion fails.
EncoderNotAvailableError
¶
Bases: ConversionError
Exception raised when the required encoder is not available.
BaseConverter
¶
Bases: ABC
Abstract base class for video converters.
This class defines the interface that all video converters must implement. It provides common functionality for command execution and progress tracking.
Attributes:
| Name | Type | Description |
|---|---|---|
mode |
The conversion mode (hardware or software). |
Initialize the converter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
ConversionMode
|
The conversion mode to use. |
required |
Source code in src/video_converter/converters/base.py
encoder_name
abstractmethod
property
¶
Get the encoder name for FFmpeg.
Returns:
| Type | Description |
|---|---|
str
|
The encoder name (e.g., "hevc_videotoolbox", "libx265"). |
is_available
abstractmethod
¶
Check if this encoder is available on the system.
Returns:
| Type | Description |
|---|---|
bool
|
True if the encoder is available, False otherwise. |
build_command
abstractmethod
¶
Build the FFmpeg command for conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
ConversionRequest
|
The conversion request with input/output paths and settings. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of command arguments for FFmpeg. |
Source code in src/video_converter/converters/base.py
convert
async
¶
Convert a video file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
ConversionRequest
|
The conversion request. |
required |
on_progress
|
Callable[[float], None] | None
|
Optional callback for simple progress updates (0.0-1.0). |
None
|
on_progress_info
|
Callable[[ProgressInfo], None] | None
|
Optional callback for detailed progress updates with ProgressInfo containing ETA and other metrics. |
None
|
Returns:
| Type | Description |
|---|---|
ConversionResult
|
ConversionResult with success status and statistics. |
Raises:
| Type | Description |
|---|---|
ConversionError
|
If conversion fails. |
EncoderNotAvailableError
|
If the encoder is not available. |
Note
If both callbacks are provided, both will be called. on_progress_info provides more detailed information including eta_seconds, eta_formatted, and current encoding speed.
Source code in src/video_converter/converters/base.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 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 | |
cancel
¶
Cancel the current conversion.
convert_sync
¶
Synchronous wrapper for convert.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
ConversionRequest
|
The conversion request. |
required |
on_progress
|
Callable[[float], None] | None
|
Optional callback for simple progress updates (0.0-1.0). |
None
|
on_progress_info
|
Callable[[ProgressInfo], None] | None
|
Optional callback for detailed progress updates with ProgressInfo containing ETA and other metrics. |
None
|
Returns:
| Type | Description |
|---|---|
ConversionResult
|
ConversionResult with success status and statistics. |
Source code in src/video_converter/converters/base.py
Hardware Converter¶
hardware
¶
Hardware video converter using VideoToolbox.
This module implements hardware-accelerated H.265 encoding using macOS VideoToolbox through FFmpeg's hevc_videotoolbox encoder.
SDS Reference: SDS-V01-002 SRS Reference: SRS-203 (Hardware Encoding)
Example
from video_converter.converters.hardware import HardwareConverter from video_converter.core import ConversionRequest
converter = HardwareConverter() if converter.is_available(): ... request = ConversionRequest( ... input_path=Path("input.mov"), ... output_path=Path("output.mp4"), ... ) ... result = await converter.convert(request) ... print(f"Success: {result.success}")
HardwareConverter
¶
Bases: BaseConverter
Hardware-accelerated H.265 converter using VideoToolbox.
This converter uses Apple's VideoToolbox framework through FFmpeg for fast, hardware-accelerated H.265 encoding on macOS.
Attributes:
| Name | Type | Description |
|---|---|---|
mode |
Always HARDWARE for this converter. |
Initialize the hardware converter.
Source code in src/video_converter/converters/hardware.py
encoder_name
property
¶
Get the encoder name.
Returns:
| Type | Description |
|---|---|
str
|
The VideoToolbox HEVC encoder name. |
is_available
¶
Check if VideoToolbox encoder is available.
Returns:
| Type | Description |
|---|---|
bool
|
True if hevc_videotoolbox is available, False otherwise. |
Source code in src/video_converter/converters/hardware.py
build_command
¶
Build FFmpeg command for VideoToolbox encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
ConversionRequest
|
The conversion request with settings. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
FFmpeg command arguments. |
Source code in src/video_converter/converters/hardware.py
create_hardware_converter
¶
Factory function to create a hardware converter if available.
Returns:
| Type | Description |
|---|---|
HardwareConverter | None
|
HardwareConverter instance if available, None otherwise. |
Source code in src/video_converter/converters/hardware.py
Software Converter¶
software
¶
Software video converter using libx265.
This module implements software-based H.265 encoding using the libx265 encoder through FFmpeg.
SDS Reference: SDS-V01-003 SRS Reference: SRS-202 (Software Encoding)
Example
from video_converter.converters.software import SoftwareConverter from video_converter.core import ConversionRequest
converter = SoftwareConverter() if converter.is_available(): ... request = ConversionRequest( ... input_path=Path("input.mov"), ... output_path=Path("output.mp4"), ... preset="medium", ... crf=22, ... ) ... result = await converter.convert(request) ... print(f"Success: {result.success}")
SoftwareConverter
¶
Bases: BaseConverter
Software-based H.265 converter using libx265.
This converter uses the libx265 encoder for CPU-based H.265 encoding. It's slower than hardware encoding but more widely compatible and offers more fine-grained control over encoding parameters.
Supports both 8-bit and 10-bit encoding for HDR content.
Attributes:
| Name | Type | Description |
|---|---|---|
mode |
Always SOFTWARE for this converter. |
Initialize the software converter.
Source code in src/video_converter/converters/software.py
encoder_name
property
¶
Get the encoder name.
Returns:
| Type | Description |
|---|---|
str
|
The libx265 encoder name. |
is_available
¶
Check if libx265 encoder is available.
Returns:
| Type | Description |
|---|---|
bool
|
True if libx265 is available, False otherwise. |
Source code in src/video_converter/converters/software.py
build_command
¶
Build FFmpeg command for libx265 encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
ConversionRequest
|
The conversion request with settings. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
FFmpeg command arguments. |
Source code in src/video_converter/converters/software.py
create_software_converter
¶
Factory function to create a software converter if available.
Returns:
| Type | Description |
|---|---|
SoftwareConverter | None
|
SoftwareConverter instance if available, None otherwise. |
Source code in src/video_converter/converters/software.py
Factory¶
factory
¶
Converter factory for creating appropriate video converters.
This module provides a factory pattern implementation for selecting and instantiating the appropriate video converter based on system capabilities and user preferences.
SDS Reference: SDS-V01-004 SRS Reference: SRS-204 (Encoder Selection)
Example
from video_converter.converters.factory import ConverterFactory from video_converter.core import ConversionMode
factory = ConverterFactory()
Get best available converter¶
converter = factory.get_converter()
Get specific converter type¶
hw_converter = factory.get_converter(ConversionMode.HARDWARE)
Check what's available¶
available = factory.get_available_converters() print(f"Available: {[c.encoder_name for c in available]}")
ConverterFactory
¶
Factory for creating video converters.
This factory manages converter instances and provides methods for selecting the most appropriate converter based on availability and user preferences.
Initialize the converter factory.
Source code in src/video_converter/converters/factory.py
get_converter
¶
Get a converter for the specified mode.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
ConversionMode | None
|
Preferred conversion mode. If None, selects best available. |
None
|
fallback
|
bool
|
Whether to fall back to alternative if preferred unavailable. |
True
|
Returns:
| Type | Description |
|---|---|
BaseConverter
|
A converter instance. |
Raises:
| Type | Description |
|---|---|
EncoderNotAvailableError
|
If no suitable encoder is available. |
Source code in src/video_converter/converters/factory.py
get_available_converters
¶
Get list of all available converters.
Returns:
| Type | Description |
|---|---|
list[BaseConverter]
|
List of available converter instances. |
Source code in src/video_converter/converters/factory.py
is_hardware_available
¶
Check if hardware encoding is available.
Returns:
| Type | Description |
|---|---|
bool
|
True if VideoToolbox encoder is available. |
Source code in src/video_converter/converters/factory.py
is_software_available
¶
Check if software encoding is available.
Returns:
| Type | Description |
|---|---|
bool
|
True if libx265 encoder is available. |
Source code in src/video_converter/converters/factory.py
get_converter
¶
Get a converter using the default factory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
ConversionMode | None
|
Preferred conversion mode. If None, selects best available. |
None
|
fallback
|
bool
|
Whether to fall back to alternative if preferred unavailable. |
True
|
Returns:
| Type | Description |
|---|---|
BaseConverter
|
A converter instance. |
Source code in src/video_converter/converters/factory.py
Progress¶
progress
¶
Real-time conversion progress monitoring module.
This module provides real-time progress monitoring for FFmpeg video conversions, including accurate progress tracking, ETA calculation, and callback-based updates.
SDS Reference: SDS-V01-005 SRS Reference: SRS-205 (Real-time Progress Monitoring)
Example
from video_converter.converters.progress import ProgressMonitor, ProgressInfo
def on_progress(info: ProgressInfo) -> None: ... print(f"Progress: {info.percentage:.1f}% | ETA: {info.eta_formatted}")
monitor = ProgressMonitor(total_duration=180.5, callback=on_progress) monitor.on_output("frame= 720 fps=180 time=00:00:24.00 speed=6.0x")
ProgressInfo
dataclass
¶
ProgressInfo(frame=0, fps=0.0, current_time=0.0, total_time=0.0, current_size=0, bitrate=0.0, speed=0.0, quality=0.0)
Progress information for video conversion.
This dataclass contains all progress metrics extracted from FFmpeg output, including calculated properties for percentage and ETA.
Attributes:
| Name | Type | Description |
|---|---|---|
frame |
int
|
Current frame number being processed. |
fps |
float
|
Encoding frames per second. |
current_time |
float
|
Current position in seconds. |
total_time |
float
|
Total video duration in seconds. |
current_size |
int
|
Current output file size in bytes. |
bitrate |
float
|
Current encoding bitrate in kbps. |
speed |
float
|
Encoding speed multiplier (e.g., 6.0 means 6x realtime). |
quality |
float
|
FFmpeg quality parameter (q value). |
percentage
property
¶
Calculate completion percentage.
Returns:
| Type | Description |
|---|---|
float
|
Completion percentage from 0.0 to 100.0. |
eta_seconds
property
¶
Calculate estimated time remaining in seconds.
Returns:
| Type | Description |
|---|---|
float
|
Estimated seconds remaining, or infinity if speed is zero. |
eta_formatted
property
¶
Get human-readable ETA string.
Returns:
| Type | Description |
|---|---|
str
|
Formatted ETA string like "1h 30m", "5m 30s", or "calculating...". |
size_formatted
property
¶
Get human-readable current size string.
Returns:
| Type | Description |
|---|---|
str
|
Formatted size string like "1.5 GB", "256 MB", or "0 B". |
ProgressParser
¶
Parser for FFmpeg stderr progress output.
This parser extracts real-time progress information from FFmpeg's stderr, converting it into ProgressInfo objects for further processing.
Attributes:
| Name | Type | Description |
|---|---|---|
total_duration |
Total video duration in seconds. |
Initialize the progress parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_duration
|
float
|
Total video duration in seconds. Used for percentage and ETA calculations. |
0.0
|
Source code in src/video_converter/converters/progress.py
last_info
property
¶
Get the most recently parsed progress info.
Returns:
| Type | Description |
|---|---|
ProgressInfo | None
|
The last ProgressInfo parsed, or None if nothing parsed yet. |
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 |
|---|---|
ProgressInfo | None
|
ProgressInfo object if progress data was found, None otherwise. |
Source code in src/video_converter/converters/progress.py
ProgressMonitor
dataclass
¶
Monitor and report FFmpeg conversion progress.
This class combines parsing and callback functionality to provide a complete progress monitoring solution. It handles debouncing, minimum update intervals, and error handling.
Attributes:
| Name | Type | Description |
|---|---|---|
total_duration |
float
|
Total video duration in seconds. |
callback |
Callable[[ProgressInfo], None]
|
Function to call with progress updates. |
min_interval |
float
|
Minimum seconds between callback invocations. |
__post_init__
¶
on_output
¶
Process a line of FFmpeg output.
This method parses the line and invokes the callback if new progress information is available and enough time has passed since the last callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
A line from FFmpeg's stderr output. |
required |
Source code in src/video_converter/converters/progress.py
get_current_progress
¶
Get the current progress without triggering a callback.
Returns:
| Type | Description |
|---|---|
ProgressInfo | None
|
The most recent ProgressInfo, or None if not available. |
force_callback
¶
Force a callback with current progress, ignoring interval.
Useful for final progress update at 100% completion.
Source code in src/video_converter/converters/progress.py
create_simple_callback
¶
Create a simple console progress callback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show_bar
|
bool
|
Whether to show a visual progress bar. |
True
|
bar_width
|
int
|
Width of the progress bar in characters. |
30
|
Returns:
| Type | Description |
|---|---|
Callable[[ProgressInfo], None]
|
A callback function suitable for ProgressMonitor. |
Example
callback = create_simple_callback() monitor = ProgressMonitor(duration, callback)