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()