File size: 8,695 Bytes
2d0b48d e3952dc 2d0b48d 3005b66 2d0b48d e3952dc 2d0b48d 3005b66 e3952dc 2d0b48d e3952dc 2d0b48d 3005b66 2d0b48d 3005b66 2d0b48d 3005b66 2d0b48d 4abd773 3005b66 4abd773 2d0b48d 3005b66 2d0b48d 4abd773 3005b66 2d0b48d e3952dc 3005b66 e3952dc 2d0b48d 4abd773 3005b66 2d0b48d 3005b66 2d0b48d e3952dc 2d0b48d e3952dc 8e92fe3 e3952dc 2d0b48d e3952dc 2d0b48d e3952dc 3005b66 e3952dc 2d0b48d 4abd773 2d0b48d 3005b66 e3952dc 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 |
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 process_video(video,
start_time_str,
end_time_str,
resolution_factor,
frame_rate_factor,
speed_factor,
repeat_count):
"""
๋์์์ 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)
# ํด์๋ ์กฐ์ (ํ์ผ ํฌ๊ธฐ ์ ๊ฐ์ ์ํด)
if abs(resolution_factor - 1.0) > 1e-3:
add_log(f"[LOG 7] ํด์๋ {resolution_factor*100:.1f}%๋ก ์กฐ์ ์ค...")
clip = clip.resize(resolution_factor)
# ์ฌ์์๋ ์กฐ์ : 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์ frame_rate_factor๋ฅผ ๋ฐ์
original_fps = clip.fps
target_fps = original_fps * speed_factor * 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 ๋ฉํ๋ฐ์ดํฐ ์ฌ์ฉ)
add_log(f"[LOG 10] GIF ๋ฐ๋ณต ํ์ ์ค์ : {repeat_count} (0์ด๋ฉด ๋ฌดํ๋ฐ๋ณต)")
final_clip = clip
add_log("[LOG 11] GIF ์์ฑ ์ค...")
output_filename = f"temp_{uuid.uuid4().hex}.gif"
try:
# loop=0์ด๋ฉด ๋ฌดํ๋ฐ๋ณต, ๊ทธ ์ธ์๋ ํด๋น ๊ฐ ์ฌ์ฉ
loop_param = 0 if int(repeat_count) == 0 else int(repeat_count)
final_clip.write_gif(output_filename, fps=target_fps, program='ffmpeg', 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="์ข
๋ฃ ์ธ๋ค์ผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ")
# ํด์๋, ํ๋ ์ ๋ ์ดํธ, ์ฌ์ ์๋, ๋ฐ๋ณต ํ์ ์กฐ์
resolution_slider = gr.Slider(label="ํด์๋ ๋น์จ ์กฐ์ (0.1 ~ 1.0)", minimum=0.1, maximum=1.0, step=0.1, value=0.5)
fps_slider = gr.Slider(label="ํ๋ ์ ๋ ์ดํธ ๋ฐฐ์จ ์กฐ์ (0.1 ~ 2.0)", minimum=0.1, maximum=2.0, step=0.1, value=0.5)
speed_slider = gr.Slider(label="์ฌ์ ์๋ ์กฐ์ (0.5 ~ 2.0)", minimum=0.5, maximum=2.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 ์์ฑํ๊ธฐ")
# ๋ฏธ๋ฆฌ๋ณด๊ธฐ: gr.Image๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋๋ฉ์ด์
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,
resolution_slider, fps_slider, speed_slider, repeat_slider],
outputs=[gif_preview_output, download_output, logs_output])
gr.Markdown(
"### [์ฌ์ฉ ๊ฐ์ด๋]\n"
"1. ๋์์์ ์
๋ก๋ํ์ธ์.\n"
"2. ์์/๋ ์๊ฐ์ ์ ์ ํ ์
๋ ฅํ์ธ์.\n"
"3. ํด์๋, ํ๋ ์ ๋ ์ดํธ ๋ฐฐ์จ, ์ฌ์ ์๋, ๋ฐ๋ณต ํ์๋ฅผ ์กฐ์ ํ ๋ค `GIF ์์ฑํ๊ธฐ` ๋ฒํผ์ ๋๋ฅด๋ฉด GIF๊ฐ ์์ฑ๋ฉ๋๋ค.\n"
" - ๋ฐ๋ณต ํ์: 0์ ์ ํํ๋ฉด ๋ฌดํ๋ฐ๋ณต, 1~10์ ํด๋น ํ์๋งํผ ๋ฐ๋ณต๋ฉ๋๋ค.\n"
"4. ๊ฒฐ๊ณผ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์ ๋ค์ด๋ก๋ ๋งํฌ๋ฅผ ํตํด GIF๋ฅผ ํ์ธํ ์ ์์ต๋๋ค."
)
if __name__ == "__main__":
demo.launch()
|