kijeoung commited on
Commit
4c59373
Β·
verified Β·
1 Parent(s): 8af36c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
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()