Datasets:
File size: 8,319 Bytes
067b479 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
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() |