File size: 11,826 Bytes
2d0b48d b2ee4ad 2d0b48d e3952dc 2d0b48d ddf3982 382dbbf ddf3982 382dbbf ddf3982 382dbbf 2d0b48d 3005b66 2d0b48d e3952dc 2d0b48d 382dbbf e3952dc 2d0b48d e3952dc 2d0b48d 382dbbf 2d0b48d ddf3982 2d0b48d 8e9820c ebd5cce 382dbbf 3005b66 382dbbf 2d0b48d 4680eca 3005b66 2d0b48d 8e9820c 2d0b48d e3952dc 8e9820c e3952dc 2d0b48d 8e9820c 3005b66 2d0b48d 3005b66 2d0b48d e3952dc 2d0b48d e3952dc 8e92fe3 e3952dc 2d0b48d e3952dc 2d0b48d 382dbbf 3793744 ddf3982 382dbbf ddf3982 382dbbf ddf3982 382dbbf e3952dc 2d0b48d 4abd773 2d0b48d 382dbbf e3952dc b2ee4ad 382dbbf b2ee4ad 382dbbf b2ee4ad e3952dc 3005b66 ddf3982 382dbbf 3005b66 2d0b48d |
1 2 3 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
import os
import uuid
import moviepy.editor as mp
from PIL import Image
import gradio as gr
# ----------------------------------
# ๊ณตํต์ ์ผ๋ก ์ฌ์ฉํ ๋ก๊ทธ ๋ฆฌ์คํธ
# ----------------------------------
global_logs = []
def add_log(message: str):
"""
๋ก๊ทธ ๋ฉ์์ง๋ฅผ ์ ์ฅํ๋ ํจ์
"""
global_logs.append(message)
print(message)
def parse_time_to_seconds(time_str: str):
"""
์:๋ถ:์ด ํํ์ ๋ฌธ์์ด(์: '00:00:10' ๋๋ '00:01:02')์ ์ด ๋จ์(float)๋ก ๋ณํ
"""
try:
h, m, s = time_str.split(":")
total_seconds = int(h) * 3600 + int(m) * 60 + float(s)
return total_seconds
except Exception:
return 0.0
def generate_thumbnail(video_clip, time_point):
"""
ํน์ ์์ ์ ํ๋ ์์ ์ธ๋ค์ผ๋ก ์์ฑํ์ฌ PIL.Image๋ก ๋ฐํ
"""
try:
frame = video_clip.get_frame(time_point)
thumbnail_img = Image.fromarray(frame)
return thumbnail_img
except Exception as e:
add_log(f"[ERROR] ์ธ๋ค์ผ ์์ฑ ์คํจ: {e}")
frame = video_clip.get_frame(0)
thumbnail_img = Image.fromarray(frame)
return thumbnail_img
def adjust_aspect_ratio(clip, option):
"""
์์ ํด๋ฆฝ์ ์ ํํ ํ๋ซํผ์ ๊ถ์ฅ ๋น์จ๋ก ์ค์ ํฌ๋กญํ์ฌ ์กฐ์ ํฉ๋๋ค.
"""
if option == "์๋ณธ ์ ์ง":
return clip
if option == "์ ํ๋ธ (16:9)":
target_ratio = 16 / 9
elif option == "์ผ์ธ /๋ฆด์ค (9:16)":
target_ratio = 9 / 16
elif option == "์ ์ฌ๊ฐํ (1:1)":
target_ratio = 1.0
elif option == "์ธ์คํ๊ทธ๋จ (4:5)":
target_ratio = 4 / 5
elif option == "ํด๋์ (4:3)":
target_ratio = 4 / 3
else:
return clip
width, height = clip.size
current_ratio = width / height
# ์์์ด ๋๋ฌด ๋๋ค๋ฉด ๊ฐ๋ก๋ฅผ, ๋๋ฌด ๋๋ค๋ฉด ์ธ๋ก๋ฅผ ํฌ๋กญ
if current_ratio > target_ratio:
new_width = int(height * target_ratio)
new_height = height
else:
new_width = width
new_height = int(width / target_ratio)
return clip.crop(x_center=width / 2, y_center=height / 2, width=new_width, height=new_height)
def process_video(video,
start_time_str,
end_time_str,
platform_option, # ํ๋ซํผ๋ณ ๋น์จ ์ ํ (Radio)
frame_rate_factor, # ํ๋ ์ ๋ ์ดํธ ์ถ์ ๋ฐฐ์จ (1.0์ด๋ฉด ์๋ณธ)
speed_factor,
repeat_count,
resolution_scale): # ์ถ๋ ฅ ํด์๋ ์ถ์ ๋น์จ (1.0์ด๋ฉด ์๋ณธ)
"""
๋์์์ GIF๋ก ๋ณํํ๋ ํจ์
"""
global global_logs
global_logs = [] # ํธ์ถ ์๋ง๋ค ๋ก๊ทธ ์ด๊ธฐํ
add_log("[LOG 1] ๋น๋์ค ์
๋ก๋ ๋ฐ ์ฒ๋ฆฌ ์์")
# video ํ๋ผ๋ฏธํฐ์์ ํ์ผ ๊ฒฝ๋ก ์ถ์ถ (Gradio์์ ๋ฐํํ๋ ๊ฐ์ด ๋ฌธ์์ด์ผ ์ ์์)
video_path = video if isinstance(video, str) else video.name
add_log("[LOG 2] ๋น๋์ค ๋ก๋ ์ค...")
try:
input_video = mp.VideoFileClip(video_path)
except Exception as e:
add_log(f"[ERROR] ๋น๋์ค ๋ก๋ ์คํจ: {e}")
return None, None, "\n".join(global_logs)
duration = input_video.duration
add_log(f"[LOG 3] ์
๋ก๋๋ ์์์ ์ฌ์์๊ฐ: {duration:.2f}์ด")
add_log("[LOG 4] ์์/๋ ์๊ฐ ํ์ฑ ์ค...")
start_sec = parse_time_to_seconds(start_time_str)
end_sec = parse_time_to_seconds(end_time_str)
# ์๊ฐ ๋ฒ์ ๋ณด์
if start_sec < 0:
start_sec = 0
if end_sec <= 0 or end_sec > duration:
end_sec = duration
if start_sec >= end_sec:
start_sec = 0
end_sec = duration
add_log(f"[LOG 5] ์ ์ฉ๋ ์์ ์๊ฐ: {start_sec}์ด, ์ข
๋ฃ ์๊ฐ: {end_sec}์ด")
add_log("[LOG 6] ์์ ์๋ฅด๊ธฐ ์์
์งํ...")
clip = input_video.subclip(start_sec, end_sec)
# ํ๋ซํผ๋ณ ํด์๋(๋น์จ) ์กฐ์
add_log(f"[LOG 7] ํ๋ซํผ ๋น์จ ์กฐ์ : {platform_option}")
clip = adjust_aspect_ratio(clip, platform_option)
# ์ถ๋ ฅ ํด์๋ ์ถ์ ์ต์
์ ์ฉ
if abs(resolution_scale - 1.0) > 1e-3:
add_log(f"[LOG 7-1] ์ถ๋ ฅ ํด์๋ ์ถ์: {resolution_scale*100:.0f}%")
clip = clip.resize(resolution_scale)
# ์ฌ์์๋ ์กฐ์ : speedx๋ฅผ ์ ์ฉํ๋ฉด clip.duration์ ์ค์ด๋ค์ง๋ง fps๋ ๊ทธ๋๋ก ์ ์ง๋จ.
if abs(speed_factor - 1.0) > 1e-3:
add_log(f"[LOG 8] ์ฌ์์๋ {speed_factor}๋ฐฐ๋ก ์กฐ์ ์ค...")
clip = clip.fx(mp.vfx.speedx, speed_factor)
# FPS ์กฐ์ : ์ต์ข
์ถ๋ ฅ FPS๋ ํ๋ ์ ๋ ์ดํธ ๋ฐฐ์จ๋ง ๋ฐ์ (speed_factor๋ ์ด๋ฏธ duration์ ๋ฐ์๋จ)
original_fps = clip.fps
target_fps = original_fps * frame_rate_factor
add_log(f"[LOG 9] ์ต์ข
์ถ๋ ฅ FPS: {target_fps:.2f} (์๋ณธ FPS: {original_fps})")
clip = clip.set_fps(target_fps)
# ๋ฐ๋ณต ํ์: 0์ด๋ฉด ๋ฌดํ๋ฐ๋ณต, 1~10์ด๋ฉด ํด๋น ํ์๋งํผ ๋ฐ๋ณต (GIF์ loop ๋ฉํ๋ฐ์ดํฐ ์ฌ์ฉ)
loop_param = 0 if int(repeat_count) == 0 else int(repeat_count)
add_log(f"[LOG 10] GIF ๋ฐ๋ณต ํ์ ์ค์ : {repeat_count} (0์ด๋ฉด ๋ฌดํ๋ฐ๋ณต, ์ ์ฉ๊ฐ: {loop_param})")
add_log("[LOG 11] GIF ์์ฑ ์ค...")
output_filename = f"temp_{uuid.uuid4().hex}.gif"
try:
# MoviePy์ write_gif ๋์ ์ง์ imageio๋ฅผ ์ฌ์ฉํ์ฌ GIF ์์ฑ
import imageio
# ํ๋ ์ ์ถ์ถ
frames = []
for t in range(int(clip.duration * target_fps)):
frame_time = t / target_fps
frame = clip.get_frame(frame_time)
frames.append(frame)
# imageio๋ฅผ ์ฌ์ฉํ์ฌ GIF ์ ์ฅ (loop ํ๋ผ๋ฏธํฐ ์ ์ฉ)
imageio.mimsave(
output_filename,
frames,
fps=target_fps,
loop=loop_param # ์ฌ๊ธฐ์ ๋ฐ๋ณต ํ์ ์ค์
)
add_log("[LOG 12] GIF ์์ฑ ์๋ฃ! ํ์ผ๋ช
: " + output_filename)
except Exception as e:
add_log(f"[ERROR] GIF ์์ฑ ์คํจ: {e}")
return None, None, "\n".join(global_logs)
# ๋ฏธ๋ฆฌ๋ณด๊ธฐ: ์์ฑ๋ GIF ํ์ผ ๊ฒฝ๋ก๋ฅผ ๊ทธ๋๋ก ๋ฐํํ๋ฉด gr.Image์์ ์ ๋๋ฉ์ด์
๋ฏธ๋ฆฌ๋ณด๊ธฐ๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
return output_filename, output_filename, "\n".join(global_logs)
def update_thumbnails(video, start_time_str, end_time_str):
"""
์์/๋ ์ธ๋ค์ผ์ ์
๋ฐ์ดํธํ๋ ํจ์
"""
video_path = video if isinstance(video, str) else video.name
try:
input_video = mp.VideoFileClip(video_path)
except Exception as e:
add_log(f"[ERROR] ๋น๋์ค ๋ก๋ ์คํจ: {e}")
return None, None
duration = input_video.duration
start_sec = parse_time_to_seconds(start_time_str)
end_sec = parse_time_to_seconds(end_time_str)
if start_sec < 0:
start_sec = 0
if end_sec <= 0 or end_sec > duration:
end_sec = duration
if start_sec >= end_sec:
start_sec = 0
end_sec = duration
start_thumb = generate_thumbnail(input_video, start_sec)
end_thumb = generate_thumbnail(input_video, end_sec)
return start_thumb, end_thumb
# ------------------------------
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ (Blocks)
# ------------------------------
with gr.Blocks() as demo:
gr.Markdown("## ๋์์์ GIF๋ก ๋ณํํ๊ธฐ ๋ฐ๋ชจ")
with gr.Tab("GIF ๋ณํ"):
# ๋์์ ์
๋ก๋
video_input = gr.Video(label="๋์์ ์
๋ก๋")
# ์์/๋ ์๊ฐ ์
๋ ฅ
start_time = gr.Textbox(label="์์ ์๊ฐ (์: 00:00:05)", value="00:00:00")
end_time = gr.Textbox(label="์ข
๋ฃ ์๊ฐ (์: 00:00:10)", value="00:00:05")
# ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ
start_thumb_output = gr.Image(label="์์ ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
end_thumb_output = gr.Image(label="์ข
๋ฃ ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
# ํ๋ซํผ๋ณ ํด์๋(๋น์จ) ์ ํ (Radio ๋ฒํผ)
platform_option = gr.Radio(
label="ํด์๋/๋น์จ ์ ํ",
choices=["์๋ณธ ์ ์ง", "์ ํ๋ธ (16:9)", "์ผ์ธ /๋ฆด์ค (9:16)", "์ ์ฌ๊ฐํ (1:1)", "์ธ์คํ๊ทธ๋จ (4:5)", "ํด๋์ (4:3)"],
value="์๋ณธ ์ ์ง"
)
# ์ถ๋ ฅ ํด์๋ ์ถ์ ์ต์
(1.0์ด๋ฉด ์๋ณธ, 0.1 ~ 1.0 ๋ฒ์)
resolution_scale_slider = gr.Slider(label="์ถ๋ ฅ ํด์๋ ์ถ์ ๋น์จ (0.1 ~ 1.0)",
minimum=0.1, maximum=1.0, step=0.1, value=1.0)
# ํ๋ ์ ๋ ์ดํธ, ์ฌ์ ์๋, ๋ฐ๋ณต ํ์ ์กฐ์
frame_rate_slider = gr.Slider(label="ํ๋ ์ ๋ ์ดํธ ๋ฐฐ์จ ์กฐ์ (0.1 ~ 1.0)",
minimum=0.1, maximum=1.0, step=0.1, value=1.0)
speed_slider = gr.Slider(label="์ฌ์ ์๋ ์กฐ์ (0.5 ~ 5.0)",
minimum=0.5, maximum=5.0, step=0.1, value=1.0)
repeat_slider = gr.Slider(label="GIF ๋ฐ๋ณต ํ์ (0: ๋ฌดํ๋ฐ๋ณต, 1~10: ๋ฐ๋ณต ํ์)",
minimum=0, maximum=10, step=1, value=0)
# GIF ์์ฑ ๋ฒํผ ๋ฐ ๊ฒฐ๊ณผ ์ถ๋ ฅ
generate_button = gr.Button("GIF ์์ฑํ๊ธฐ")
gif_preview_output = gr.Image(label="์์ฑ๋ GIF ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
download_output = gr.File(label="GIF ๋ค์ด๋ก๋ ๋งํฌ")
logs_output = gr.Textbox(label="๋ก๊ทธ ์ถ๋ ฅ", lines=10)
# ์ธ๋ค์ผ ์
๋ฐ์ดํธ ์ด๋ฒคํธ (๋์์ ๋๋ ์๊ฐ ์
๋ ฅ ๋ณ๊ฒฝ ์)
start_time.change(fn=update_thumbnails,
inputs=[video_input, start_time, end_time],
outputs=[start_thumb_output, end_thumb_output])
end_time.change(fn=update_thumbnails,
inputs=[video_input, start_time, end_time],
outputs=[start_thumb_output, end_thumb_output])
video_input.change(fn=update_thumbnails,
inputs=[video_input, start_time, end_time],
outputs=[start_thumb_output, end_thumb_output])
# GIF ์์ฑ ๋ฒํผ ์ด๋ฒคํธ
generate_button.click(
fn=process_video,
inputs=[
video_input, # ๋์์ ์
๋ก๋
start_time, # ์์ ์๊ฐ
end_time, # ์ข
๋ฃ ์๊ฐ
platform_option, # ํ๋ซํผ๋ณ ๋น์จ
frame_rate_slider, # ํ๋ ์ ๋ ์ดํธ ๋ฐฐ์จ
speed_slider, # ์ฌ์ ์๋
repeat_slider, # ๋ฐ๋ณต ํ์
resolution_scale_slider # ํด์๋ ์ถ์ ๋น์จ
],
outputs=[
gif_preview_output, # ์์ฑ๋ GIF ๋ฏธ๋ฆฌ๋ณด๊ธฐ
download_output, # GIF ๋ค์ด๋ก๋ ๋งํฌ
logs_output # ๋ก๊ทธ ์ถ๋ ฅ
]
)
gr.Markdown(
"### [์ฌ์ฉ ๊ฐ์ด๋]\n"
"1. ๋์์์ ์
๋ก๋ํ์ธ์.\n"
"2. ์์/๋ ์๊ฐ์ ์
๋ ฅํ์ธ์.\n"
"3. ํ๋ซํผ๋ณ ๊ถ์ฅ ํด์๋/๋น์จ(์๋ณธ ์ ์ง, ์ ํ๋ธ, ์ผ์ธ /๋ฆด์ค, ์ ์ฌ๊ฐํ, ์ธ์คํ๊ทธ๋จ, ํด๋์)๊ณผ ์ถ๋ ฅ ํด์๋ ์ถ์ ๋น์จ์ ์ ํํ๊ณ ,\n"
" ํ๋ ์ ๋ ์ดํธ ๋ฐฐ์จ, ์ฌ์ ์๋, ๋ฐ๋ณต ํ์๋ฅผ ์กฐ์ ํ ํ `GIF ์์ฑํ๊ธฐ` ๋ฒํผ์ ๋๋ฅด๋ฉด GIF๊ฐ ์์ฑ๋ฉ๋๋ค.\n"
" - ๋ฐ๋ณต ํ์(0: ๋ฌดํ๋ฐ๋ณต, 1~10: ์ง์ ํ์). ์) 1์ด๋ฉด 1ํ ์ฌ์ ํ ๋ฉ์ถค, 2์ด๋ฉด 2ํ ์ฌ์, 0์ด๋ฉด ๋ฌดํ๋ฐ๋ณต.\n"
"4. ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ ๋ค์ด๋ก๋ ๋งํฌ๋ฅผ ํตํด GIF๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.\n"
"โป ๋ฌดํ๋ฐ๋ณต์ด ์๋ ํน์ ๋ฐ๋ณต ํ์๋ฅผ ์ํ ๊ฒฝ์ฐ, ImageMagick์ด ์ค์น๋์ด ์์ด์ผ ํฉ๋๋ค."
)
if __name__ == "__main__":
demo.launch()
|