File size: 6,510 Bytes
31d86f9 cc04543 3f777d6 a263ba4 3f777d6 31d86f9 3f777d6 31d86f9 3f777d6 31d86f9 3f777d6 cc04543 31d86f9 cc04543 31d86f9 cc04543 3f777d6 31d86f9 3f777d6 31d86f9 cc04543 31d86f9 3f777d6 31d86f9 3f777d6 31d86f9 cc04543 3f777d6 cc04543 3f777d6 cc04543 3f777d6 31d86f9 3f777d6 31d86f9 cc04543 31d86f9 3f777d6 31d86f9 cc04543 3f777d6 cc04543 3f777d6 cc04543 3f777d6 31d86f9 cc04543 3f777d6 31d86f9 3f777d6 31d86f9 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 3f777d6 5a56204 |
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 |
import os
import tempfile
import gradio as gr
import numpy as np # (νμνλ€λ©΄)
import imageio
from PIL import Image
import moviepy.editor as me # MoviePy μ 체 μν¬νΈ
LOGS = []
def log_step(message: str):
LOGS.append(message)
print(message)
return "\n".join(LOGS)
def parse_time_str(time_str: str):
try:
parts = time_str.strip().split(":")
if len(parts) == 3:
h, m, s = parts
return int(h)*3600 + int(m)*60 + float(s)
elif len(parts) == 2:
m, s = parts
return int(m)*60 + float(s)
elif len(parts) == 1:
return float(parts[0])
else:
return 0.0
except:
return 0.0
def upload_video(video_file):
if video_file is None:
return None, "μ
λ‘λλ νμΌμ΄ μμ΅λλ€.", None, log_step("[λ¨κ³ 1] μ
λ‘λλ νμΌμ΄ μμ΅λλ€.")
temp_dir = tempfile.mkdtemp()
temp_video_path = os.path.join(temp_dir, video_file.name)
with open(temp_video_path, "wb") as f:
f.write(video_file.read())
try:
clip = me.VideoFileClip(temp_video_path)
duration_sec = clip.duration
msg = f"μ¬μ μκ°: {duration_sec:.2f}μ΄"
log_out = log_step("[λ¨κ³ 1] λΉλμ€ μ
λ‘λ λ° μ¬μ νμΈ μλ£")
return temp_video_path, msg, duration_sec, log_out
except Exception as e:
err_log = log_step(f"[λ¨κ³ 1] μ
λ‘λ μλ¬: {e}")
return None, f"μμ μ²λ¦¬ μλ¬: {e}", None, err_log
def generate_thumbnails(video_path, start_time_str, end_time_str, log_history):
log_step("[λ¨κ³ 4] μμ/λ μκ° μ
λ ₯ μλ£")
start_s = parse_time_str(start_time_str)
end_s = parse_time_str(end_time_str)
if not video_path:
return None, None, log_step("[λ¨κ³ 5] λΉλμ€ λ―Έμ
λ‘λ β μΈλ€μΌ λΆκ°")
try:
clip = me.VideoFileClip(video_path)
duration = clip.duration
if start_s < 0: start_s = 0
if end_s > duration: end_s = duration
if start_s > end_s: start_s, end_s = end_s, start_s
thumb1 = clip.get_frame(start_s)
thumb2 = clip.get_frame(end_s)
thumb1_img = Image.fromarray(thumb1)
thumb2_img = Image.fromarray(thumb2)
log_out = log_step("[λ¨κ³ 5] μΈλ€μΌ μμ± μλ£")
return thumb1_img, thumb2_img, log_out
except Exception as e:
err_log = log_step(f"[λ¨κ³ 5] μΈλ€μΌ μμ± μλ¬: {e}")
return None, None, err_log
def create_gif(video_path, start_time_str, end_time_str, resolution, fps, speed, loop_count, log_history):
if not video_path:
return None, None, log_step("[λ¨κ³ 10] GIF μμ± μ€ν¨: λΉλμ€κ° μ
λ‘λλμ§ μμ.")
start_s = parse_time_str(start_time_str)
end_s = parse_time_str(end_time_str)
try:
clip = me.VideoFileClip(video_path)
duration = clip.duration
if start_s < 0: start_s = 0
if end_s > duration: end_s = duration
if start_s > end_s: start_s, end_s = end_s, start_s
subclip = clip.subclip(start_s, end_s)
if speed != 1.0:
subclip = subclip.fx(me.vfx.speedx, speed)
# ν΄μλ μ‘°μ (κΈ°λ³Έ 100%)
w, h = subclip.size
if resolution < 100:
scale = resolution / 100.0
subclip = subclip.resize((int(w * scale), int(h * scale)))
# FPS
fps = int(fps) if fps > 0 else None
# λ°λ³΅ νμ
loop = loop_count # 0=무ν
temp_dir = tempfile.mkdtemp()
gif_path = os.path.join(temp_dir, "output.gif")
subclip.write_gif(gif_path, fps=fps, loop=loop)
log_out1 = log_step("[λ¨κ³ 10] 'GIF μμ±' λ²νΌ ν΄λ¦λ¨")
log_out2 = log_step("[λ¨κ³ 11] GIF 미리보기 μμ± μλ£")
log_out3 = log_step("[λ¨κ³ 12] GIF λ€μ΄λ‘λ μ€λΉ μλ£")
preview_img = Image.open(gif_path)
return preview_img, gif_path, "\n".join([log_out1, log_out2, log_out3])
except Exception as e:
err_log = log_step(f"[λ¨κ³ 10~12] GIF μμ± μλ¬: {e}")
return None, None, err_log
def build_interface():
with gr.Blocks() as demo:
gr.Markdown("## λμμμ GIFλ‘ λ³ννκΈ°")
video_upload = gr.File(label="λΉλμ€ μ
λ‘λ", file_types=["video"])
video_player = gr.Video(label="μ
λ‘λλ μμ 미리보기")
video_info = gr.Textbox(label="μ
λ‘λλ μμ μ 보", interactive=False)
video_path_state = gr.State()
video_duration_state = gr.State()
log_box = gr.Textbox(label="λ‘κ·Έ", interactive=False, lines=10)
with gr.Row():
start_time = gr.Textbox(label="μμ μκ° (HH:MM:SS/ MM:SS)", value="0:00")
end_time = gr.Textbox(label="λ μκ° (HH:MM:SS/ MM:SS)", value="0:10")
with gr.Row():
thumb1 = gr.Image(label="μμ μ§μ μΈλ€μΌ")
thumb2 = gr.Image(label="λ μ§μ μΈλ€μΌ")
resolution_slider = gr.Slider(1, 100, 100, step=1, label="ν΄μλ(%)")
fps_slider = gr.Slider(1, 60, 30, step=1, label="FPS")
speed_slider = gr.Slider(0.1, 3.0, 1.0, step=0.1, label="μ¬μ μλ(λ°°)")
loop_slider = gr.Slider(0, 10, 0, step=1, label="GIF λ°λ³΅ νμ (0=무ν)")
generate_gif_btn = gr.Button("GIF μμ±")
gif_preview = gr.Image(label="GIF 미리보기")
gif_download = gr.File(label="GIF λ€μ΄λ‘λ(ν΄λ¦νμ¬ μ μ₯)")
# μ΄λ²€νΈ λ°μΈλ©
video_upload.change(
fn=upload_video,
inputs=video_upload,
outputs=[video_path_state, video_info, video_duration_state, log_box]
)
start_time.change(
fn=generate_thumbnails,
inputs=[video_path_state, start_time, end_time, log_box],
outputs=[thumb1, thumb2, log_box]
)
end_time.change(
fn=generate_thumbnails,
inputs=[video_path_state, start_time, end_time, log_box],
outputs=[thumb1, thumb2, log_box]
)
generate_gif_btn.click(
fn=create_gif,
inputs=[video_path_state, start_time, end_time, resolution_slider, fps_slider, speed_slider, loop_slider, log_box],
outputs=[gif_preview, gif_download, log_box]
)
return demo
if __name__ == "__main__":
interface = build_interface()
interface.launch()
|