Spaces:
Sleeping
Sleeping
import gradio as gr | |
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, vfx | |
from moviepy.video.fx.all import blackwhite, colorx # Import specific effects from fx.all | |
from moviepy.video.tools.drawing import color_gradient # For additional effects if needed | |
import numpy as np | |
# Function to adjust audio pitch | |
def adjust_audio_pitch(input_video_path, pitch_factor): | |
video = VideoFileClip(input_video_path) | |
audio = video.audio | |
if audio is None: | |
return input_video_path # No audio to process | |
# Adjust pitch (this is a simplified approach, more sophisticated methods may be needed) | |
new_audio = audio.fx(vfx.pitch, pitch_factor) | |
new_video = video.set_audio(new_audio) | |
output_path = "temp_audio_adjusted.mp4" | |
new_video.write_videofile(output_path, codec='libx264', audio_codec='aac') | |
return output_path | |
# Function to apply visual effects | |
def apply_visual_effects(input_video_path, glitch_effect, mirror_effect, black_and_white, sepia, blur, brightness): | |
video = VideoFileClip(input_video_path) | |
if glitch_effect: | |
# Simple glitch effect (you can enhance this by adding more complex glitches) | |
def glitch_clip(clip): | |
frames = [] | |
for frame in clip.iter_frames(fps=clip.fps): | |
if np.random.rand() < 0.1: # Randomly flip some frames horizontally | |
frame = np.fliplr(frame) | |
frames.append(frame) | |
return clip.set_data(np.array(frames)) | |
video = video.fl_image(lambda img: glitch_clip(video).get_frame(0)) | |
if mirror_effect: | |
video = video.fx(vfx.mirror_x) # Mirror reflection along X-axis | |
if black_and_white: | |
video = video.fx(blackwhite) | |
if sepia: | |
# Apply sepia effect using a custom function | |
def sepia_effect(img): | |
sepia_filter = np.array([[0.393, 0.769, 0.189], | |
[0.349, 0.686, 0.168], | |
[0.272, 0.534, 0.131]]) | |
sepia_img = np.dot(img[...,:3], sepia_filter.T) | |
sepia_img = np.clip(sepia_img, 0, 255).astype(np.uint8) | |
return sepia_img | |
video = video.fl_image(sepia_effect) | |
if blur: | |
video = video.fx(vfx.blur, blur_radius=5) | |
if brightness != 0: | |
video = video.fx(colorx, brightness) | |
output_path = "temp_visual_effects.mp4" | |
video.write_videofile(output_path, codec='libx264', audio_codec='aac') | |
return output_path | |
# Main function to handle all modifications | |
def edit_video(input_video, pitch_factor, glitch_effect, mirror_effect, black_and_white, sepia, blur, brightness): | |
temp_video = input_video.name if isinstance(input_video, gr.File) else input_video | |
# Step 1: Adjust audio pitch | |
temp_video = adjust_audio_pitch(temp_video, pitch_factor) | |
# Step 2: Apply visual effects | |
final_video = apply_visual_effects(temp_video, glitch_effect, mirror_effect, black_and_white, sepia, blur, brightness) | |
return final_video | |
# Create the Gradio Interface with a blue theme | |
with gr.Blocks(theme="default-blue") as demo: | |
gr.Markdown( | |
""" | |
# Neon Video Editor | |
A sleek and modern video editor with advanced features. | |
""" | |
) | |
with gr.Row(): | |
with gr.Column(): | |
input_video = gr.File(label="Upload Video", file_types=["video"]) | |
pitch_factor = gr.Slider(label="Audio Pitch Adjustment", minimum=-20, maximum=20, value=0, step=0.1) | |
glitch_effect = gr.Checkbox(label="Apply Glitch Effect") | |
mirror_effect = gr.Checkbox(label="Apply Mirror Reflection") | |
black_and_white = gr.Checkbox(label="Apply Black & White Effect") | |
sepia = gr.Checkbox(label="Apply Sepia Effect") | |
blur = gr.Checkbox(label="Apply Blur Effect") | |
brightness = gr.Slider(label="Adjust Brightness", minimum=-1, maximum=1, value=0, step=0.1) | |
apply_button = gr.Button("Apply Changes") | |
with gr.Column(): | |
output_video = gr.Video(label="Edited Video Preview") | |
apply_button.click( | |
fn=edit_video, | |
inputs=[input_video, pitch_factor, glitch_effect, mirror_effect, black_and_white, sepia, blur, brightness], | |
outputs=output_video | |
) | |
demo.launch() |