video / app.py
seawolf2357's picture
Update app.py
3bdb854 verified
raw
history blame
1.75 kB
import gradio as gr
from moviepy.editor import ImageSequenceClip
import tempfile
import os
def images_to_video(image_files):
# ์ž„์‹œ ๋””๋ ‰ํ„ฐ๋ฆฌ ์ƒ์„ฑ
temp_dir = tempfile.mkdtemp()
image_paths = []
for i, image_file in enumerate(image_files):
# ์—…๋กœ๋“œ๋œ ํŒŒ์ผ์ด ์ด๋ฏธ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ,
# ํ•ด๋‹น ๊ฒฝ๋กœ๋ฅผ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
image_path = os.path.join(temp_dir, f"image_{i}.png")
if isinstance(image_file, str):
# ์—ฌ๊ธฐ์„œ image_file์€ ์ด๋ฏธ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฏ€๋กœ, ๋ณ„๋„์˜ ์ฒ˜๋ฆฌ ์—†์ด ์‚ฌ์šฉ
shutil.copy(image_file, image_path)
else:
# image_file์ด ๋ฐ”์ดํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ, ํŒŒ์ผ๋กœ ์“ฐ๊ธฐ
with open(image_path, "wb") as f:
f.write(image_file)
image_paths.append(image_path)
# ์ด๋ฏธ์ง€ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋น„๋””์˜ค ํด๋ฆฝ ์ƒ์„ฑ ๋ฐ ๊ธฐํƒ€ ๋กœ์ง...
# ์ด๋ฏธ์ง€ ์‹œํ€€์Šค๋กœ๋ถ€ํ„ฐ ๋น„๋””์˜ค ํด๋ฆฝ ์ƒ์„ฑ (๊ฐ ์ด๋ฏธ์ง€์˜ ์ง€์† ์‹œ๊ฐ„์€ 2์ดˆ)
clip = ImageSequenceClip(image_paths, fps=0.5) # fps=0.5 => ๊ฐ ์ด๋ฏธ์ง€๋Š” 2์ดˆ ๋™์•ˆ ๋ณด์ž„
# ๋น„๋””์˜ค ํŒŒ์ผ ์ €์žฅ
output_video_path = os.path.join(temp_dir, "output.mp4")
clip.write_videofile(output_video_path, fps=24) # 24fps๋กœ ์ถœ๋ ฅ ๋น„๋””์˜ค ์ €์žฅ
# ์ƒ์„ฑ๋œ ๋น„๋””์˜ค ํŒŒ์ผ ๊ฒฝ๋กœ ๋ฐ˜ํ™˜
return output_video_path
# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
with gr.Blocks() as demo:
with gr.Row():
file_input = gr.File(label="Upload images")
video_output = gr.Video(label="Output video")
file_input.change(images_to_video, inputs=file_input, outputs=video_output)
demo.launch()