| |
| |
|
|
| import argparse |
| from pathlib import Path |
|
|
| AUDIO_EXTS = [".wav", ".flac", ".mp3", ".ogg", ".m4a"] |
|
|
|
|
| def is_audio_name(x: str) -> bool: |
| return Path(x).suffix.lower() in AUDIO_EXTS |
|
|
|
|
| def is_sbc_source(x: str) -> bool: |
| """ |
| 第三列来源文件是否是 SBC 切片。 |
| 例如: |
| train_02263_SBC023_bgclip_1123.00_1127.00_score54.7008.wav |
| """ |
| name = Path(x).name |
| return "SBC" in name and is_audio_name(name) |
|
|
|
|
| def parse_protocol(protocol_path: Path): |
| """ |
| 返回所有 label == orig 的行,并标记第三列是否是 SBC 来源。 |
| 兼容格式: |
| train_orig_0007711 orig train_02263_SBC023_bgclip_1123.00_1127.00_score54.7008.wav |
| """ |
| orig_rows = [] |
|
|
| with open(protocol_path, "r", encoding="utf-8", errors="ignore") as f: |
| for line_no, line in enumerate(f, 1): |
| line = line.strip() |
|
|
| if not line or line.startswith("#"): |
| continue |
|
|
| parts = line.split() |
| if len(parts) < 2: |
| continue |
|
|
| utt_id = parts[0] |
| label = parts[1] |
|
|
| |
| if utt_id.lower() in {"utt_id", "id"} or label.lower() == "label": |
| continue |
|
|
| if label != "orig": |
| continue |
|
|
| src = parts[2] if len(parts) >= 3 else "" |
|
|
| orig_rows.append({ |
| "line_no": line_no, |
| "utt_id": utt_id, |
| "src": Path(src).name if src else "", |
| "is_sbc": is_sbc_source(src), |
| }) |
|
|
| return orig_rows |
|
|
|
|
| def find_target_file(wav_dir: Path, utt_id: str): |
| """ |
| 要删的是第一列 utt_id 对应的音频: |
| train_orig_0007711 -> train_orig_0007711.wav |
| """ |
| for ext in AUDIO_EXTS: |
| p = wav_dir / f"{utt_id}{ext}" |
| if p.exists(): |
| return p |
| return None |
|
|
|
|
| def process_split(mixed_dir: Path, split: str, do_delete: bool): |
| split_dir = mixed_dir / split |
| wav_dir = split_dir / "wav" |
| protocol_path = split_dir / "protocol.txt" |
|
|
| result = { |
| "split": split, |
| "total_orig_rows": 0, |
| "sbc_orig_rows": 0, |
| "non_sbc_orig_rows": 0, |
| "existing_delete_files": 0, |
| "missing_delete_files": 0, |
| "deleted_files": 0, |
| "delete_targets": [], |
| "missing_targets": [], |
| "non_sbc_orig": [], |
| } |
|
|
| if not protocol_path.exists(): |
| print(f"[WARN] Missing protocol: {protocol_path}") |
| return result |
|
|
| if not wav_dir.exists(): |
| print(f"[WARN] Missing wav dir: {wav_dir}") |
| return result |
|
|
| orig_rows = parse_protocol(protocol_path) |
|
|
| result["total_orig_rows"] = len(orig_rows) |
|
|
| for r in orig_rows: |
| if not r["is_sbc"]: |
| result["non_sbc_orig_rows"] += 1 |
| result["non_sbc_orig"].append(r) |
| continue |
|
|
| result["sbc_orig_rows"] += 1 |
|
|
| target = find_target_file(wav_dir, r["utt_id"]) |
|
|
| if target is None: |
| result["missing_delete_files"] += 1 |
| result["missing_targets"].append(r) |
| continue |
|
|
| result["existing_delete_files"] += 1 |
| result["delete_targets"].append({ |
| **r, |
| "target": target, |
| }) |
|
|
| if do_delete: |
| target.unlink() |
| result["deleted_files"] += 1 |
|
|
| return result |
|
|
|
|
| def write_reports(report_dir: Path, mixed_dir: Path, results): |
| report_dir.mkdir(parents=True, exist_ok=True) |
|
|
| summary_path = report_dir / "summary.txt" |
|
|
| with open(summary_path, "w", encoding="utf-8") as f: |
| f.write( |
| "split total_orig sbc_orig non_sbc_orig " |
| "existing_to_delete missing_to_delete deleted\n" |
| ) |
|
|
| for r in results: |
| f.write( |
| f"{r['split']} " |
| f"{r['total_orig_rows']} " |
| f"{r['sbc_orig_rows']} " |
| f"{r['non_sbc_orig_rows']} " |
| f"{r['existing_delete_files']} " |
| f"{r['missing_delete_files']} " |
| f"{r['deleted_files']}\n" |
| ) |
|
|
| f.write("\n") |
|
|
| f.write( |
| f"TOTAL " |
| f"{sum(r['total_orig_rows'] for r in results)} " |
| f"{sum(r['sbc_orig_rows'] for r in results)} " |
| f"{sum(r['non_sbc_orig_rows'] for r in results)} " |
| f"{sum(r['existing_delete_files'] for r in results)} " |
| f"{sum(r['missing_delete_files'] for r in results)} " |
| f"{sum(r['deleted_files'] for r in results)}\n" |
| ) |
|
|
| for r in results: |
| split = r["split"] |
|
|
| with open(report_dir / f"{split}_delete_targets.txt", "w", encoding="utf-8") as f: |
| f.write("line_no utt_id src_sbc target_file\n") |
| for x in r["delete_targets"]: |
| rel = x["target"].relative_to(mixed_dir) |
| f.write(f"{x['line_no']} {x['utt_id']} {x['src']} {rel}\n") |
|
|
| with open(report_dir / f"{split}_missing_targets.txt", "w", encoding="utf-8") as f: |
| f.write("line_no utt_id src_sbc expected_file\n") |
| for x in r["missing_targets"]: |
| f.write(f"{x['line_no']} {x['utt_id']} {x['src']} {split}/wav/{x['utt_id']}.wav\n") |
|
|
| with open(report_dir / f"{split}_non_sbc_orig_kept.txt", "w", encoding="utf-8") as f: |
| f.write("line_no utt_id src\n") |
| for x in r["non_sbc_orig"]: |
| f.write(f"{x['line_no']} {x['utt_id']} {x['src']}\n") |
|
|
|
|
| def print_summary(results, do_delete: bool, report_dir: Path): |
| print("\n==================== Summary ====================") |
| print(f"Mode : {'DELETE' if do_delete else 'DRY-RUN'}") |
| print(f"Report dir : {report_dir}") |
|
|
| total_orig = 0 |
| total_sbc = 0 |
| total_non_sbc = 0 |
| total_existing = 0 |
| total_missing = 0 |
| total_deleted = 0 |
|
|
| for r in results: |
| total_orig += r["total_orig_rows"] |
| total_sbc += r["sbc_orig_rows"] |
| total_non_sbc += r["non_sbc_orig_rows"] |
| total_existing += r["existing_delete_files"] |
| total_missing += r["missing_delete_files"] |
| total_deleted += r["deleted_files"] |
|
|
| print(f"\n[{r['split']}]") |
| print(f" Total orig rows : {r['total_orig_rows']}") |
| print(f" SBC-corresponding orig rows : {r['sbc_orig_rows']}") |
| print(f" Non-SBC orig rows kept : {r['non_sbc_orig_rows']}") |
| print(f" Existing files to delete : {r['existing_delete_files']}") |
| print(f" Missing target files : {r['missing_delete_files']}") |
| print(f" Deleted files : {r['deleted_files']}") |
|
|
| print("\n[TOTAL]") |
| print(f" Total orig rows : {total_orig}") |
| print(f" SBC-corresponding orig rows : {total_sbc}") |
| print(f" Non-SBC orig rows kept : {total_non_sbc}") |
| print(f" Existing files to delete : {total_existing}") |
| print(f" Missing target files : {total_missing}") |
| print(f" Deleted files : {total_deleted}") |
|
|
| if total_orig > 0: |
| ratio = total_sbc / total_orig * 100 |
| print(f" SBC-orig ratio : {ratio:.2f}%") |
|
|
| if not do_delete: |
| print("\n[DRY-RUN] 没有真正删除。确认 existing files to delete 数量正确后,加 --delete 再运行。") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser( |
| description="Compare total orig rows with SBC-corresponding orig rows, then delete only those orig wavs." |
| ) |
|
|
| parser.add_argument( |
| "--mixed_sounds_dir", |
| type=str, |
| required=True, |
| help="Path to Mixed_sounds directory.", |
| ) |
|
|
| parser.add_argument( |
| "--delete", |
| action="store_true", |
| help="Actually delete files. Default is dry-run.", |
| ) |
|
|
| args = parser.parse_args() |
|
|
| mixed_dir = Path(args.mixed_sounds_dir).resolve() |
| report_dir = mixed_dir / "delete_sbc_orig_compare_report" |
|
|
| results = [] |
|
|
| for split in ["train", "eval"]: |
| results.append( |
| process_split( |
| mixed_dir=mixed_dir, |
| split=split, |
| do_delete=args.delete, |
| ) |
| ) |
|
|
| write_reports(report_dir, mixed_dir, results) |
| print_summary(results, args.delete, report_dir) |
|
|
|
|
| if __name__ == "__main__": |
| main() |