video / app.py
seawolf2357's picture
Update app.py
513a44d verified
raw
history blame
No virus
1.56 kB
import gradio as gr
from moviepy.editor import ImageSequenceClip
import tempfile
import os
import shutil # 이 뢀뢄을 μΆ”κ°€
def images_to_video(image_files):
# μž„μ‹œ 디렉터리 생성
temp_dir = tempfile.mkdtemp()
image_paths = []
for i, image_file in enumerate(image_files):
# μ—…λ‘œλ“œλœ νŒŒμΌμ„ μž„μ‹œ 파일둜 μ €μž₯
temp_file_path = os.path.join(temp_dir, f"image_{i}.png")
with open(temp_file_path, "wb") as f:
f.write(image_file.read()) # λ°”μ΄νŠΈ 데이터λ₯Ό 파일둜 μ €μž₯
image_paths.append(temp_file_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
return image_paths # μ˜ˆμ‹œ λ°˜ν™˜κ°’, μ‹€μ œλ‘œλŠ” μƒμ„±λœ λΉ„λ””μ˜€ 파일 경둜λ₯Ό λ°˜ν™˜ν•΄μ•Ό 함
# 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()