Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip, vfx, fx
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Function to adjust audio pitch
|
6 |
+
def adjust_audio_pitch(input_video_path, pitch_factor):
|
7 |
+
video = VideoFileClip(input_video_path)
|
8 |
+
audio = video.audio
|
9 |
+
if audio is None:
|
10 |
+
return input_video_path # No audio to process
|
11 |
+
|
12 |
+
# Adjust pitch (this is a simplified approach, more sophisticated methods may be needed)
|
13 |
+
new_audio = audio.fx(vfx.pitch, pitch_factor)
|
14 |
+
new_video = video.set_audio(new_audio)
|
15 |
+
output_path = "temp_audio_adjusted.mp4"
|
16 |
+
new_video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
17 |
+
return output_path
|
18 |
+
|
19 |
+
# Function to apply visual effects
|
20 |
+
def apply_visual_effects(input_video_path, glitch_effect, mirror_effect, black_and_white, sepia, blur, brightness):
|
21 |
+
video = VideoFileClip(input_video_path)
|
22 |
+
|
23 |
+
if glitch_effect:
|
24 |
+
# Simple glitch effect (you can enhance this by adding more complex glitches)
|
25 |
+
def glitch_clip(clip):
|
26 |
+
frames = []
|
27 |
+
for frame in clip.iter_frames(fps=clip.fps):
|
28 |
+
if np.random.rand() < 0.1: # Randomly flip some frames horizontally
|
29 |
+
frame = np.fliplr(frame)
|
30 |
+
frames.append(frame)
|
31 |
+
return clip.set_data(np.array(frames))
|
32 |
+
|
33 |
+
video = video.fl_image(lambda img: glitch_clip(video).get_frame(0))
|
34 |
+
|
35 |
+
if mirror_effect:
|
36 |
+
video = video.fx(vfx.mirror_x) # Mirror reflection along X-axis
|
37 |
+
|
38 |
+
if black_and_white:
|
39 |
+
video = video.fx(vfx.blackwhite)
|
40 |
+
|
41 |
+
if sepia:
|
42 |
+
video = video.fl_image(lambda img: fx.color.sepia(img, intensity=1.0))
|
43 |
+
|
44 |
+
if blur:
|
45 |
+
video = video.fx(vfx.blur, blur_radius=5)
|
46 |
+
|
47 |
+
if brightness != 0:
|
48 |
+
video = video.fx(vfx.colorx, brightness)
|
49 |
+
|
50 |
+
output_path = "temp_visual_effects.mp4"
|
51 |
+
video.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
52 |
+
return output_path
|
53 |
+
|
54 |
+
# Main function to handle all modifications
|
55 |
+
def edit_video(input_video, pitch_factor, glitch_effect, mirror_effect, black_and_white, sepia, blur, brightness):
|
56 |
+
temp_video = input_video.name if isinstance(input_video, gr.File) else input_video
|
57 |
+
|
58 |
+
# Step 1: Adjust audio pitch
|
59 |
+
temp_video = adjust_audio_pitch(temp_video, pitch_factor)
|
60 |
+
|
61 |
+
# Step 2: Apply visual effects
|
62 |
+
final_video = apply_visual_effects(temp_video, glitch_effect, mirror_effect, black_and_white, sepia, blur, brightness)
|
63 |
+
|
64 |
+
return final_video
|
65 |
+
|
66 |
+
# Create the Gradio Interface with a blue theme
|
67 |
+
with gr.Blocks(theme="default-blue") as demo:
|
68 |
+
gr.Markdown(
|
69 |
+
"""
|
70 |
+
# Neon Video Editor
|
71 |
+
A sleek and modern video editor with advanced features.
|
72 |
+
"""
|
73 |
+
)
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
with gr.Column():
|
77 |
+
input_video = gr.File(label="Upload Video", file_types=["video"])
|
78 |
+
pitch_factor = gr.Slider(label="Audio Pitch Adjustment", minimum=-20, maximum=20, value=0, step=0.1)
|
79 |
+
glitch_effect = gr.Checkbox(label="Apply Glitch Effect")
|
80 |
+
mirror_effect = gr.Checkbox(label="Apply Mirror Reflection")
|
81 |
+
black_and_white = gr.Checkbox(label="Apply Black & White Effect")
|
82 |
+
sepia = gr.Checkbox(label="Apply Sepia Effect")
|
83 |
+
blur = gr.Checkbox(label="Apply Blur Effect")
|
84 |
+
brightness = gr.Slider(label="Adjust Brightness", minimum=-1, maximum=1, value=0, step=0.1)
|
85 |
+
apply_button = gr.Button("Apply Changes")
|
86 |
+
|
87 |
+
with gr.Column():
|
88 |
+
output_video = gr.Video(label="Edited Video Preview")
|
89 |
+
|
90 |
+
apply_button.click(
|
91 |
+
fn=edit_video,
|
92 |
+
inputs=[input_video, pitch_factor, glitch_effect, mirror_effect, black_and_white, sepia, blur, brightness],
|
93 |
+
outputs=output_video
|
94 |
+
)
|
95 |
+
|
96 |
+
demo.launch()
|