| import os |
| import json |
| import shutil |
| import time |
| import concurrent.futures |
|
|
| split = "train" |
| |
| OLD_ROOT = "/workspace/ci_vid_chunk0" |
| NEW_ROOT = f"/workspace/ci_vid_SC_{split}" |
| JSONL_FILE = os.path.join(OLD_ROOT, f"CI-VID_{split}_samples_SC.jsonl") |
|
|
| |
| MAX_WORKERS = 16 |
| PRINT_EVERY = 100 |
|
|
| def move_video_dir(line: str) -> str: |
| """ |
| 解析 JSONL 单行,将对应的视频文件夹移动到新目录。 |
| 返回状态字符串用于统计。 |
| """ |
| line = line.strip() |
| if not line: |
| return "empty" |
|
|
| try: |
| row = json.loads(line) |
| except json.JSONDecodeError: |
| return "json_fail" |
|
|
| video_rel_path = row.get("video_path") |
| if not isinstance(video_rel_path, str): |
| return "format_fail" |
|
|
| old_dir = os.path.join(OLD_ROOT, video_rel_path) |
| new_dir = os.path.join(NEW_ROOT, video_rel_path) |
|
|
| |
| if not os.path.isdir(old_dir): |
| |
| if os.path.isdir(new_dir): |
| return "already_moved" |
| return "missing_old" |
|
|
| |
| parent_new_dir = os.path.dirname(new_dir) |
| os.makedirs(parent_new_dir, exist_ok=True) |
|
|
| try: |
| |
| |
| shutil.copytree(old_dir, new_dir, dirs_exist_ok=True) |
| return "success" |
| except Exception as e: |
| |
| return f"error" |
|
|
| def main(): |
| if not os.path.exists(JSONL_FILE): |
| print(f"找不到 JSONL 文件: {JSONL_FILE}") |
| return |
|
|
| |
| total_lines = sum(1 for line in open(JSONL_FILE, "r", encoding="utf-8") if line.strip()) |
| print(f"共发现 {total_lines} 条待处理记录。开始移动...\n") |
|
|
| stats = { |
| "success": 0, |
| "already_moved": 0, |
| "missing_old": 0, |
| "error": 0, |
| "json_fail": 0, |
| "format_fail": 0, |
| "empty": 0 |
| } |
|
|
| t_start = time.time() |
| processed = 0 |
|
|
| with open(JSONL_FILE, "r", encoding="utf-8") as fin: |
| with concurrent.futures.ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor: |
| |
| futures = [executor.submit(move_video_dir, line) for line in fin] |
| |
| |
| for future in concurrent.futures.as_completed(futures): |
| status = future.result() |
| stats[status] += 1 |
| processed += 1 |
| |
| |
| if processed % PRINT_EVERY == 0 or processed == total_lines: |
| elapsed = time.time() - t_start |
| speed = processed / elapsed if elapsed > 0 else 0.0 |
| pct = (processed / total_lines) * 100 |
| print( |
| f"[{processed}/{total_lines} | {pct:.1f}%] " |
| f"成功: {stats['success']} | 已移动: {stats['already_moved']} | " |
| f"缺失: {stats['missing_old']} | 报错: {stats['error']} | " |
| f"速度: {speed:.1f} 个/秒" |
| ) |
|
|
| print("\n移动完成!统计结果:") |
| for k, v in stats.items(): |
| if v > 0: |
| print(f"- {k}: {v}") |
|
|
| if __name__ == "__main__": |
| main() |