Core Module
This module contains the core classes for video and audio handling.
- class fmusvid.core.video.Video(path=None, backend='auto', **kwargs)[source]
Bases:
objectMain Video class for FMUS-VID.
This class provides a unified interface for all video operations.
- save(path, **kwargs)[source]
Save video to file.
- Parameters:
- Return type:
Example
>>> video.save("output.mp4") >>> video.save("output.mp4", codec="h264", bitrate="5M")
- async save_async(path, progress_callback=None, **kwargs)[source]
Save video asynchronously.
- Parameters:
- Return type:
Example
>>> await video.save_async("output.mp4", ... progress_callback=lambda p: print(f"{p*100:.1f}%"))
- info()[source]
Get video information.
- Return type:
VideoInfo- Returns:
VideoInfo object
Example
>>> info = video.info() >>> print(f"Resolution: {info.width}x{info.height}")
- get_info()[source]
Get video information as a dictionary. For CLI compatibility.
Example
>>> info_dict = video.get_info() >>> print(f"Resolution: {info_dict['width']}x{info_dict['height']}")
- trim(start, end=None)[source]
Trim video to specified time range.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video.trim(10, 20) # Keep only seconds 10-20
- resize(width=None, height=None, keep_aspect=True)[source]
Resize video to specified dimensions.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video.resize(720) # Resize to 720p (height auto-calculated) >>> video.resize(width=1280, height=720) # Resize to 720p
- crop(x, y, width, height)[source]
Crop video to specified region.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video.crop(100, 100, 500, 300) # Crop to 500x300 region
- rotate(degrees)[source]
Rotate video by specified degrees.
- Parameters:
degrees (
float) – Rotation angle in degrees- Return type:
- Returns:
Self for method chaining
Example
>>> video.rotate(90) # Rotate 90 degrees clockwise
- grayscale()[source]
Convert video to grayscale.
- Return type:
- Returns:
Self for method chaining
Example
>>> video.grayscale() # Convert to black and white
- blur(radius)[source]
Apply Gaussian blur with specified radius.
Example
>>> video.blur(5) # Apply blur with radius 5
- brightness(factor)[source]
Adjust video brightness.
- Parameters:
factor (
float) – Brightness factor (1.0 = original, 1.5 = +50%, 0.5 = -50%)- Return type:
- Returns:
Self for method chaining
Example
>>> video.brightness(1.2) # Increase brightness by 20%
- contrast(factor)[source]
Adjust video contrast.
- Parameters:
factor (
float) – Contrast factor (1.0 = original, 1.5 = +50%, 0.5 = -50%)- Return type:
- Returns:
Self for method chaining
Example
>>> video.contrast(1.2) # Increase contrast by 20%
- mute()[source]
Remove audio track.
- Return type:
- Returns:
Self for method chaining
Example
>>> video.mute() # Remove audio
- volume(level)[source]
Set audio volume.
- Parameters:
level (
float) – Volume level (1.0 = original, 0.5 = 50%, 2.0 = 200%)- Return type:
- Returns:
Self for method chaining
Example
>>> video.volume(0.8) # Reduce volume to 80%
- add_audio(audio_path, start=0, volume=1.0)[source]
Add audio track to the video.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video.add_audio("music.mp3", start=5, volume=0.8)
- add_subtitles(subtitle_path, font='Arial', size=24, color='white', position=None)[source]
Add subtitles to the video.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video.add_subtitles("subtitles.srt") >>> video.add_subtitles("subtitles.vtt", font="DejaVuSans.ttf", size=32)
- add_subtitle_text(text, start_time, end_time, font='Arial', size=24, color='white', position=None)[source]
Add a single subtitle entry directly to the video.
- Parameters:
text (
str) – Subtitle textstart_time (
float) – Start time in secondsend_time (
float) – End time in secondsfont (
str) – Font name or pathsize (
int) – Font size in pixelscolor (
Union[str,Tuple[int,int,int]]) – Text color (name or RGB tuple)position (
Optional[Tuple[int,int]]) – Optional (x, y) position (None for bottom center)
- Return type:
- Returns:
Self for method chaining
Example
>>> video.add_subtitle_text("Hello world", 5.0, 10.0)
- extract_frame(time)[source]
Extract a single frame at specified time.
- Parameters:
time (
float) – Time in seconds- Returns:
Frame object
Example
>>> frame = video.extract_frame(10.5) >>> frame.save("thumbnail.jpg")
- get_frame(time)[source]
Extract a single frame at specified time (alias for extract_frame).
- Parameters:
time (
float) – Time in seconds- Returns:
Frame object (numpy array)
Example
>>> frame = video.get_frame(1.0) >>> frame.shape # (height, width, channels)
- overlay(video, position=(0, 0), start=0, duration=None, opacity=1.0)[source]
Overlay another video (picture-in-picture).
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> main_video.overlay(pip_video, position=(50, 50), opacity=0.8)
- classmethod create(width, height, duration, fps=30, color=(0, 0, 0), backend='auto', **kwargs)[source]
Create a blank video canvas.
- Parameters:
- Return type:
- Returns:
Video object
Example
>>> canvas = Video.create(1920, 1080, 10) # 10 second blank video
- classmethod concat(videos, backend='auto', **kwargs)[source]
Concatenate multiple videos.
- Parameters:
- Return type:
- Returns:
New concatenated Video object
Example
>>> combined = Video.concat([video1, video2, video3])
- classmethod grid(videos, rows, cols, backend='auto', **kwargs)[source]
Arrange videos in a grid layout.
- Parameters:
- Return type:
- Returns:
New grid Video object
Example
>>> grid_video = Video.grid([video1, video2, video3, video4], rows=2, cols=2)
- speed(factor)[source]
Change video playback speed.
- Parameters:
factor (
float) – Speed factor (2.0 = 2x faster, 0.5 = half speed)- Return type:
- Returns:
Self for method chaining
Example
>>> video.speed(2.0) # 2x speed
- saturation(factor)[source]
Adjust video saturation.
- Parameters:
factor (
float) – Saturation factor (1.0 = original, 1.5 = +50%, 0.5 = -50%, 0 = grayscale)- Return type:
- Returns:
Self for method chaining
Example
>>> video.saturation(1.5) # Increase saturation
- fade_in(duration)[source]
Fade video in from black.
- Parameters:
duration (
float) – Fade duration in seconds- Return type:
- Returns:
Self for method chaining
Example
>>> video.fade_in(1.0) # 1 second fade in
- fade_out(duration)[source]
Fade video out to black.
- Parameters:
duration (
float) – Fade duration in seconds- Return type:
- Returns:
Self for method chaining
Example
>>> video.fade_out(1.0) # 1 second fade out
- reverse()[source]
Reverse video playback.
- Return type:
- Returns:
Self for method chaining
Example
>>> video.reverse()
- chroma_key(color, similarity=0.1, blend=0.0)[source]
Remove green screen / chroma key.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video.chroma_key((0, 255, 0)) # Remove green screen
- crossfade(other_video, duration)[source]
Crossfade between this video and another.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video1.crossfade(video2, 1.0)
- fade_audio_in(duration)[source]
Fade audio in.
- Parameters:
duration (
float) – Fade duration in seconds- Return type:
- Returns:
Self for method chaining
Example
>>> video.fade_audio_in(1.0)
- fade_audio_out(duration)[source]
Fade audio out.
- Parameters:
duration (
float) – Fade duration in seconds- Return type:
- Returns:
Self for method chaining
Example
>>> video.fade_audio_out(1.0)
- normalize_audio(target_db=-16.0)[source]
Normalize audio to target dB level.
- Parameters:
target_db (
float) – Target loudness in dB (typically -16 to -20)- Return type:
- Returns:
Self for method chaining
Example
>>> video.normalize_audio(-16.0)
- replace_audio(audio_path)[source]
Replace audio track with new audio.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video.replace_audio("new_audio.mp3")
- to_gif(path, fps=None, quality=95, loop=0, width=None)[source]
Export video as animated GIF.
- Parameters:
- Return type:
Example
>>> video.to_gif("output.gif", fps=10, width=320)
- to_image_sequence(output_dir, prefix='frame_', format='png', start=None, end=None, fps=None)[source]
Export video frames as images.
- Parameters:
- Return type:
- Returns:
List of exported frame paths
Example
>>> frames = video.to_image_sequence("frames/", prefix="img_", format="jpg")
- add_text(text, position='center', font='Arial', size=32, color='white', duration=None, start_time=0)[source]
Add text overlay to the video.
- Parameters:
text (
str) – Text to displayposition (
Union[Tuple[int,int],str]) – Position as (x, y) tuple or preset (“center”, “top”, “bottom”, etc.)font (
str) – Font name or path to font filesize (
int) – Font size in pixelscolor (
Union[str,Tuple[int,int,int]]) – Text color (name or RGB tuple)duration (
Optional[float]) – Duration to display text (None = entire video)start_time (
float) – When to start displaying text
- Return type:
- Returns:
Self for method chaining
Example
>>> video.add_text("Hello World", position="center", size=48)
- add_image(image_path, position=(0, 0), opacity=1.0, scale=1.0, duration=None, start_time=0)[source]
Add image overlay to the video.
- Parameters:
- Return type:
- Returns:
Self for method chaining
Example
>>> video.add_image("logo.png", position=(10, 10), opacity=0.8)
- apply_filter(filter_string)[source]
Apply custom FFmpeg filter string.
- Parameters:
filter_string (
str) – FFmpeg filter string- Return type:
- Returns:
Self for method chaining
Example
>>> video.apply_filter("negate") # Invert colors
- detect_scenes(threshold=30.0, min_scene_length=15)[source]
Detect scene changes in the video.
- Parameters:
- Return type:
- Returns:
List of scene change objects
Example
>>> scenes = video.detect_scenes()
- analyze_audio()[source]
Analyze audio content of the video.
- Return type:
- Returns:
Audio analysis results
Example
>>> analysis = video.analyze_audio()
- class fmusvid.core.frame.Frame(frame_data, backend)[source]
Bases:
objectClass for handling individual video frames.
This class provides methods for manipulating and saving frames.
- save(path, **kwargs)[source]
Save frame to file.
Example
>>> frame.save("thumbnail.jpg", quality=95)
- resize(width=None, height=None, keep_aspect=True)[source]
Resize frame to specified dimensions.
- Parameters:
- Return type:
- Returns:
New Frame object
Example
>>> thumbnail = frame.resize(width=320)
- crop(x, y, width, height)[source]
Crop frame to specified region.
- Parameters:
- Return type:
- Returns:
New Frame object
Example
>>> cropped = frame.crop(100, 100, 400, 300)
- grayscale()[source]
Convert frame to grayscale.
- Return type:
- Returns:
New Frame object
Example
>>> bw = frame.grayscale()
- blur(radius)[source]
Apply Gaussian blur with specified radius.
Example
>>> blurred = frame.blur(5)
- brightness(factor)[source]
Adjust frame brightness.
- Parameters:
factor (
float) – Brightness factor (1.0 = original, 1.5 = +50%, 0.5 = -50%)- Return type:
- Returns:
New Frame object
Example
>>> brighter = frame.brightness(1.2)
- contrast(factor)[source]
Adjust frame contrast.
- Parameters:
factor (
float) – Contrast factor (1.0 = original, 1.5 = +50%, 0.5 = -50%)- Return type:
- Returns:
New Frame object
Example
>>> higher_contrast = frame.contrast(1.2)
- overlay(image, position=(0, 0), opacity=1.0)[source]
Overlay an image on the frame.
- Parameters:
- Return type:
- Returns:
New Frame object
Example
>>> with_logo = frame.overlay(logo, position=(20, 20), opacity=0.8)
- add_text(text, position, font='Arial', size=24, color='white')[source]
Add text to the frame.
- Parameters:
- Return type:
- Returns:
New Frame object
Example
>>> with_caption = frame.add_text("Hello World", position=(50, 50), size=32)
- to_pil()[source]
Convert frame to PIL Image.
- Returns:
PIL Image object
Example
>>> pil_image = frame.to_pil() >>> pil_image.show()
- class fmusvid.core.audio.Audio(audio_data, backend)[source]
Bases:
objectClass for handling audio operations.
This class provides methods for manipulating audio.
- save(path, **kwargs)[source]
Save audio to file.
- Parameters:
- Return type:
Example
>>> audio.save("output.mp3", codec="mp3", bitrate="192k")
- trim(start, end=None)[source]
Trim audio to specified time range.
- Parameters:
- Return type:
- Returns:
New Audio object
Example
>>> intro = audio.trim(0, 10) # First 10 seconds
- volume(level)[source]
Set audio volume.
- Parameters:
level (
float) – Volume level (1.0 = original, 0.5 = 50%, 2.0 = 200%)- Return type:
- Returns:
New Audio object
Example
>>> quieter = audio.volume(0.8) # Reduce volume to 80%
- fade_in(duration)[source]
Add fade-in effect.
- Parameters:
duration (
float) – Fade duration in seconds- Return type:
- Returns:
New Audio object
Example
>>> with_fade = audio.fade_in(2) # 2-second fade in
- fade_out(duration)[source]
Add fade-out effect.
- Parameters:
duration (
float) – Fade duration in seconds- Return type:
- Returns:
New Audio object
Example
>>> with_fade = audio.fade_out(2) # 2-second fade out
- normalize(target_db=-3)[source]
Normalize audio levels.
- Parameters:
target_db (
float) – Target peak level in dB- Return type:
- Returns:
New Audio object
Example
>>> normalized = audio.normalize(-3) # Normalize to -3 dB
- mix(other, weight=0.5)[source]
Mix with another audio track.
- Parameters:
- Return type:
- Returns:
New Audio object
Example
>>> mixed = voice.mix(music, weight=0.3) # Mix voice with background music
- info()[source]
Get audio information.
- Return type:
- Returns:
- Dictionary with audio information
duration: Duration in seconds
channels: Number of channels
sample_rate: Sample rate in Hz
codec: Audio codec
bitrate: Audio bitrate
Example
>>> info = audio.info() >>> print(f"Duration: {info['duration']} seconds")
Audio utility functions for FMUS-VID.
- fmusvid.core.audio_utils.save_audio(path, audio_data, sample_rate)[source]
Save audio data to a WAV file.
- Parameters:
path – Output file path
audio_data – Audio data as numpy array
sample_rate – Sample rate in Hz
Example
>>> import numpy as np >>> sample_rate = 44100 >>> duration = 3.0 >>> t = np.linspace(0, duration, int(sample_rate * duration)) >>> audio_data = np.sin(2 * np.pi * 440 * t) >>> save_audio("output.wav", audio_data, sample_rate)