Update app.py
Browse files
app.py
CHANGED
@@ -41,10 +41,43 @@ def generate_thumbnail(video_clip, time_point):
|
|
41 |
thumbnail_img = Image.fromarray(frame)
|
42 |
return thumbnail_img
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
def process_video(video,
|
45 |
start_time_str,
|
46 |
end_time_str,
|
47 |
-
|
48 |
frame_rate_factor,
|
49 |
speed_factor,
|
50 |
repeat_count):
|
@@ -87,12 +120,11 @@ def process_video(video,
|
|
87 |
add_log("[LOG 6] ์์ ์๋ฅด๊ธฐ ์์
์งํ...")
|
88 |
clip = input_video.subclip(start_sec, end_sec)
|
89 |
|
90 |
-
# ํด์๋
|
91 |
-
|
92 |
-
|
93 |
-
clip = clip.resize(resolution_factor)
|
94 |
|
95 |
-
# ์ฌ์์๋ ์กฐ์ : speedx๋ฅผ ์ ์ฉํ๋ฉด clip.duration
|
96 |
if abs(speed_factor - 1.0) > 1e-3:
|
97 |
add_log(f"[LOG 8] ์ฌ์์๋ {speed_factor}๋ฐฐ๋ก ์กฐ์ ์ค...")
|
98 |
clip = clip.fx(mp.vfx.speedx, speed_factor)
|
@@ -110,7 +142,6 @@ def process_video(video,
|
|
110 |
add_log("[LOG 11] GIF ์์ฑ ์ค...")
|
111 |
output_filename = f"temp_{uuid.uuid4().hex}.gif"
|
112 |
try:
|
113 |
-
# loop=0์ด๋ฉด ๋ฌดํ๋ฐ๋ณต, ๊ทธ ์ธ์๋ ํด๋น ๊ฐ ์ฌ์ฉ
|
114 |
loop_param = 0 if int(repeat_count) == 0 else int(repeat_count)
|
115 |
final_clip.write_gif(output_filename, fps=target_fps, program='ffmpeg', loop=loop_param)
|
116 |
add_log("[LOG 12] GIF ์์ฑ ์๋ฃ! ํ์ผ๋ช
: " + output_filename)
|
@@ -165,9 +196,14 @@ with gr.Blocks() as demo:
|
|
165 |
# ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
|
166 |
start_thumb_output = gr.Image(label="์์ ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
167 |
end_thumb_output = gr.Image(label="์ข
๋ฃ ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
168 |
-
#
|
169 |
-
|
170 |
-
|
|
|
|
|
|
|
|
|
|
|
171 |
speed_slider = gr.Slider(label="์ฌ์ ์๋ ์กฐ์ (0.5 ~ 2.0)", minimum=0.5, maximum=2.0, step=0.1, value=1.0)
|
172 |
repeat_slider = gr.Slider(label="GIF ๋ฐ๋ณต ํ์ (0: ๋ฌดํ๋ฐ๋ณต, 1~10: ๋ฐ๋ณต ํ์)", minimum=0, maximum=10, step=1, value=0)
|
173 |
# GIF ์์ฑ ๋ฒํผ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
@@ -190,14 +226,15 @@ with gr.Blocks() as demo:
|
|
190 |
# GIF ์์ฑ ๋ฒํผ ์ด๋ฒคํธ
|
191 |
generate_button.click(fn=process_video,
|
192 |
inputs=[video_input, start_time, end_time,
|
193 |
-
|
194 |
outputs=[gif_preview_output, download_output, logs_output])
|
195 |
|
196 |
gr.Markdown(
|
197 |
"### [์ฌ์ฉ ๊ฐ์ด๋]\n"
|
198 |
"1. ๋์์์ ์
๋ก๋ํ์ธ์.\n"
|
199 |
-
"2. ์์/๋ ์๊ฐ์
|
200 |
-
"3.
|
|
|
201 |
" - ๋ฐ๋ณต ํ์: 0์ ์ ํํ๋ฉด ๋ฌดํ๋ฐ๋ณต, 1~10์ ํด๋น ํ์๋งํผ ๋ฐ๋ณต๋ฉ๋๋ค.\n"
|
202 |
"4. ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ ๋ค์ด๋ก๋ ๋งํฌ๋ฅผ ํตํด GIF๋ฅผ ํ์ธํ ์ ์์ต๋๋ค."
|
203 |
)
|
|
|
41 |
thumbnail_img = Image.fromarray(frame)
|
42 |
return thumbnail_img
|
43 |
|
44 |
+
def adjust_aspect_ratio(clip, option):
|
45 |
+
"""
|
46 |
+
์์ ํด๋ฆฝ์ ์ ํํ ํ๋ซํผ์ ๊ถ์ฅ ๋น์จ๋ก ์ค์ ํฌ๋กญํ์ฌ ์กฐ์ ํฉ๋๋ค.
|
47 |
+
"""
|
48 |
+
if option == "์๋ณธ ์ ์ง":
|
49 |
+
return clip
|
50 |
+
|
51 |
+
if option == "์ ํ๋ธ (16:9)":
|
52 |
+
target_ratio = 16 / 9
|
53 |
+
elif option == "์ผ์ธ /๋ฆด์ค (9:16)":
|
54 |
+
target_ratio = 9 / 16
|
55 |
+
elif option == "์ ์ฌ๊ฐํ (1:1)":
|
56 |
+
target_ratio = 1.0
|
57 |
+
elif option == "์ธ์คํ๊ทธ๋จ (4:5)":
|
58 |
+
target_ratio = 4 / 5
|
59 |
+
elif option == "ํด๋์ (4:3)":
|
60 |
+
target_ratio = 4 / 3
|
61 |
+
else:
|
62 |
+
return clip
|
63 |
+
|
64 |
+
width, height = clip.size
|
65 |
+
current_ratio = width / height
|
66 |
+
|
67 |
+
# ์์์ด ๋๋ฌด ๋๋ค๋ฉด ๊ฐ๋ก๋ฅผ, ๋๋ฌด ๋๋ค๋ฉด ์ธ๋ก๋ฅผ ํฌ๋กญํฉ๋๋ค.
|
68 |
+
if current_ratio > target_ratio:
|
69 |
+
new_width = int(height * target_ratio)
|
70 |
+
new_height = height
|
71 |
+
else:
|
72 |
+
new_width = width
|
73 |
+
new_height = int(width / target_ratio)
|
74 |
+
|
75 |
+
return clip.crop(x_center=width / 2, y_center=height / 2, width=new_width, height=new_height)
|
76 |
+
|
77 |
def process_video(video,
|
78 |
start_time_str,
|
79 |
end_time_str,
|
80 |
+
platform_option, # ํด์๋ ๋น์จ ์ต์
์ ํ
|
81 |
frame_rate_factor,
|
82 |
speed_factor,
|
83 |
repeat_count):
|
|
|
120 |
add_log("[LOG 6] ์์ ์๋ฅด๊ธฐ ์์
์งํ...")
|
121 |
clip = input_video.subclip(start_sec, end_sec)
|
122 |
|
123 |
+
# ํ๋ซํผ๋ณ ํด์๋(๋น์จ) ์กฐ์
|
124 |
+
add_log(f"[LOG 7] ํ๋ซํผ ๋น์จ ์กฐ์ : {platform_option}")
|
125 |
+
clip = adjust_aspect_ratio(clip, platform_option)
|
|
|
126 |
|
127 |
+
# ์ฌ์์๋ ์กฐ์ : speedx๋ฅผ ์ ์ฉํ๋ฉด clip.duration์ ์ค์ด๋ค์ง๋ง fps๋ ๊ทธ๋๋ก ์ ์ง๋จ.
|
128 |
if abs(speed_factor - 1.0) > 1e-3:
|
129 |
add_log(f"[LOG 8] ์ฌ์์๋ {speed_factor}๋ฐฐ๋ก ์กฐ์ ์ค...")
|
130 |
clip = clip.fx(mp.vfx.speedx, speed_factor)
|
|
|
142 |
add_log("[LOG 11] GIF ์์ฑ ์ค...")
|
143 |
output_filename = f"temp_{uuid.uuid4().hex}.gif"
|
144 |
try:
|
|
|
145 |
loop_param = 0 if int(repeat_count) == 0 else int(repeat_count)
|
146 |
final_clip.write_gif(output_filename, fps=target_fps, program='ffmpeg', loop=loop_param)
|
147 |
add_log("[LOG 12] GIF ์์ฑ ์๋ฃ! ํ์ผ๋ช
: " + output_filename)
|
|
|
196 |
# ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
|
197 |
start_thumb_output = gr.Image(label="์์ ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
198 |
end_thumb_output = gr.Image(label="์ข
๋ฃ ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
|
199 |
+
# ํ๋ซํผ๋ณ ํด์๋(๋น์จ) ์ ํ
|
200 |
+
platform_option = gr.Dropdown(
|
201 |
+
label="ํด์๋/๋น์จ ์ ํ",
|
202 |
+
choices=["์๋ณธ ์ ์ง", "์ ํ๋ธ (16:9)", "์ผ์ธ /๋ฆด์ค (9:16)", "์ ์ฌ๊ฐํ (1:1)", "์ธ์คํ๊ทธ๋จ (4:5)", "ํด๋์ (4:3)"],
|
203 |
+
value="์๋ณธ ์ ์ง"
|
204 |
+
)
|
205 |
+
# ํ๋ ์ ๋ ์ดํธ, ์ฌ์ ์๋, ๋ฐ๋ณต ํ์ ์กฐ์
|
206 |
+
frame_rate_slider = gr.Slider(label="ํ๋ ์ ๋ ์ดํธ ๋ฐฐ์จ ์กฐ์ (0.1 ~ 2.0)", minimum=0.1, maximum=2.0, step=0.1, value=1.0)
|
207 |
speed_slider = gr.Slider(label="์ฌ์ ์๋ ์กฐ์ (0.5 ~ 2.0)", minimum=0.5, maximum=2.0, step=0.1, value=1.0)
|
208 |
repeat_slider = gr.Slider(label="GIF ๋ฐ๋ณต ํ์ (0: ๋ฌดํ๋ฐ๋ณต, 1~10: ๋ฐ๋ณต ํ์)", minimum=0, maximum=10, step=1, value=0)
|
209 |
# GIF ์์ฑ ๋ฒํผ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
|
|
|
226 |
# GIF ์์ฑ ๋ฒํผ ์ด๋ฒคํธ
|
227 |
generate_button.click(fn=process_video,
|
228 |
inputs=[video_input, start_time, end_time,
|
229 |
+
platform_option, frame_rate_slider, speed_slider, repeat_slider],
|
230 |
outputs=[gif_preview_output, download_output, logs_output])
|
231 |
|
232 |
gr.Markdown(
|
233 |
"### [์ฌ์ฉ ๊ฐ์ด๋]\n"
|
234 |
"1. ๋์์์ ์
๋ก๋ํ์ธ์.\n"
|
235 |
+
"2. ์์/๋ ์๊ฐ์ ์
๋ ฅํ์ธ์.\n"
|
236 |
+
"3. ํ๋ซํผ๋ณ ๊ถ์ฅ ํด์๋/๋น์จ(์๋ณธ ์ ์ง, ์ ํ๋ธ, ์ผ์ธ /๋ฆด์ค, ์ ์ฌ๊ฐํ, ์ธ์คํ๊ทธ๋จ, ํด๋์)์ ์ ํํ๊ณ ,\n"
|
237 |
+
" ํ๋ ์ ๋ ์ดํธ, ์ฌ์ ์๋, ๋ฐ๋ณต ํ์๋ฅผ ์กฐ์ ํ ํ `GIF ์์ฑํ๊ธฐ` ๋ฒํผ์ ๋๋ฅด๋ฉด GIF๊ฐ ์์ฑ๋ฉ๋๋ค.\n"
|
238 |
" - ๋ฐ๋ณต ํ์: 0์ ์ ํํ๋ฉด ๋ฌดํ๋ฐ๋ณต, 1~10์ ํด๋น ํ์๋งํผ ๋ฐ๋ณต๋ฉ๋๋ค.\n"
|
239 |
"4. ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ ๋ค์ด๋ก๋ ๋งํฌ๋ฅผ ํตํด GIF๋ฅผ ํ์ธํ ์ ์์ต๋๋ค."
|
240 |
)
|