Spaces:
Runtime error
Runtime error
import gradio as gr | |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, clips_array | |
from moviepy.video.fx.all import speedx, rotate, lum_contrast | |
import numpy as np | |
def adjust_brightness(clip, brightness_factor): | |
"""Adjusts the brightness of a video clip.""" | |
return clip.fl_image(lambda image: np.clip(image * brightness_factor, 0, 255).astype(np.uint8)) | |
def adjust_saturation(clip, saturation_factor): | |
"""Adjust saturation of the video clip.""" | |
def fl_image(image): | |
# Convert to HSV and scale the saturation channel | |
hsv = colorsys.rgb_to_hsv(*np.array(image) / 255.0) | |
hsv[:, :, 1] *= saturation_factor | |
hsv[:, :, 1] = np.clip(hsv[:, :, 1], 0, 1) | |
return (np.array(colorsys.hsv_to_rgb(*hsv)) * 255).astype(np.uint8) | |
return clip.fl_image(fl_image) | |
def video_editor(input_video, start_time, end_time, playback_speed, create_gif, | |
rotation_degrees, watermark_text, brightness_factor, contrast, saturation): | |
# Load and sub-clip video | |
clip = VideoFileClip(input_video).subclip(start_time, end_time) | |
# Change playback speed | |
if playback_speed != 1: | |
clip = speedx(clip, factor=playback_speed) | |
# Rotate the video | |
if rotation_degrees != 0: | |
clip = rotate(clip, rotation_degrees) | |
# Adjust brightness | |
if brightness_factor != 1: | |
clip = adjust_brightness(clip, brightness_factor) | |
# Adjust contrast | |
if contrast != 1: | |
clip = lum_contrast(clip, contrast=contrast) | |
# Adjust saturation | |
if saturation != 1: | |
clip = adjust_saturation(clip, saturation) | |
# Add text watermark | |
if watermark_text: | |
txt_clip = TextClip(watermark_text, fontsize=24, color='white', bg_color='black') | |
txt_clip = txt_clip.set_position(('right', 'bottom')).set_duration(clip.duration) | |
clip = CompositeVideoClip([clip, txt_clip]) | |
# Output paths | |
output_video_path = "edited_video.mp4" | |
output_gif_path = "edited_video.gif" | |
# Write file | |
if not create_gif: | |
clip.write_videofile(output_video_path, codec="libx264", audio_codec="aac") | |
return output_video_path | |
else: | |
clip.write_gif(output_gif_path) | |
return output_gif_path | |
# Gradio interface setup | |
iface = gr.Interface( | |
fn=video_editor, | |
inputs=[ | |
gr.Video(label="Input Video"), | |
gr.Textbox(default="00:00:00", label="Start Time (HH:MM:SS)"), | |
gr.Textbox(default="00:00:10", label="End Time (HH:MM:SS)"), | |
gr.Slider(minimum=0.25, maximum=4.0, step=0.25, default=1, label="Playback Speed"), | |
gr.Checkbox(default=False, label="Create GIF instead of a video"), | |
gr.Slider(minimum=0, maximum=360, step=90, default=0, label="Rotation (Degrees)"), | |
gr.Textbox(default="", label="Watermark Text"), | |
gr.Slider(minimum=0.5, maximum=3.0, step=0.1, default=1, label="Brightness Factor"), | |
gr.Slider(minimum=0.1, maximum=2.0, step=0.1, default=1, label="Contrast"), | |
gr.Slider(minimum=0, maximum=2.0, step=0.1, default=1, label="Saturation") | |
], | |
outputs=gr.Video(label="Output Video/GIF"), | |
) | |
# Launch the application | |
iface.launch() | |