Skip to main content
The Edit component provides core video editing functions for extracting audio and cropping videos by time ranges.

Functions

extractAudio

Extracts the audio track from a video file and saves it as a WAV file.
extractAudio(video_path: str, audio_path: str = "audio.wav") -> str | None
video_path
str
required
Path to the input video file
audio_path
str
default:"audio.wav"
Path where the extracted audio will be saved (WAV format)
audio_path
str | None
Path to the extracted audio file on success, or None if extraction failed

Features

  • Format: Outputs WAV format for maximum compatibility
  • Quality: Preserves original audio quality
  • Error Handling: Returns None on failure with error message
  • Automatic Cleanup: Properly closes video clip after extraction
from Components.Edit import extractAudio

# Extract audio with default filename
audio_file = extractAudio("video.mp4")
if audio_file:
    print(f"Audio extracted to: {audio_file}")
    # Output: Extracted audio to: audio.wav
else:
    print("Audio extraction failed")

Error Handling

audio_path = extractAudio("video.mp4")

if audio_path is None:
    print("Failed to extract audio")
    # Possible causes:
    # - Video file not found
    # - Video has no audio track
    # - Insufficient permissions
    # - Corrupted video file
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.
crop_video(input_file: str, output_file: str, 
           start_time: float, end_time: float) -> None
input_file
str
required
Path to the input video file
output_file
str
required
Path where the cropped video segment will be saved
start_time
float
required
Start time of the segment in seconds (supports decimals)
end_time
float
required
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
from Components.Edit import crop_video

# Extract 2-minute segment
crop_video(
    input_file="full_video.mp4",
    output_file="highlight.mp4",
    start_time=45.5,
    end_time=165.5
)

# Output: Creates highlight.mp4 with 120-second segment

Automatic Duration Capping

The function automatically handles invalid end times:
with VideoFileClip(input_file) as video:
    # Ensure end_time doesn't exceed video duration
    max_time = video.duration - 0.1  # 0.1s buffer for safety
    if end_time > max_time:
        print(f"Warning: Requested end time ({end_time}s) exceeds " +
              f"video duration ({video.duration}s). Capping to {max_time}s")
        end_time = max_time
    
    cropped_video = video.subclip(start_time, end_time)
Example:
  • 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

# Precise decimal timing
crop_video("video.mp4", "segment.mp4", 31.92, 49.2)

# Whole seconds
crop_video("video.mp4", "segment.mp4", 30, 60)

# From start of video
crop_video("video.mp4", "intro.mp4", 0, 10)

# Last 30 seconds (if video is 300s long)
crop_video("video.mp4", "outro.mp4", 270, 300)

Use Cases

# Extract AI-selected highlight
transcriptions = transcribeAudio(audio)
TransText = "\n".join([f"{s} - {e}: {t}" 
                       for t, s, e in transcriptions])
start, end = GetHighlight(TransText)
crop_video(video_path, "highlight.mp4", start, end)

Error Handling

try:
    audio = extractAudio("video.mp4")
    if audio is None:
        raise ValueError("Audio extraction failed")
    
    crop_video("video.mp4", "segment.mp4", 30, 90)
except FileNotFoundError:
    print("Video file not found")
except Exception as e:
    print(f"Error: {e}")
Common errors:
  • 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
Requires MoviePy package. Video cropping can be memory-intensive for large files or long segments.

Performance Tips

  1. Audio Extraction: Fast (~5-10s for a 10-minute video)
  2. Video Cropping: Depends on segment length and codec
    • 2-minute segment: ~30-60 seconds
    • Longer segments: Proportionally longer
  3. Memory Usage: Loads entire segment into memory
    • Recommend ≤5 minute segments at a time
  4. Concurrent Processing: Use unique filenames (see main.py example)

Concurrent Execution Support

For processing multiple videos simultaneously:
import uuid

# Generate unique session ID
session_id = str(uuid.uuid4())[:8]

# Use unique filenames
audio_file = f"audio_{session_id}.wav"
temp_clip = f"clip_{session_id}.mp4"

extractAudio(video_path, audio_file)
crop_video(video_path, temp_clip, start, end)

# Clean up when done
import os
os.remove(audio_file)
os.remove(temp_clip)

Build docs developers (and LLMs) love