| | import cv2
|
| | import os
|
| | import sys
|
| | from datetime import timedelta
|
| |
|
| | def extract_frames_per_second(video_path, output_dir, interval_seconds=3):
|
| | """
|
| | 从视频中每隔 interval_seconds 提取一帧并保存到指定目录(按时间定位,保证时间戳单调递增)。
|
| | :param video_path: 视频文件的路径
|
| | :param output_dir: 帧图片的保存目录
|
| | :param interval_seconds: 每隔多少秒保存一帧(默认3秒)
|
| | """
|
| | os.makedirs(output_dir, exist_ok=True)
|
| |
|
| | if os.path.exists(output_dir) and os.listdir(output_dir):
|
| | print(f"输出目录已存在且非空,跳过提取:{os.path.abspath(output_dir)}")
|
| | return
|
| | os.makedirs(output_dir, exist_ok=True)
|
| | print(f"帧保存目录:{os.path.abspath(output_dir)}")
|
| |
|
| |
|
| | cap = cv2.VideoCapture(video_path)
|
| | if not cap.isOpened():
|
| | raise ValueError(f"无法打开视频文件:{video_path}")
|
| |
|
| |
|
| | fps = cap.get(cv2.CAP_PROP_FPS)
|
| | total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| | duration = total_frames / fps if fps > 0 else 0
|
| | print(f"视频信息:帧率={fps:.2f} FPS | 总帧数={total_frames} | 总时长={timedelta(seconds=duration)}")
|
| |
|
| | if fps <= 0:
|
| | raise ValueError("无法获取视频帧率,视频文件可能损坏或格式不支持")
|
| |
|
| | saved_count = 0
|
| |
|
| | try:
|
| | t = 0.0
|
| |
|
| | while t <= duration:
|
| |
|
| | cap.set(cv2.CAP_PROP_POS_MSEC, t * 1000)
|
| | ret, frame = cap.read()
|
| | if not ret:
|
| |
|
| | break
|
| |
|
| |
|
| | frame_filename = f"{saved_count:06d}_{t:.2f}s.jpg"
|
| | frame_path = os.path.join(output_dir, frame_filename)
|
| |
|
| |
|
| | cv2.imwrite(frame_path, frame)
|
| | saved_count += 1
|
| |
|
| |
|
| | if saved_count % 10 == 0:
|
| | progress = (t / duration) * 100 if duration > 0 else 0
|
| | print(f"进度:{progress:.1f}% | 已保存 {saved_count} 帧 | 时间:{t:.2f}s")
|
| |
|
| | t += interval_seconds
|
| |
|
| | except Exception as e:
|
| | raise RuntimeError(f"提取帧时发生错误:{str(e)}")
|
| | finally:
|
| |
|
| | cap.release()
|
| | cv2.destroyAllWindows()
|
| |
|
| |
|
| | print(f"\n提取完成!共保存 {saved_count} 帧,保存路径:{os.path.abspath(output_dir)}")
|
| |
|
| | output_dirs = ["/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/no other choice","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/the roses","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/nouvelle","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/legs","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/frankenstein"]
|
| | input_dirs = ["/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/films/어쩔수가없다 NO OTHER CHOICE, 2025.1080p.WEB-DL.H264.AAC.mp4","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/films/The.Roses.2025.2160p.WEB-DL.DDP5.1.Atmos.SDR.H265-AOC/The.Roses.2025.2160p.WEB-DL.DDP5.1.Atmos.SDR.H265-AOC.mkv","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/films/NOUVELLE.VAGUE.2025.2160p.NF.WEB-DL.DDP.5.1.H.265-CHDWEB[PianYuan]/NOUVELLE.VAGUE.2025.2160p.NF.WEB-DL.DDP.5.1.H.265-CHDWEB.mkv","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/films/If.I.Had.Legs.Id.Kick.You.2025.1080p.iT.WEB-DL.DDP5.1.Atmos.H264-BTM/If.I.Had.Legs.Id.Kick.You.2025.1080p.iT.WEB-DL[Ben The Men].mkv","/fi-lib/workspace/sjx/DiffSynth-Studio/dataset/films/Frankenstein.2025.1080p.NF.WEB-DL.DDP5.1.Atmos.H.264-FLUX/Frankenstein.2025.1080p.NF.WEB-DL.DDP5.1.Atmos.H.264-FLUX.mkv"]
|
| | if __name__ == "__main__":
|
| |
|
| | for i in range(len(output_dirs)):
|
| | try:
|
| | extract_frames_per_second(video_path=input_dirs[i],output_dir=output_dirs[i],interval_seconds=3)
|
| | except Exception as e:
|
| | print(f"程序异常:{str(e)}", file=sys.stderr)
|
| | sys.exit(1)
|
| |
|