Kvikontent commited on
Commit
dbcf8ab
1 Parent(s): 8897375

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -58
app.py CHANGED
@@ -1,59 +1,72 @@
1
  import gradio as gr
2
- from moviepy.editor import *
3
- from PIL import ImageFont, ImageDraw, Image
4
-
5
- def edit_video(video_file, name, author, font, filter):
6
- # Load the video
7
- clip = VideoFileClip(video_file)
8
-
9
- # Apply the chosen filter
10
- clip = eval(f"clip.{filter}()")
11
-
12
- # Load the font based on user input
13
- font_path = f"{font}.ttf"
14
- font_size = clip.size[0] // 10
15
- font = ImageFont.truetype(font_path, font_size)
16
-
17
- # Create an image with the author text
18
- author_text = f"Author: {author}"
19
- author_image = Image.new('RGB', (clip.w // 2, clip.h // 8), (0, 0, 0))
20
- author_draw = ImageDraw.Draw(author_image)
21
- author_draw.text((10, 10), author_text, font=font, fill=(255, 255, 255))
22
-
23
- # Create an image with the bigger text
24
- bigger_text = f"Author: {author}"
25
- bigger_image = Image.new('RGB', (clip.w // 2, clip.h // 4), (0, 0, 0))
26
- bigger_draw = ImageDraw.Draw(bigger_image)
27
- bigger_draw.text((10, 10), bigger_text, font=font, fill=(255, 255, 255))
28
-
29
- # Create a composite video with the texts
30
- text_clip1 = ImageClip(author_image).set_duration(4).set_position(("right", "bottom"))
31
- text_clip2 = ImageClip(bigger_image).set_duration(4).set_position(("right", "bottom"))
32
- composite_clip = CompositeVideoClip([clip, text_clip1, text_clip2])
33
-
34
- # Save the edited video
35
- output_file = f"{name}_edited.mp4"
36
- composite_clip.write_videofile(output_file, codec='libx264')
37
-
38
- return output_file
39
-
40
- def video_editor(name, author, font, filter, video_file):
41
- edited_video = edit_video(video_file, name, author, font, filter)
42
- return edited_video
43
-
44
- # Define the inputs and outputs for Gradio app
45
- inputs = [
46
- gr.Textbox(label="Video Name"),
47
- gr.Textbox(label="Author Name"),
48
- gr.Radio(["Arial", "Barbie", "MrBeast", "OptiAgle"], label="Font"),
49
- gr.Radio(["blur", "rotate", "resize", "mirror"], label="Filter"),
50
- gr.inputs.File(label="Video File", type="file")
51
- ]
52
-
53
- outputs = gr.outputs.File(label="Edited Video")
54
-
55
- # Create a Gradio app interface
56
- grapp = gr.Interface(fn=video_editor, inputs=inputs, outputs=outputs, capture_session=True)
57
-
58
- # Run the Gradio app
59
- grapp.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
3
+ from moviepy.video.fx.all import speedx, rotate, multiply_brightness, colorx
4
+ from moviepy.video.fx.all import lum_contrast
5
+
6
+ def video_editor(input_video, start_time, end_time, playback_speed, create_gif,
7
+ rotation_degrees, watermark_text, brightness_factor,
8
+ contrast, saturation):
9
+
10
+ # Load video
11
+ clip = VideoFileClip(input_video).subclip(start_time, end_time)
12
+
13
+ # Change playback speed if applicable
14
+ if playback_speed != 1:
15
+ clip = speedx(clip, factor=playback_speed)
16
+
17
+ # Rotate the video if applicable
18
+ if rotation_degrees != 0:
19
+ clip = rotate(clip, rotation_degrees)
20
+
21
+ # Adjust brightness if applicable
22
+ if brightness_factor != 1:
23
+ clip = multiply_brightness(clip, brightness_factor)
24
+
25
+ # Adjust contrast if applicable
26
+ if contrast != 1:
27
+ clip = lum_contrast(clip, contrast=contrast - 1)
28
+
29
+ # Adjust color saturation if applicable
30
+ if saturation != 1:
31
+ clip = colorx(clip, factor=saturation)
32
+
33
+ # Add text watermark if applicable
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
+ # Define output paths
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(source="upload", type="file", label="Input Video"),
56
+ gr.Textbox(value="00:00:00", label="Start Time (HH:MM:SS)"),
57
+ gr.Textbox(value="00:00:10", label="End Time (HH:MM:SS)"),
58
+ gr.Slider(minimum=0.25, maximum=4.0, value=1, step=0.25, label="Playback Speed"),
59
+ gr.Checkbox(label="Create GIF instead of video", value=False),
60
+ gr.Slider(minimum=0, maximum=360, value=0, step=90, label="Rotation (Degrees)"),
61
+ gr.Textbox(value="", label="Watermark Text"),
62
+ gr.Slider(minimum=0.5, maximum=2.0, value=1, step=0.1, label="Brightness Factor"),
63
+ gr.Slider(minimum=0.1, maximum=2.0, value=1, step=0.1, label="Contrast"),
64
+ gr.Slider(minimum=0, maximum=2.0, value=1, step=0.1, label="Saturation")
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
+ iface.launch()