Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import moviepy.editor as mp
|
3 |
+
import os
|
4 |
+
|
5 |
+
def process_video(video, start_time, end_time, resolution, fps, speed, loop):
|
6 |
+
try:
|
7 |
+
# Load video
|
8 |
+
clip = mp.VideoFileClip(video.name)
|
9 |
+
|
10 |
+
# Get video info
|
11 |
+
duration = clip.duration
|
12 |
+
width, height = clip.size
|
13 |
+
info = {
|
14 |
+
"duration": duration,
|
15 |
+
"resolution": f"{width}x{height}"
|
16 |
+
}
|
17 |
+
|
18 |
+
# Cut clip based on input times
|
19 |
+
start_time = float(start_time) if start_time else 0
|
20 |
+
end_time = float(end_time) if end_time else duration
|
21 |
+
clip = clip.subclip(start_time, end_time)
|
22 |
+
|
23 |
+
# Create thumbnails
|
24 |
+
start_thumbnail = clip.get_frame(start_time) if start_time < duration else None
|
25 |
+
end_thumbnail = clip.get_frame(end_time) if end_time <= duration else None
|
26 |
+
|
27 |
+
# Adjust resolution, fps, speed, and loop count
|
28 |
+
if resolution:
|
29 |
+
width, height = map(int, resolution.split('x'))
|
30 |
+
clip = clip.resize((width, height))
|
31 |
+
|
32 |
+
if fps:
|
33 |
+
clip = clip.set_fps(int(fps))
|
34 |
+
|
35 |
+
if speed:
|
36 |
+
clip = clip.fx(mp.vfx.speedx, float(speed))
|
37 |
+
|
38 |
+
gif_path = f"output.gif"
|
39 |
+
clip.write_gif(gif_path, loop=int(loop))
|
40 |
+
|
41 |
+
return start_thumbnail, end_thumbnail, gif_path, info
|
42 |
+
except Exception as e:
|
43 |
+
return None, None, None, {"error": str(e)}
|
44 |
+
|
45 |
+
def main():
|
46 |
+
with gr.Blocks() as app:
|
47 |
+
gr.Markdown("## λμμμ GIFλ‘ λ³ννλ λꡬ")
|
48 |
+
|
49 |
+
with gr.Row():
|
50 |
+
video_input = gr.Video(label="λμμ μ
λ‘λ")
|
51 |
+
video_info = gr.JSON(label="μμ μ 보")
|
52 |
+
|
53 |
+
with gr.Row():
|
54 |
+
start_time = gr.Textbox(label="μμ μκ° (μ΄)", placeholder="μ: 0")
|
55 |
+
end_time = gr.Textbox(label="λ μκ° (μ΄)", placeholder="μ: 10")
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
resolution = gr.Textbox(label="ν΄μλ (μ: 640x480)", placeholder="μ: 640x480")
|
59 |
+
fps = gr.Slider(label="νλ μ μλ (FPS)", minimum=1, maximum=60, step=1, value=24)
|
60 |
+
speed = gr.Slider(label="μ¬μ μλ (λ°°μ)", minimum=0.1, maximum=3.0, step=0.1, value=1.0)
|
61 |
+
loop = gr.Slider(label="GIF λ°λ³΅ νμ", minimum=0, maximum=10, step=1, value=0)
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
start_thumbnail = gr.Image(label="μμ μΈλ€μΌ")
|
65 |
+
end_thumbnail = gr.Image(label="λ μΈλ€μΌ")
|
66 |
+
|
67 |
+
gif_preview = gr.Image(label="GIF 미리보기")
|
68 |
+
gif_download = gr.File(label="GIF λ€μ΄λ‘λ")
|
69 |
+
|
70 |
+
generate_button = gr.Button("GIF μμ±")
|
71 |
+
|
72 |
+
def generate(video, start, end, res, frame_rate, playback_speed, repeat):
|
73 |
+
return process_video(video, start, end, res, frame_rate, playback_speed, repeat)
|
74 |
+
|
75 |
+
generate_button.click(
|
76 |
+
generate,
|
77 |
+
inputs=[video_input, start_time, end_time, resolution, fps, speed, loop],
|
78 |
+
outputs=[start_thumbnail, end_thumbnail, gif_preview, gif_download]
|
79 |
+
)
|
80 |
+
|
81 |
+
return app
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
app = main()
|
85 |
+
app.launch()
|