CI-VID / move_ci_vid_samples.py
Helios1208's picture
Upload 3 files
7232c2a verified
import os
import json
import shutil
import time
import concurrent.futures
split = "train" # train, test
# 配置路径
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"
# 确保新目录的父级结构存在 (例如创建 chunk_33/00d2d19dd524c7f7ccaadba657c49263)
parent_new_dir = os.path.dirname(new_dir)
os.makedirs(parent_new_dir, exist_ok=True)
try:
# 移动整个文件夹
# shutil.move(old_dir, new_dir)
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()