Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,29 @@
|
|
1 |
import os
|
2 |
import tempfile
|
3 |
-
|
4 |
-
# κΌ "moviepy.editor"λ‘λΆν° μν¬νΈν νμλ μκ³ , μλμ²λΌ μ 체 μν¬νΈ ν νμν κ°μ²΄λ₯Ό μ°Έμ‘°ν΄λ λ©λλ€.
|
5 |
-
# μ¬κΈ°μλ λ¬Έμ λ κ²μ΄ μμ§λ§, νΉμ νκ²½μ λ°λΌ κ²½λ‘ importκ° κΉ¨μ§λ€λ©΄ μλ λ°©μμ μλν΄λ³Ό μλ μμ΅λλ€.
|
6 |
-
import moviepy.editor as me
|
7 |
-
|
8 |
-
# Gradio
|
9 |
import gradio as gr
|
10 |
|
11 |
-
#
|
12 |
-
from PIL import Image
|
13 |
import imageio
|
|
|
|
|
|
|
14 |
|
15 |
-
# ------------------------------------------------
|
16 |
-
# μ μ λ‘κ·Έ 리μ€νΈ
|
17 |
-
# ------------------------------------------------
|
18 |
LOGS = []
|
19 |
|
20 |
def log_step(message: str):
|
21 |
LOGS.append(message)
|
22 |
-
print(message)
|
23 |
return "\n".join(LOGS)
|
24 |
|
25 |
-
# ------------------------------------------------
|
26 |
-
# μκ° λ¬Έμμ΄ -> μ΄ λ¨μ(float)
|
27 |
-
# ------------------------------------------------
|
28 |
def parse_time_str(time_str: str):
|
29 |
try:
|
30 |
parts = time_str.strip().split(":")
|
31 |
if len(parts) == 3:
|
32 |
h, m, s = parts
|
33 |
-
|
34 |
-
return h * 3600 + m * 60 + s
|
35 |
elif len(parts) == 2:
|
36 |
m, s = parts
|
37 |
-
|
38 |
-
return m * 60 + s
|
39 |
elif len(parts) == 1:
|
40 |
return float(parts[0])
|
41 |
else:
|
@@ -43,174 +31,132 @@ def parse_time_str(time_str: str):
|
|
43 |
except:
|
44 |
return 0.0
|
45 |
|
46 |
-
# ------------------------------------------------
|
47 |
-
# 1) λμμ μ
λ‘λ μ²λ¦¬
|
48 |
-
# ------------------------------------------------
|
49 |
def upload_video(video_file):
|
50 |
if video_file is None:
|
51 |
return None, "μ
λ‘λλ νμΌμ΄ μμ΅λλ€.", None, log_step("[λ¨κ³ 1] μ
λ‘λλ νμΌμ΄ μμ΅λλ€.")
|
52 |
-
|
53 |
temp_dir = tempfile.mkdtemp()
|
54 |
temp_video_path = os.path.join(temp_dir, video_file.name)
|
55 |
with open(temp_video_path, "wb") as f:
|
56 |
f.write(video_file.read())
|
57 |
-
|
58 |
try:
|
59 |
clip = me.VideoFileClip(temp_video_path)
|
60 |
duration_sec = clip.duration
|
61 |
msg = f"μ¬μ μκ°: {duration_sec:.2f}μ΄"
|
62 |
-
|
63 |
-
|
64 |
-
# λ°ν: (λΉλμ€ κ²½λ‘, μ¬μμκ° λ©μμ§, duration, λ‘κ·Έ)
|
65 |
-
return temp_video_path, msg, duration_sec, log_output
|
66 |
except Exception as e:
|
67 |
-
|
68 |
-
return None, f"μμ μ²λ¦¬ μλ¬: {e}", None,
|
69 |
|
70 |
-
# ------------------------------------------------
|
71 |
-
# 5) μΈλ€μΌ μμ± (μμ/λ μκ°)
|
72 |
-
# ------------------------------------------------
|
73 |
def generate_thumbnails(video_path, start_time_str, end_time_str, log_history):
|
74 |
log_step("[λ¨κ³ 4] μμ/λ μκ° μ
λ ₯ μλ£")
|
75 |
start_s = parse_time_str(start_time_str)
|
76 |
end_s = parse_time_str(end_time_str)
|
77 |
-
|
78 |
if not video_path:
|
79 |
-
return None, None, log_step("[λ¨κ³ 5] λΉλμ€ λ―Έμ
λ‘λ β μΈλ€μΌ
|
80 |
|
81 |
try:
|
82 |
clip = me.VideoFileClip(video_path)
|
|
|
83 |
if start_s < 0: start_s = 0
|
84 |
-
if end_s >
|
85 |
if start_s > end_s: start_s, end_s = end_s, start_s
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
thumb1_img = Image.fromarray(
|
91 |
-
thumb2_img = Image.fromarray(
|
92 |
-
|
93 |
-
|
94 |
-
return thumb1_img, thumb2_img,
|
95 |
except Exception as e:
|
96 |
-
|
97 |
-
return None, None,
|
98 |
|
99 |
-
# ------------------------------------------------
|
100 |
-
# 10~12) GIF μμ±
|
101 |
-
# ------------------------------------------------
|
102 |
def create_gif(video_path, start_time_str, end_time_str, resolution, fps, speed, loop_count, log_history):
|
103 |
if not video_path:
|
104 |
return None, None, log_step("[λ¨κ³ 10] GIF μμ± μ€ν¨: λΉλμ€κ° μ
λ‘λλμ§ μμ.")
|
105 |
|
106 |
start_s = parse_time_str(start_time_str)
|
107 |
end_s = parse_time_str(end_time_str)
|
108 |
-
|
109 |
try:
|
110 |
clip = me.VideoFileClip(video_path)
|
|
|
111 |
if start_s < 0: start_s = 0
|
112 |
-
if end_s >
|
113 |
if start_s > end_s: start_s, end_s = end_s, start_s
|
114 |
-
|
115 |
-
# 1) μλΈν΄λ¦½
|
116 |
subclip = clip.subclip(start_s, end_s)
|
117 |
-
|
118 |
-
# 2) μλ μ‘°μ
|
119 |
if speed != 1.0:
|
120 |
subclip = subclip.fx(me.vfx.speedx, speed)
|
121 |
-
|
122 |
-
#
|
|
|
123 |
if resolution < 100:
|
124 |
-
w, h = subclip.size
|
125 |
scale = resolution / 100.0
|
126 |
-
subclip = subclip.resize((int(w*scale), int(h*scale)))
|
127 |
-
|
128 |
-
#
|
129 |
fps = int(fps) if fps > 0 else None
|
130 |
-
|
131 |
-
#
|
132 |
-
loop = loop_count
|
133 |
-
|
134 |
temp_dir = tempfile.mkdtemp()
|
135 |
gif_path = os.path.join(temp_dir, "output.gif")
|
136 |
-
|
137 |
subclip.write_gif(gif_path, fps=fps, loop=loop)
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
preview_img = Image.open(gif_path)
|
144 |
-
|
145 |
-
return preview_img, gif_path, "\n".join([log1, log2, log3])
|
146 |
except Exception as e:
|
147 |
-
|
148 |
-
return None, None,
|
149 |
|
150 |
-
# ------------------------------------------------
|
151 |
-
# Gradio μΈν°νμ΄μ€
|
152 |
-
# ------------------------------------------------
|
153 |
def build_interface():
|
154 |
with gr.Blocks() as demo:
|
155 |
gr.Markdown("## λμμμ GIFλ‘ λ³ννκΈ°")
|
156 |
-
|
157 |
-
# (1) λΉλμ€ μ
λ‘λ
|
158 |
video_upload = gr.File(label="λΉλμ€ μ
λ‘λ", file_types=["video"])
|
159 |
-
|
160 |
-
# (2) μ
λ‘λλ μμ 미리보기
|
161 |
video_player = gr.Video(label="μ
λ‘λλ μμ 미리보기")
|
162 |
-
|
163 |
-
|
164 |
-
video_info = gr.Textbox(label="μ
λ‘λλ μμ μ 보", interactive=False)
|
165 |
-
|
166 |
-
# λ΄λΆ μν
|
167 |
video_path_state = gr.State()
|
168 |
video_duration_state = gr.State()
|
169 |
-
|
170 |
-
# λ‘κ·Έ
|
171 |
log_box = gr.Textbox(label="λ‘κ·Έ", interactive=False, lines=10)
|
172 |
-
|
173 |
-
# (4) μμ/λ μκ°
|
174 |
with gr.Row():
|
175 |
-
start_time = gr.Textbox(label="μμ μκ° (HH:MM:SS
|
176 |
-
end_time = gr.Textbox(label="λ μκ° (HH:MM:SS
|
177 |
-
|
178 |
-
# (5) μΈλ€μΌ
|
179 |
with gr.Row():
|
180 |
thumb1 = gr.Image(label="μμ μ§μ μΈλ€μΌ")
|
181 |
thumb2 = gr.Image(label="λ μ§μ μΈλ€μΌ")
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
# (9) GIF λ°λ³΅ νμ (0=무ν, ~10)
|
193 |
-
loop_slider = gr.Slider(0, 10, value=0, step=1, label="GIF λ°λ³΅ νμ(0=무ν)")
|
194 |
-
|
195 |
-
# (10) GIF μμ± λ²νΌ
|
196 |
-
generate_gif_btn = gr.Button("GIF μμ±")
|
197 |
-
|
198 |
-
# (11,12) κ²°κ³Ό
|
199 |
-
gif_preview = gr.Image(label="GIF 미리보기")
|
200 |
gif_download = gr.File(label="GIF λ€μ΄λ‘λ(ν΄λ¦νμ¬ μ μ₯)")
|
201 |
-
|
202 |
-
#
|
203 |
-
# μ΄λ²€νΈ μ°κ²°
|
204 |
-
# -----------------------------
|
205 |
-
|
206 |
-
# μ
λ‘λ -> (λΉλμ€κ²½λ‘, μ 보, κΈΈμ΄, λ‘κ·Έ)
|
207 |
video_upload.change(
|
208 |
fn=upload_video,
|
209 |
inputs=video_upload,
|
210 |
outputs=[video_path_state, video_info, video_duration_state, log_box]
|
211 |
)
|
212 |
-
|
213 |
-
# μμ/λ μκ° -> μΈλ€μΌ
|
214 |
start_time.change(
|
215 |
fn=generate_thumbnails,
|
216 |
inputs=[video_path_state, start_time, end_time, log_box],
|
@@ -221,23 +167,12 @@ def build_interface():
|
|
221 |
inputs=[video_path_state, start_time, end_time, log_box],
|
222 |
outputs=[thumb1, thumb2, log_box]
|
223 |
)
|
224 |
-
|
225 |
-
# GIF μμ± λ²νΌ ν΄λ¦
|
226 |
generate_gif_btn.click(
|
227 |
fn=create_gif,
|
228 |
-
inputs=[
|
229 |
-
video_path_state,
|
230 |
-
start_time,
|
231 |
-
end_time,
|
232 |
-
resolution_slider,
|
233 |
-
fps_slider,
|
234 |
-
speed_slider,
|
235 |
-
loop_slider,
|
236 |
-
log_box
|
237 |
-
],
|
238 |
outputs=[gif_preview, gif_download, log_box]
|
239 |
)
|
240 |
-
|
241 |
return demo
|
242 |
|
243 |
if __name__ == "__main__":
|
|
|
1 |
import os
|
2 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import gradio as gr
|
4 |
|
5 |
+
import numpy as np # (νμνλ€λ©΄)
|
|
|
6 |
import imageio
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
import moviepy.editor as me # MoviePy μ 체 μν¬νΈ
|
10 |
|
|
|
|
|
|
|
11 |
LOGS = []
|
12 |
|
13 |
def log_step(message: str):
|
14 |
LOGS.append(message)
|
15 |
+
print(message)
|
16 |
return "\n".join(LOGS)
|
17 |
|
|
|
|
|
|
|
18 |
def parse_time_str(time_str: str):
|
19 |
try:
|
20 |
parts = time_str.strip().split(":")
|
21 |
if len(parts) == 3:
|
22 |
h, m, s = parts
|
23 |
+
return int(h)*3600 + int(m)*60 + float(s)
|
|
|
24 |
elif len(parts) == 2:
|
25 |
m, s = parts
|
26 |
+
return int(m)*60 + float(s)
|
|
|
27 |
elif len(parts) == 1:
|
28 |
return float(parts[0])
|
29 |
else:
|
|
|
31 |
except:
|
32 |
return 0.0
|
33 |
|
|
|
|
|
|
|
34 |
def upload_video(video_file):
|
35 |
if video_file is None:
|
36 |
return None, "μ
λ‘λλ νμΌμ΄ μμ΅λλ€.", None, log_step("[λ¨κ³ 1] μ
λ‘λλ νμΌμ΄ μμ΅λλ€.")
|
|
|
37 |
temp_dir = tempfile.mkdtemp()
|
38 |
temp_video_path = os.path.join(temp_dir, video_file.name)
|
39 |
with open(temp_video_path, "wb") as f:
|
40 |
f.write(video_file.read())
|
|
|
41 |
try:
|
42 |
clip = me.VideoFileClip(temp_video_path)
|
43 |
duration_sec = clip.duration
|
44 |
msg = f"μ¬μ μκ°: {duration_sec:.2f}μ΄"
|
45 |
+
log_out = log_step("[λ¨κ³ 1] λΉλμ€ μ
λ‘λ λ° μ¬μ νμΈ μλ£")
|
46 |
+
return temp_video_path, msg, duration_sec, log_out
|
|
|
|
|
47 |
except Exception as e:
|
48 |
+
err_log = log_step(f"[λ¨κ³ 1] μ
λ‘λ μλ¬: {e}")
|
49 |
+
return None, f"μμ μ²λ¦¬ μλ¬: {e}", None, err_log
|
50 |
|
|
|
|
|
|
|
51 |
def generate_thumbnails(video_path, start_time_str, end_time_str, log_history):
|
52 |
log_step("[λ¨κ³ 4] μμ/λ μκ° μ
λ ₯ μλ£")
|
53 |
start_s = parse_time_str(start_time_str)
|
54 |
end_s = parse_time_str(end_time_str)
|
55 |
+
|
56 |
if not video_path:
|
57 |
+
return None, None, log_step("[λ¨κ³ 5] λΉλμ€ λ―Έμ
λ‘λ β μΈλ€μΌ λΆκ°")
|
58 |
|
59 |
try:
|
60 |
clip = me.VideoFileClip(video_path)
|
61 |
+
duration = clip.duration
|
62 |
if start_s < 0: start_s = 0
|
63 |
+
if end_s > duration: end_s = duration
|
64 |
if start_s > end_s: start_s, end_s = end_s, start_s
|
65 |
+
|
66 |
+
thumb1 = clip.get_frame(start_s)
|
67 |
+
thumb2 = clip.get_frame(end_s)
|
68 |
+
|
69 |
+
thumb1_img = Image.fromarray(thumb1)
|
70 |
+
thumb2_img = Image.fromarray(thumb2)
|
71 |
+
|
72 |
+
log_out = log_step("[λ¨κ³ 5] μΈλ€μΌ μμ± μλ£")
|
73 |
+
return thumb1_img, thumb2_img, log_out
|
74 |
except Exception as e:
|
75 |
+
err_log = log_step(f"[λ¨κ³ 5] μΈλ€μΌ μμ± μλ¬: {e}")
|
76 |
+
return None, None, err_log
|
77 |
|
|
|
|
|
|
|
78 |
def create_gif(video_path, start_time_str, end_time_str, resolution, fps, speed, loop_count, log_history):
|
79 |
if not video_path:
|
80 |
return None, None, log_step("[λ¨κ³ 10] GIF μμ± μ€ν¨: λΉλμ€κ° μ
λ‘λλμ§ μμ.")
|
81 |
|
82 |
start_s = parse_time_str(start_time_str)
|
83 |
end_s = parse_time_str(end_time_str)
|
84 |
+
|
85 |
try:
|
86 |
clip = me.VideoFileClip(video_path)
|
87 |
+
duration = clip.duration
|
88 |
if start_s < 0: start_s = 0
|
89 |
+
if end_s > duration: end_s = duration
|
90 |
if start_s > end_s: start_s, end_s = end_s, start_s
|
91 |
+
|
|
|
92 |
subclip = clip.subclip(start_s, end_s)
|
|
|
|
|
93 |
if speed != 1.0:
|
94 |
subclip = subclip.fx(me.vfx.speedx, speed)
|
95 |
+
|
96 |
+
# ν΄μλ μ‘°μ (κΈ°λ³Έ 100%)
|
97 |
+
w, h = subclip.size
|
98 |
if resolution < 100:
|
|
|
99 |
scale = resolution / 100.0
|
100 |
+
subclip = subclip.resize((int(w * scale), int(h * scale)))
|
101 |
+
|
102 |
+
# FPS
|
103 |
fps = int(fps) if fps > 0 else None
|
104 |
+
|
105 |
+
# λ°λ³΅ νμ
|
106 |
+
loop = loop_count # 0=무ν
|
107 |
+
|
108 |
temp_dir = tempfile.mkdtemp()
|
109 |
gif_path = os.path.join(temp_dir, "output.gif")
|
110 |
+
|
111 |
subclip.write_gif(gif_path, fps=fps, loop=loop)
|
112 |
+
|
113 |
+
log_out1 = log_step("[λ¨κ³ 10] 'GIF μμ±' λ²νΌ ν΄λ¦λ¨")
|
114 |
+
log_out2 = log_step("[λ¨κ³ 11] GIF 미리보기 μμ± μλ£")
|
115 |
+
log_out3 = log_step("[λ¨κ³ 12] GIF λ€μ΄λ‘λ μ€λΉ μλ£")
|
116 |
+
|
117 |
preview_img = Image.open(gif_path)
|
118 |
+
return preview_img, gif_path, "\n".join([log_out1, log_out2, log_out3])
|
|
|
119 |
except Exception as e:
|
120 |
+
err_log = log_step(f"[λ¨κ³ 10~12] GIF μμ± μλ¬: {e}")
|
121 |
+
return None, None, err_log
|
122 |
|
|
|
|
|
|
|
123 |
def build_interface():
|
124 |
with gr.Blocks() as demo:
|
125 |
gr.Markdown("## λμμμ GIFλ‘ λ³ννκΈ°")
|
126 |
+
|
|
|
127 |
video_upload = gr.File(label="λΉλμ€ μ
λ‘λ", file_types=["video"])
|
|
|
|
|
128 |
video_player = gr.Video(label="μ
λ‘λλ μμ 미리보기")
|
129 |
+
video_info = gr.Textbox(label="μ
λ‘λλ μμ μ 보", interactive=False)
|
130 |
+
|
|
|
|
|
|
|
131 |
video_path_state = gr.State()
|
132 |
video_duration_state = gr.State()
|
133 |
+
|
|
|
134 |
log_box = gr.Textbox(label="λ‘κ·Έ", interactive=False, lines=10)
|
135 |
+
|
|
|
136 |
with gr.Row():
|
137 |
+
start_time = gr.Textbox(label="μμ μκ° (HH:MM:SS/ MM:SS)", value="0:00")
|
138 |
+
end_time = gr.Textbox(label="λ μκ° (HH:MM:SS/ MM:SS)", value="0:10")
|
139 |
+
|
|
|
140 |
with gr.Row():
|
141 |
thumb1 = gr.Image(label="μμ μ§μ μΈλ€μΌ")
|
142 |
thumb2 = gr.Image(label="λ μ§μ μΈλ€μΌ")
|
143 |
+
|
144 |
+
resolution_slider = gr.Slider(1, 100, 100, step=1, label="ν΄μλ(%)")
|
145 |
+
fps_slider = gr.Slider(1, 60, 30, step=1, label="FPS")
|
146 |
+
speed_slider = gr.Slider(0.1, 3.0, 1.0, step=0.1, label="μ¬μ μλ(λ°°)")
|
147 |
+
loop_slider = gr.Slider(0, 10, 0, step=1, label="GIF λ°λ³΅ νμ (0=무ν)")
|
148 |
+
|
149 |
+
generate_gif_btn = gr.Button("GIF μμ±")
|
150 |
+
|
151 |
+
gif_preview = gr.Image(label="GIF 미리보기")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
gif_download = gr.File(label="GIF λ€μ΄λ‘λ(ν΄λ¦νμ¬ μ μ₯)")
|
153 |
+
|
154 |
+
# μ΄λ²€νΈ λ°μΈλ©
|
|
|
|
|
|
|
|
|
155 |
video_upload.change(
|
156 |
fn=upload_video,
|
157 |
inputs=video_upload,
|
158 |
outputs=[video_path_state, video_info, video_duration_state, log_box]
|
159 |
)
|
|
|
|
|
160 |
start_time.change(
|
161 |
fn=generate_thumbnails,
|
162 |
inputs=[video_path_state, start_time, end_time, log_box],
|
|
|
167 |
inputs=[video_path_state, start_time, end_time, log_box],
|
168 |
outputs=[thumb1, thumb2, log_box]
|
169 |
)
|
|
|
|
|
170 |
generate_gif_btn.click(
|
171 |
fn=create_gif,
|
172 |
+
inputs=[video_path_state, start_time, end_time, resolution_slider, fps_slider, speed_slider, loop_slider, log_box],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
outputs=[gif_preview, gif_download, log_box]
|
174 |
)
|
175 |
+
|
176 |
return demo
|
177 |
|
178 |
if __name__ == "__main__":
|