import os import cv2 import glob from concurrent.futures import ThreadPoolExecutor from tqdm import tqdm # ================= 配置路径 ================= SRC_BASE = "/mnt/petrelfs/fengyuan/workspace_zpy/data/RISEBench/RISEBench_outputs/wan2/wan2_2_result" DST_BASE = "/mnt/petrelfs/fengyuan/workspace_zpy/data/RISEBench/RISEBench_outputs/wan2" SUB_FOLDERS = [ "causal_reasoning", "logical_reasoning", "spatial_reasoning", "temporal_reasoning" ] # 设置并行线程数 (根据CPU核心数调整,通常 8-16 比较合适) MAX_WORKERS = 16 # =========================================== def extract_last_frame(args): """ 单个视频处理函数 """ src_path, dst_path = args try: # 打开视频 cap = cv2.VideoCapture(src_path) if not cap.isOpened(): return f"Error: Cannot open {src_path}" # 获取总帧数 total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) if total_frames > 0: # 定位到最后一帧 cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1) ret, frame = cap.read() if ret: # 保存图片 cv2.imwrite(dst_path, frame) else: return f"Error: Could not read last frame of {src_path}" else: return f"Error: Video has 0 frames {src_path}" cap.release() return None # Success except Exception as e: return f"Exception processing {src_path}: {str(e)}" def main(): tasks = [] print("正在扫描视频文件...") # 遍历四个子文件夹 for sub in SUB_FOLDERS: src_dir = os.path.join(SRC_BASE, sub) dst_dir = os.path.join(DST_BASE, sub) # 如果输出目录不存在则创建 os.makedirs(dst_dir, exist_ok=True) # 获取所有mp4文件 mp4_files = glob.glob(os.path.join(src_dir, "*.mp4")) for mp4_path in mp4_files: file_name = os.path.basename(mp4_path) # 替换扩展名 .mp4 -> .png image_name = os.path.splitext(file_name)[0] + ".png" dst_path = os.path.join(dst_dir, image_name) tasks.append((mp4_path, dst_path)) print(f"共发现 {len(tasks)} 个视频,开始处理...") # 使用多线程并行处理 with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: # 使用 tqdm 显示进度条 results = list(tqdm(executor.map(extract_last_frame, tasks), total=len(tasks), unit="vid")) # 打印错误日志(如果有) errors = [res for res in results if res is not None] if errors: print(f"\n处理完成,但有 {len(errors)} 个错误:") for err in errors: print(err) else: print("\n所有视频处理成功!") if __name__ == "__main__": main()