# -*- coding: utf-8 -*- # SPDX-License-Identifier: MIT # Copyright (c) 2024 [shin0624] import gradio as gr import cv2 import tempfile import zipfile from pathlib import Path def extract_frames_per_second(video_file): temp_dir = tempfile.mkdtemp() frames_dir = Path(temp_dir) / "frames" frames_dir.mkdir(exist_ok=True) cap = cv2.VideoCapture(video_file) fps = cap.get(cv2.CAP_PROP_FPS) if fps == 0: return "FPS를 가져올 수 없습니다." frame_interval = int(fps) # 1초당 1장 frame_count = 0 saved_count = 0 while cap.isOpened(): ret, frame = cap.read() if not ret: break if frame_count % frame_interval == 0: frame_path = frames_dir / f"frame_{saved_count:05d}.png" cv2.imwrite(str(frame_path), frame) saved_count += 1 frame_count += 1 cap.release() zip_path = Path(temp_dir) / "frames.zip" with zipfile.ZipFile(zip_path, 'w') as zipf: for frame_file in frames_dir.glob("*.png"): zipf.write(frame_file, arcname=frame_file.name) return str(zip_path) demo = gr.Interface( fn=extract_frames_per_second, inputs=gr.Video(label="동영상 업로드"), outputs=gr.File(label="1초 간격 프레임 ZIP"), title="1초 간격 프레임 추출기", description="동영상에서 1초마다 1장의 프레임을 PNG로 추출하여 ZIP 파일로 제공합니다." ) if __name__ == "__main__": demo.launch()