Spaces:
Runtime error
Runtime error
Kvikontent
commited on
Commit
•
b762f57
1
Parent(s):
7124f75
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,84 @@
|
|
1 |
import gradio as gr
|
2 |
-
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
3 |
-
from moviepy.video.fx.all import speedx, rotate,
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def video_editor(input_video, start_time, end_time, playback_speed, create_gif,
|
7 |
-
rotation_degrees, watermark_text, brightness_factor,
|
8 |
-
|
9 |
-
|
10 |
-
# Load video
|
11 |
clip = VideoFileClip(input_video).subclip(start_time, end_time)
|
12 |
|
13 |
-
# Change playback speed
|
14 |
if playback_speed != 1:
|
15 |
clip = speedx(clip, factor=playback_speed)
|
16 |
|
17 |
-
# Rotate the video
|
18 |
if rotation_degrees != 0:
|
19 |
clip = rotate(clip, rotation_degrees)
|
20 |
|
21 |
-
# Adjust brightness
|
22 |
if brightness_factor != 1:
|
23 |
-
clip =
|
24 |
|
25 |
-
# Adjust contrast
|
26 |
if contrast != 1:
|
27 |
-
clip = lum_contrast(clip, contrast=contrast
|
28 |
-
|
29 |
-
# Adjust
|
30 |
if saturation != 1:
|
31 |
-
clip =
|
32 |
|
33 |
-
# Add text watermark
|
34 |
if watermark_text:
|
35 |
txt_clip = TextClip(watermark_text, fontsize=24, color='white', bg_color='black')
|
36 |
txt_clip = txt_clip.set_position(('right', 'bottom')).set_duration(clip.duration)
|
37 |
clip = CompositeVideoClip([clip, txt_clip])
|
38 |
|
39 |
-
#
|
40 |
output_video_path = "edited_video.mp4"
|
41 |
output_gif_path = "edited_video.gif"
|
42 |
|
|
|
43 |
if not create_gif:
|
44 |
-
# Write video file with the edits
|
45 |
clip.write_videofile(output_video_path, codec="libx264", audio_codec="aac")
|
46 |
return output_video_path
|
47 |
else:
|
48 |
-
# Write GIF file with the edits
|
49 |
clip.write_gif(output_gif_path)
|
50 |
return output_gif_path
|
51 |
|
|
|
52 |
iface = gr.Interface(
|
53 |
fn=video_editor,
|
54 |
inputs=[
|
55 |
-
gr.Video(
|
56 |
-
gr.Textbox(
|
57 |
-
gr.Textbox(
|
58 |
-
gr.Slider(minimum=0.25, maximum=4.0,
|
59 |
-
gr.Checkbox(label="Create GIF instead of video"
|
60 |
-
gr.Slider(minimum=0, maximum=360,
|
61 |
-
gr.Textbox(
|
62 |
-
gr.Slider(minimum=0.5, maximum=
|
63 |
-
gr.Slider(minimum=0.1, maximum=2.0,
|
64 |
-
gr.Slider(minimum=0, maximum=2.0,
|
65 |
],
|
66 |
-
outputs=gr.Video(label="Output Video/GIF"),
|
67 |
-
examples=[
|
68 |
-
["sample_video.mp4", "00:00:02", "00:00:05", 1, False, 0, "", 1, 1, 1],
|
69 |
-
]
|
70 |
)
|
71 |
|
72 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip, clips_array
|
3 |
+
from moviepy.video.fx.all import speedx, rotate, lum_contrast
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
def adjust_brightness(clip, brightness_factor):
|
7 |
+
"""Adjusts the brightness of a video clip."""
|
8 |
+
return clip.fl_image(lambda image: np.clip(image * brightness_factor, 0, 255).astype(np.uint8))
|
9 |
+
|
10 |
+
def adjust_saturation(clip, saturation_factor):
|
11 |
+
"""Adjust saturation of the video clip."""
|
12 |
+
def fl_image(image):
|
13 |
+
# Convert to HSV and scale the saturation channel
|
14 |
+
hsv = colorsys.rgb_to_hsv(*np.array(image) / 255.0)
|
15 |
+
hsv[:, :, 1] *= saturation_factor
|
16 |
+
hsv[:, :, 1] = np.clip(hsv[:, :, 1], 0, 1)
|
17 |
+
return (np.array(colorsys.hsv_to_rgb(*hsv)) * 255).astype(np.uint8)
|
18 |
+
|
19 |
+
return clip.fl_image(fl_image)
|
20 |
|
21 |
def video_editor(input_video, start_time, end_time, playback_speed, create_gif,
|
22 |
+
rotation_degrees, watermark_text, brightness_factor, contrast, saturation):
|
23 |
+
# Load and sub-clip video
|
|
|
|
|
24 |
clip = VideoFileClip(input_video).subclip(start_time, end_time)
|
25 |
|
26 |
+
# Change playback speed
|
27 |
if playback_speed != 1:
|
28 |
clip = speedx(clip, factor=playback_speed)
|
29 |
|
30 |
+
# Rotate the video
|
31 |
if rotation_degrees != 0:
|
32 |
clip = rotate(clip, rotation_degrees)
|
33 |
|
34 |
+
# Adjust brightness
|
35 |
if brightness_factor != 1:
|
36 |
+
clip = adjust_brightness(clip, brightness_factor)
|
37 |
|
38 |
+
# Adjust contrast
|
39 |
if contrast != 1:
|
40 |
+
clip = lum_contrast(clip, contrast=contrast)
|
41 |
+
|
42 |
+
# Adjust saturation
|
43 |
if saturation != 1:
|
44 |
+
clip = adjust_saturation(clip, saturation)
|
45 |
|
46 |
+
# Add text watermark
|
47 |
if watermark_text:
|
48 |
txt_clip = TextClip(watermark_text, fontsize=24, color='white', bg_color='black')
|
49 |
txt_clip = txt_clip.set_position(('right', 'bottom')).set_duration(clip.duration)
|
50 |
clip = CompositeVideoClip([clip, txt_clip])
|
51 |
|
52 |
+
# Output paths
|
53 |
output_video_path = "edited_video.mp4"
|
54 |
output_gif_path = "edited_video.gif"
|
55 |
|
56 |
+
# Write file
|
57 |
if not create_gif:
|
|
|
58 |
clip.write_videofile(output_video_path, codec="libx264", audio_codec="aac")
|
59 |
return output_video_path
|
60 |
else:
|
|
|
61 |
clip.write_gif(output_gif_path)
|
62 |
return output_gif_path
|
63 |
|
64 |
+
# Gradio interface setup
|
65 |
iface = gr.Interface(
|
66 |
fn=video_editor,
|
67 |
inputs=[
|
68 |
+
gr.inputs.Video(label="Input Video"),
|
69 |
+
gr.inputs.Textbox(default="00:00:00", label="Start Time (HH:MM:SS)"),
|
70 |
+
gr.inputs.Textbox(default="00:00:10", label="End Time (HH:MM:SS)"),
|
71 |
+
gr.inputs.Slider(minimum=0.25, maximum=4.0, step=0.25, default=1, label="Playback Speed"),
|
72 |
+
gr.inputs.Checkbox(default=False, label="Create GIF instead of a video"),
|
73 |
+
gr.inputs.Slider(minimum=0, maximum=360, step=90, default=0, label="Rotation (Degrees)"),
|
74 |
+
gr.inputs.Textbox(default="", label="Watermark Text"),
|
75 |
+
gr.inputs.Slider(minimum=0.5, maximum=3.0, step=0.1, default=1, label="Brightness Factor"),
|
76 |
+
gr.inputs.Slider(minimum=0.1, maximum=2.0, step=0.1, default=1, label="Contrast"),
|
77 |
+
gr.inputs.Slider(minimum=0, maximum=2.0, step=0.1, default=1, label="Saturation")
|
78 |
],
|
79 |
+
outputs=gr.outputs.Video(label="Output Video/GIF"),
|
|
|
|
|
|
|
80 |
)
|
81 |
|
82 |
+
# Launch the application
|
83 |
+
iface.launch()
|
84 |
+
|