File size: 3,260 Bytes
1b05743
b762f57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dbcf8ab
 
b762f57
 
dbcf8ab
 
b762f57
dbcf8ab
 
 
b762f57
dbcf8ab
 
 
b762f57
dbcf8ab
b762f57
dbcf8ab
b762f57
dbcf8ab
b762f57
 
 
dbcf8ab
b762f57
dbcf8ab
b762f57
dbcf8ab
 
 
 
 
b762f57
dbcf8ab
 
 
b762f57
dbcf8ab
 
 
 
 
 
 
b762f57
dbcf8ab
 
 
b762f57
 
 
 
 
 
 
 
 
 
dbcf8ab
b762f57
dbcf8ab
 
b762f57
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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.inputs.Video(label="Input Video"),
        gr.inputs.Textbox(default="00:00:00", label="Start Time (HH:MM:SS)"),
        gr.inputs.Textbox(default="00:00:10", label="End Time (HH:MM:SS)"),
        gr.inputs.Slider(minimum=0.25, maximum=4.0, step=0.25, default=1, label="Playback Speed"),
        gr.inputs.Checkbox(default=False, label="Create GIF instead of a video"),
        gr.inputs.Slider(minimum=0, maximum=360, step=90, default=0, label="Rotation (Degrees)"),
        gr.inputs.Textbox(default="", label="Watermark Text"),
        gr.inputs.Slider(minimum=0.5, maximum=3.0, step=0.1, default=1, label="Brightness Factor"),
        gr.inputs.Slider(minimum=0.1, maximum=2.0, step=0.1, default=1, label="Contrast"),
        gr.inputs.Slider(minimum=0, maximum=2.0, step=0.1, default=1, label="Saturation")
    ],
    outputs=gr.outputs.Video(label="Output Video/GIF"),
)

# Launch the application
iface.launch()