video / app.py
seawolf2357's picture
Update app.py
80d6daf verified
raw
history blame
No virus
1.26 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")
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()