Path: blob/master/memo/comfyui_anidiff_long/extract.py
3275 views
import os1import ffmpeg23def extract_frames(video_path, output_dir, fps='source'):4"""5동영상 파일로부터 프레임을 추출하여 이미지로 저장합니다.67Args:8video_path (str): 동영상 파일의 경로입니다.9output_dir (str): 추출된 이미지를 저장할 폴더의 경로입니다.10fps (str): 이미지 추출 시 사용할 FPS입니다. 기본값은 'source'로, 동영상의 원본 FPS를 사용합니다.11"""12if not os.path.exists(output_dir):13os.makedirs(output_dir)1415if fps == 'source':16# 동영상의 FPS를 가져옵니다.17probe = ffmpeg.probe(video_path)18video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)19fps = eval(video_stream['r_frame_rate'])2021(22ffmpeg23.input(video_path)24.output(os.path.join(output_dir, '%07d.png'), vf=f'fps={fps}')25.run(capture_stdout=True, capture_stderr=True)26)2728def process_videos(folder_path):29"""30주어진 폴더 내의 모든 MP4 파일에 대해 작업을 수행합니다.3132Args:33folder_path (str): 동영상 파일이 있는 폴더의 경로입니다.34"""35for file in os.listdir(folder_path):36if file.endswith('.mp4'):37print(f'# {file}')38video_path = os.path.join(folder_path, file)39output_dir = os.path.join(folder_path, "extract", os.path.splitext(file)[0])40extract_frames(video_path, output_dir)4142# 사용 예43folder_path = './' # 동영상 파일이 있는 폴더의 경로44process_videos(folder_path)454647