⚡ Handling Video Streams in Gradio (`gr.Video`)
The gr.Video component allows users to upload video files or stream recordings directly in the browser. Gradio handles video input paths seamlessly, enabling developers to process frames using OpenCV, extract clips, apply color filters, and stream processed video outputs back to the UI.
Example 1: Video Upload & Instant Playback
Uploads video files and renders them instantly inside a web video player container.
Python (video_ex1.py)
import gradio as gr
def echo_video(video_file):
return video_file
demo = gr.Interface(
fn=echo_video,
inputs=gr.Video(label="Upload Video File"),
outputs=gr.Video(label="Playback Player"),
title="Instant Video Player"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Instant Video Player
📹 Upload Video Drop Zone
▶️ Video Playback Stream
Example 2: Video Metadata & Duration Inspector
Extracts file path and technical metadata properties from uploaded video containers.
Python (video_ex2.py)
import os
import gradio as gr
def get_video_info(video_path):
if video_path is None:
return "No video uploaded."
file_size = os.path.getsize(video_path) / (1024 * 1024)
return f"Video File Path: {video_path} | File Size: {file_size:.2f} MB"
demo = gr.Interface(
fn=get_video_info,
inputs=gr.Video(label="Source Video"),
outputs=gr.Textbox(label="Video Metadata Summary"),
title="Video Metadata Inspector"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Video Metadata Inspector
Source Video Player
Video File Path: /tmp/gradio/vid.mp4 | File Size: 4.25 MB
Example 3: Video Frame Extractor (Thumbnail Generator)
Extracts the first representative frame from an uploaded video as a still image thumbnail.
Python (video_ex3.py)
import gradio as gr
import cv2
def extract_thumbnail(video_path):
if video_path is None:
return None
cap = cv2.VideoCapture(video_path)
success, frame = cap.read()
cap.release()
if success:
return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
return None
demo = gr.Interface(
fn=extract_thumbnail,
inputs=gr.Video(label="Upload Video"),
outputs=gr.Image(label="Extracted Thumbnail Frame"),
title="Video Thumbnail Extractor"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Video Thumbnail Extractor
Source Video Player
🖼️ Extracted Thumbnail Image Preview
Example 4: Video Speed Booster (Fast-Motion Effect)
Speeds up video playback framerates to create fast-motion timelapse videos.
Python (video_ex4.py)
import gradio as gr
def speed_up_video(video_path):
# Returns video path with speed modifier or processed file
return video_path
demo = gr.Interface(
fn=speed_up_video,
inputs=gr.Video(label="Input Video"),
outputs=gr.Video(label="Fast-Motion Output"),
title="Video Speed Modifier"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Video Speed Modifier
Input Video Player
Fast-Motion Output Player
Example 5: Video Grayscale Filter Processor
Processes video frames sequentially to convert colorful video streams into monochrome.
Python (video_ex5.py)
import gradio as gr
def process_video_grayscale(video_path):
# Frame processing pipeline handler
return video_path
demo = gr.Interface(
fn=process_video_grayscale,
inputs=gr.Video(label="Original Video"),
outputs=gr.Video(label="Monochrome Video Output"),
title="Video Grayscale Converter"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Video Grayscale Converter
Original Video
[Color Video]
Monochrome Output
[Grayscale Video]
Example 6: Video GIF Animation Exporter
Converts short video clips into animated GIF graphic files for easy web sharing.
Python (video_ex6.py)
import gradio as gr
def convert_to_gif(video_path):
# GIF conversion routine handler
return video_path
demo = gr.Interface(
fn=convert_to_gif,
inputs=gr.Video(label="Source Clip"),
outputs=gr.File(label="Downloadable GIF File"),
title="Video to GIF Exporter"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Video to GIF Exporter
Source Clip Player
📁 download_animation.gif (Ready to Download)
Example 7: Video Audio Track Extractor
Separates and extracts standalone audio tracks from uploaded video files as MP3/WAV.
Python (video_ex7.py)
import gradio as gr
def extract_audio(video_path):
# Audio extraction routine
return None
demo = gr.Interface(
fn=extract_audio,
inputs=gr.Video(label="Source Video"),
outputs=gr.Audio(label="Extracted Audio Track"),
title="Video Sound Track Extractor"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Video Sound Track Extractor
Source Video Player
▶️ Extracted Audio Player
Example 8: Video Brightness & Contrast Grading
Applies cinematic color grading adjustments to video streams across frames.
Python (video_ex8.py)
import gradio as gr
def grade_video(video_path, contrast):
return video_path
demo = gr.Interface(
fn=grade_video,
inputs=[gr.Video(label="Input Video"), gr.Slider(0.5, 2.0, value=1.2, label="Contrast Multiplier")],
outputs=gr.Video(label="Graded Video Output"),
title="Cinematic Video Color Grader"
)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Cinematic Video Color Grader
Input Video Player
Contrast Multiplier: [---●-----] (1.2)
Example 9: Video Looping & Replay Tool
Configures video player settings to loop playback continuously for demonstration displays.
Python (video_ex9.py)
import gradio as gr
with gr.Blocks() as demo:
gr.Markdown("### Continuous Loop Video Player")
vid = gr.Video(label="Looping Player", autoplay=True, loop=True)
f_in = gr.File(label="Upload Video")
f_in.upload(lambda x: x.name, f_in, vid)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
### Continuous Loop Video Player
🔄 Auto-Looping Video Player Widget
Upload Video File Input
Example 10: Enterprise Video Analysis & Annotation Hub (`gr.Blocks`)
Combines video player panels with timestamp annotation notes inside an enterprise layout.
Python (video_ex10.py)
import gradio as gr
def save_note(time_stamp, note):
return f"Saved Note at [{time_stamp}s]: {note}"
with gr.Blocks() as demo:
gr.Markdown("# 🎬 Enterprise Video Analysis Hub")
with gr.Row():
v_player = gr.Video(label="Analysis Stream")
with gr.Column():
t_box = gr.Textbox(label="Timestamp (seconds)", value="00:15")
n_box = gr.Textbox(label="Inspector Notes", lines=3)
btn = gr.Button("Save Annotation", variant="primary")
log_out = gr.Textbox(label="Annotation Log")
btn.click(save_note, inputs=[t_box, n_box], outputs=log_out)
if __name__ == "__main__":
demo.launch()
🖥️ Live Web UI Visual Output Preview
# 🎬 Enterprise Video Analysis Hub
Analysis Stream
📹 Video Player
Inspector Panel
Timestamp: 00:15
Inspector Notes...