Update app.py
Browse files
app.py
CHANGED
@@ -1,99 +1,112 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
-
import
|
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 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from moviepy.editor import VideoFileClip
|
3 |
import os
|
4 |
+
from PIL import Image
|
5 |
+
import tempfile
|
6 |
+
import logging
|
7 |
+
|
8 |
+
# Configure logging
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
|
11 |
+
# Function to get video info
|
12 |
+
def get_video_info(video):
|
13 |
+
logging.info("Getting video info")
|
14 |
+
if not video:
|
15 |
+
return "No video uploaded."
|
16 |
+
clip = VideoFileClip(video)
|
17 |
+
duration = clip.duration
|
18 |
+
clip.close()
|
19 |
+
return f"Duration: {duration:.2f} seconds"
|
20 |
+
|
21 |
+
# Function to generate thumbnails
|
22 |
+
def generate-thumbnails(video, start_time, end_time):
|
23 |
+
logging.info("Generating thumbnails")
|
24 |
+
if not video:
|
25 |
+
return "No video uploaded."
|
26 |
+
clip = VideoFileClip(video)
|
27 |
+
if start_time > clip.duration:
|
28 |
+
start_img = None
|
29 |
+
else:
|
30 |
+
start_frame = clip.get_frame(start_time)
|
31 |
+
start_img = Image.fromarray(start_frame)
|
32 |
+
if end_time > clip.duration:
|
33 |
+
end_img = None
|
34 |
+
else:
|
35 |
+
end_frame = clip.get_frame(end_time)
|
36 |
+
end_img = Image.fromarray(end_frame)
|
37 |
+
clip.close()
|
38 |
+
return start_img, end_img
|
39 |
+
|
40 |
+
# Function to generate GIF
|
41 |
+
def generate_gif(video, start_time, end_time, resolution, frame_rate, playback_speed, loop_count):
|
42 |
+
logging.info("Generating GIF")
|
43 |
+
if not video:
|
44 |
+
return None, None
|
45 |
+
clip = VideoFileClip(video)
|
46 |
+
# Apply start and end times
|
47 |
+
if start_time > clip.duration:
|
48 |
+
start_time = 0
|
49 |
+
if end_time > clip.duration:
|
50 |
+
end_time = clip.duration
|
51 |
+
subclip = clip.subclip(start_time, end_time)
|
52 |
+
# Apply playback speed
|
53 |
+
subclip = subclip.set_fps(subclip.fps * playback_speed)
|
54 |
+
# Resize if resolution is changed
|
55 |
+
if resolution != subclip.size[0]:
|
56 |
+
ratio = resolution / subclip.size[0]
|
57 |
+
new_height = int(subclip.size[1] * ratio)
|
58 |
+
subclip = subclip.resize((resolution, new_height))
|
59 |
+
# Write GIF
|
60 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".gif") as temp:
|
61 |
+
gif_path = temp.name
|
62 |
+
subclip.write_gif(gif_path, fps=frame_rate, loop=loop_count)
|
63 |
+
# Read GIF for preview
|
64 |
+
preview = Image.open(gif_path)
|
65 |
+
return preview, gif_path
|
66 |
+
|
67 |
+
# Gradio interface
|
68 |
+
with gr.Blocks() as demo:
|
69 |
+
gr.Markdown("<h1>Video to GIF Converter</h1>")
|
70 |
+
|
71 |
+
# Upload video
|
72 |
+
video_input = gr.Video(label="Upload Video")
|
73 |
+
|
74 |
+
# Button to get video info
|
75 |
+
with gr.Row():
|
76 |
+
get_info_button = gr.Button("Get Video Info")
|
77 |
+
video_info = gr.Textbox(label="Video Information")
|
78 |
+
|
79 |
+
# Display uploaded video
|
80 |
+
video_output = gr.Video(label="Uploaded Video")
|
81 |
+
|
82 |
+
# Inputs for start and end times
|
83 |
+
start_time = gr.Number(label="Start Time (seconds)", value=0)
|
84 |
+
end_time = gr.Number(label="End Time (seconds)", value=0)
|
85 |
+
|
86 |
+
# Thumbnails
|
87 |
+
with gr.Row():
|
88 |
+
start_thumbnail = gr.Image(label="Start Thumbnail")
|
89 |
+
end_thumbnail = gr.Image(label="End Thumbnail")
|
90 |
+
|
91 |
+
# Sliders
|
92 |
+
resolution = gr.Slider(100, 1920, step=100, value=1920, label="Resolution")
|
93 |
+
frame_rate = gr.Slider(1, 30, step=1, value=10, label="Frame Rate")
|
94 |
+
playback_speed = gr.Slider(0.1, 2.0, step=0.1, value=1.0, label="Playback Speed")
|
95 |
+
loop_count = gr.Slider(0, 10, step=1, value=0, label="GIF Loop Count (0 for infinite)")
|
96 |
+
|
97 |
+
# GIF Generation button
|
98 |
+
generate_button = gr.Button("GIF μμ±")
|
99 |
+
|
100 |
+
# GIF Preview and Download
|
101 |
+
gif_preview = gr.Image(label="GIF Preview")
|
102 |
+
gif_download = gr.Download(label="Download GIF")
|
103 |
+
|
104 |
+
# Button functions
|
105 |
+
get_info_button.click(get_video_info, inputs=video_input, outputs=video_info)
|
106 |
+
video_input.change(lambda x: x, inputs=video_input, outputs=video_output)
|
107 |
+
video_input.change(generate-thumbnails, inputs=[video_input, start_time, end_time], outputs=[start_thumbnail, end_thumbnail])
|
108 |
+
generate_button.click(generate_gif,
|
109 |
+
inputs=[video_input, start_time, end_time, resolution, frame_rate, playback_speed, loop_count],
|
110 |
+
outputs=[gif_preview, gif_download])
|
111 |
+
|
112 |
+
demo.launch()
|