Functions
extractAudio
Extracts the audio track from a video file and saves it as a WAV file.Path to the input video file
Path where the extracted audio will be saved (WAV format)
Path to the extracted audio file on success, or
None if extraction failedFeatures
- Format: Outputs WAV format for maximum compatibility
- Quality: Preserves original audio quality
- Error Handling: Returns
Noneon failure with error message - Automatic Cleanup: Properly closes video clip after extraction
Error Handling
The function automatically creates parent directories for the output path if they don’t exist.
crop_video
Extracts a specific time segment from a video file.Path to the input video file
Path where the cropped video segment will be saved
Start time of the segment in seconds (supports decimals)
End time of the segment in seconds (supports decimals)
Features
- Precise Timing: Supports fractional seconds (e.g., 31.92)
- Safety Checks: Automatically caps end time to video duration
- Quality Preservation: Uses libx264 codec for high quality
- Audio Included: Preserves audio track in the segment
Automatic Duration Capping
The function automatically handles invalid end times:- Video duration: 300 seconds
- Requested:
crop_video("video.mp4", "out.mp4", 250, 350) - Result: Crops from 250s to 299.9s (capped)
- Console:
Warning: Requested end time (350s) exceeds video duration (300s). Capping to 299.9s
Output Specifications
extractAudio
- Format: WAV (Waveform Audio File Format)
- Channels: Preserves original (mono/stereo)
- Sample Rate: Preserves original
- Bit Depth: Preserves original
crop_video
- Video Codec: libx264 (H.264)
- Audio Codec: Preserves original (typically AAC or MP3)
- FPS: Preserves original frame rate
- Resolution: Preserves original dimensions
- Quality: High quality (lossless where possible)
Timing Examples
Use Cases
- Highlight Extraction
- Chapter Splitting
- Intro/Outro Removal
Error Handling
- File not found: Invalid input path
- No audio track: Video has no audio
- Invalid time range:
start_time >= end_time - Corrupted video: Unable to read video file
Performance Tips
- Audio Extraction: Fast (~5-10s for a 10-minute video)
- Video Cropping: Depends on segment length and codec
- 2-minute segment: ~30-60 seconds
- Longer segments: Proportionally longer
- Memory Usage: Loads entire segment into memory
- Recommend ≤5 minute segments at a time
- Concurrent Processing: Use unique filenames (see main.py example)
