Basic Video Operations
Learn about fundamental video operations in FMUS-VID.
Video Properties
import fmusvid
video = fmusvid.load("input.mp4")
# Access video properties
print(f"Duration: {video.duration}")
print(f"Frame count: {video.frame_count}")
print(f"Frame rate: {video.fps}")
print(f"Resolution: {video.width}x{video.height}")
print(f"Has audio: {video.has_audio()}")
Trimming and Cutting
# Trim by time
trimmed = video.trim(start=5.0, end=15.0)
# Trim by frame numbers
trimmed = video.trim_frames(start_frame=150, end_frame=450)
# Cut into segments
segments = video.cut([
(0, 10), # First 10 seconds
(20, 30), # 20-30 seconds
(40, 50) # 40-50 seconds
])
Resizing and Scaling
# Resize by width (maintain aspect ratio)
resized = video.resize(width=1920)
# Resize by height
resized = video.resize(height=1080)
# Specify both dimensions
resized = video.resize(width=1280, height=720)
# Scale by factor
scaled = video.scale(1.5) # Scale up by 50%
Rotation and Flipping
# Rotate by degrees
rotated = video.rotate(90)
# Flip horizontally or vertically
flipped_h = video.flip(horizontal=True)
flipped_v = video.flip(vertical=True)
Speed Adjustment
# Speed up or slow down
fast = video.speed(2.0) # 2x speed
slow = video.speed(0.5) # Half speed
# Speed with frame interpolation
smooth = video.speed(
factor=0.5,
interpolation="flow" # Use optical flow
)
Filters and Effects
# Basic filters
grayscale = video.grayscale()
blurred = video.blur(radius=2.0)
# Color adjustments
adjusted = (video
.brightness(1.2) # Increase brightness
.contrast(1.1) # Adjust contrast
.saturation(0.8) # Reduce saturation
)
# Advanced effects
stylized = (video
.sharpen(amount=1.5)
.denoise(strength=0.5)
.vignette(intensity=0.3)
)
Audio Processing
# Volume control
quiet = video.volume(0.5)
muted = video.mute()
# Add background music
with_music = video.add_audio(
"music.mp3",
start=0, # Start time
volume=0.3, # Background volume
fade_in=2.0, # Fade in duration
fade_out=2.0 # Fade out duration
)
# Extract audio
video.extract_audio("audio.mp3")
Batch Processing
from pathlib import Path
# Process multiple videos
input_dir = Path("input_videos")
output_dir = Path("output_videos")
output_dir.mkdir(exist_ok=True)
for video_path in input_dir.glob("*.mp4"):
video = fmusvid.load(video_path)
# Process each video
processed = (video
.resize(height=720)
.brightness(1.2)
.contrast(1.1)
)
# Save with same name
output_path = output_dir / video_path.name
processed.save(output_path)
Progress Tracking
def progress_callback(progress: float):
print(f"Progress: {progress:.1%}")
# Track processing progress
video.save(
"output.mp4",
progress_callback=progress_callback
)
Best Practices
Chain Operations
Use method chaining for cleaner code
Operations are applied in order
Memory Management
Process in chunks for large files
Use
cleanup()to free resources
Error Handling
try: video = fmusvid.load("input.mp4") processed = video.resize(width=1280).rotate(90) processed.save("output.mp4") except fmusvid.VideoError as e: print(f"Error processing video: {e}") finally: video.cleanup()
Performance Tips
Use appropriate codec for output
Enable hardware acceleration when available
Process in batches for multiple files