Spaces:
Sleeping
Sleeping
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() | |