File size: 13,622 Bytes
cc866c1 695aea0 cc866c1 4fad89f cc866c1 695aea0 cc866c1 695aea0 cc866c1 695aea0 cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f 695aea0 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f 695aea0 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f 695aea0 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f 695aea0 cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f cc866c1 4fad89f | 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | #!/usr/bin/env python3
"""
=============================================================
Sinhala TTS - YouTube Channel Audio Quality Evaluator v3
=============================================================
Run this on your LOCAL MACHINE.
Requirements:
pip install -U yt-dlp librosa soundfile numpy scipy certifi
Usage:
python evaluate_channels.py
=============================================================
"""
import os
import sys
import json
import ssl
import numpy as np
import warnings
warnings.filterwarnings("ignore")
# Fix macOS SSL certificate issue
try:
import certifi
os.environ['SSL_CERT_FILE'] = certifi.where()
os.environ['REQUESTS_CA_BUNDLE'] = certifi.where()
except ImportError:
pass
# Also patch ssl globally as fallback
try:
ssl._create_default_https_context = ssl._create_unverified_context
except AttributeError:
pass
# ============================================================
# CONFIGURATION
# ============================================================
CHANNELS = {
"sunchare": {
"url": "https://www.youtube.com/@sunchare/videos",
"label": "NU1's VLOG (Unlimited History)",
},
"Raamuwa": {
"url": "https://www.youtube.com/@Raamuwa/videos",
"label": "Raamuwa",
},
}
N_VIDEOS_PER_CHANNEL = 4
OUTPUT_DIR = "tts_channel_eval"
# ============================================================
# STEP 1: Download samples using yt-dlp Python API
# ============================================================
def download_samples(channel_key, channel_info, n_videos=N_VIDEOS_PER_CHANNEL):
"""Download n_videos from a channel as WAV audio using Python API."""
import yt_dlp
out_dir = os.path.join(OUTPUT_DIR, channel_key)
os.makedirs(out_dir, exist_ok=True)
print(f"\n{'='*60}")
print(f"Downloading from: {channel_info['label']}")
print(f"URL: {channel_info['url']}")
print(f"{'='*60}")
# Step 1: Extract video list from channel
print(f"\n [1/2] Fetching video list...")
list_opts = {
'quiet': True,
'no_warnings': True,
'extract_flat': 'in_playlist',
'playlist_items': f'1-{n_videos * 3}',
'nocheckcertificate': True,
}
entries = []
try:
with yt_dlp.YoutubeDL(list_opts) as ydl:
info = ydl.extract_info(channel_info["url"], download=False)
if info:
channel_title = info.get('channel', info.get('uploader', channel_key))
raw_entries = info.get('entries', [])
entries = [e for e in raw_entries if e is not None]
print(f" Channel: {channel_title}")
print(f" Found {len(entries)} videos")
except Exception as e:
print(f" Error fetching video list: {e}")
if not entries:
print(f" No entries found.")
print(f" Try: pip install -U yt-dlp certifi")
return []
# Select videos (prefer 3-40 min)
selected = []
skipped = []
for e in entries:
vid_id = e.get('id', '')
title = e.get('title', '?')
dur = e.get('duration') or 0
dur_min = dur / 60 if dur else 0
if not vid_id:
continue
if dur == 0 or (180 <= dur <= 2400):
selected.append((vid_id, title, dur))
print(f" + {title[:55]:55s} ({dur_min:.0f}min)")
if len(selected) >= n_videos:
break
else:
skipped.append((title, dur_min))
if not selected and skipped:
print(f" No videos in 3-40min range. Taking first {n_videos} anyway...")
for e in entries[:n_videos]:
vid_id = e.get('id', '')
title = e.get('title', '?')
dur = e.get('duration') or 0
if vid_id:
selected.append((vid_id, title, dur))
if not selected:
print(f" No downloadable videos found!")
return []
# Step 2: Download each video as WAV
print(f"\n [2/2] Downloading {len(selected)} videos as WAV...")
for i, (vid_id, title, dur) in enumerate(selected):
url = f"https://www.youtube.com/watch?v={vid_id}"
out_template = os.path.join(out_dir, f"{vid_id}.%(ext)s")
dl_opts = {
'format': 'bestaudio/best',
'outtmpl': out_template,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
}],
'postprocessor_args': {
'ffmpeg': ['-ac', '1', '-ar', '22050'],
},
'quiet': True,
'no_warnings': True,
'nocheckcertificate': True,
}
print(f"\n [{i+1}/{len(selected)}] {title[:50]}...")
try:
with yt_dlp.YoutubeDL(dl_opts) as ydl:
ydl.download([url])
print(f" Done")
except Exception as e:
print(f" Failed: {str(e)[:100]}")
wav_files = sorted([f for f in os.listdir(out_dir) if f.endswith('.wav')])
print(f"\n Downloaded {len(wav_files)} WAV files to {out_dir}/")
return [os.path.join(out_dir, f) for f in wav_files]
# ============================================================
# STEP 2: Audio Quality Analysis
# ============================================================
def analyze_audio(wav_path):
"""Analyze a single WAV file for TTS training suitability."""
import librosa
fname = os.path.basename(wav_path)
print(f"\nAnalyzing: {fname}")
try:
y, sr = librosa.load(wav_path, sr=22050, mono=True)
except Exception as e:
print(f" Failed to load: {e}")
return None
duration_sec = len(y) / sr
duration_min = duration_sec / 60
print(f" Duration: {duration_min:.1f} minutes")
results = {
"file": fname,
"duration_min": round(duration_min, 1),
}
# --- RMS Energy & SNR ---
rms = librosa.feature.rms(y=y, frame_length=2048, hop_length=512)[0]
rms_threshold = np.percentile(rms, 20)
noise_frames = rms[rms <= rms_threshold]
speech_frames = rms[rms > rms_threshold]
if len(noise_frames) > 0 and np.mean(noise_frames) > 0:
snr = 20 * np.log10(np.mean(speech_frames) / (np.mean(noise_frames) + 1e-10))
else:
snr = 40.0
results["snr_db"] = round(float(snr), 1)
snr_label = "excellent" if snr >= 25 else "acceptable" if snr >= 15 else "poor"
print(f" SNR: {snr:.1f} dB ({snr_label})")
# --- Spectral Flatness (music vs speech) ---
flatness = librosa.feature.spectral_flatness(y=y)[0]
mean_flat = float(np.mean(flatness))
results["spectral_flatness"] = round(mean_flat, 4)
music_risk = "low" if mean_flat > 0.02 else "medium" if mean_flat > 0.005 else "high"
results["music_risk"] = music_risk
print(f" Music risk: {music_risk} (flatness={mean_flat:.4f})")
# --- Pitch Analysis (first 5 min for speed) ---
y_short = y[:sr * 300] if len(y) > sr * 300 else y
print(f" Running pitch analysis (first {min(duration_min, 5):.0f} min)...")
f0, _, _ = librosa.pyin(y_short, fmin=50, fmax=500, sr=sr)
f0_voiced = f0[~np.isnan(f0)]
if len(f0_voiced) > 0:
pitch_mean = float(np.mean(f0_voiced))
pitch_std = float(np.std(f0_voiced))
voiced_ratio = float(np.sum(~np.isnan(f0)) / len(f0))
results["pitch_mean_hz"] = round(pitch_mean, 1)
results["pitch_std_hz"] = round(pitch_std, 1)
results["voiced_ratio"] = round(voiced_ratio, 3)
if pitch_std > 80:
results["speaker_assessment"] = "likely_multi_speaker"
print(f" Speaker: LIKELY MULTI-SPEAKER (pitch std={pitch_std:.1f}Hz)")
elif pitch_std > 60:
results["speaker_assessment"] = "possibly_multi_speaker"
print(f" Speaker: possibly multi-speaker (pitch std={pitch_std:.1f}Hz)")
else:
results["speaker_assessment"] = "single_speaker"
print(f" Speaker: consistent single speaker (pitch std={pitch_std:.1f}Hz)")
gender = "female" if pitch_mean > 180 else "male"
results["gender_estimate"] = gender
print(f" Voice: {gender} (mean pitch={pitch_mean:.0f}Hz)")
else:
print(f" Pitch: could not extract (no voiced frames detected)")
# --- Speech vs Silence Ratio ---
speech_ratio = np.sum(rms > rms_threshold) / len(rms)
results["speech_pct"] = round(float(speech_ratio * 100), 1)
results["speech_min"] = round(duration_min * speech_ratio, 1)
print(f" Speech content: {speech_ratio:.0%} ({results['speech_min']:.1f} min of speech)")
# --- Overall TTS Quality Score ---
score = 0
if snr >= 25: score += 3
elif snr >= 15: score += 2
elif snr >= 10: score += 1
if results.get("pitch_std_hz", 999) < 50: score += 2
elif results.get("pitch_std_hz", 999) < 80: score += 1
if speech_ratio > 0.6: score += 2
elif speech_ratio > 0.4: score += 1
if mean_flat > 0.01: score += 1
results["tts_score"] = score
grade = "Excellent" if score >= 7 else "Good" if score >= 5 else "Fair" if score >= 3 else "Poor"
results["grade"] = grade
print(f" TTS Quality Score: {score}/8 ({grade})")
return results
# ============================================================
# MAIN
# ============================================================
if __name__ == "__main__":
# Check dependencies
missing = []
for pkg in ['yt_dlp', 'librosa', 'soundfile', 'numpy', 'scipy']:
try:
__import__(pkg)
except ImportError:
missing.append(pkg.replace('_', '-'))
if missing:
print(f"Missing packages: {', '.join(missing)}")
print(f"Install with: pip install -U {' '.join(missing)}")
sys.exit(1)
import yt_dlp
print(f"yt-dlp version: {yt_dlp.version.__version__}")
print(f"Sinhala TTS - YouTube Channel Quality Evaluator v3")
print("=" * 60)
all_results = {}
for channel_key, channel_info in CHANNELS.items():
wav_files = download_samples(channel_key, channel_info)
if not wav_files:
print(f"\nNo files downloaded for {channel_info['label']}")
all_results[channel_key] = []
continue
channel_results = []
for wav_path in wav_files:
res = analyze_audio(wav_path)
if res:
channel_results.append(res)
all_results[channel_key] = channel_results
if channel_results:
total_dur = sum(r["duration_min"] for r in channel_results)
total_speech = sum(r.get("speech_min", 0) for r in channel_results)
avg_snr = np.mean([r["snr_db"] for r in channel_results])
avg_score = np.mean([r["tts_score"] for r in channel_results])
multi_spk = sum(1 for r in channel_results
if "multi" in r.get("speaker_assessment", ""))
music_high = sum(1 for r in channel_results if r.get("music_risk") == "high")
print(f"\n{'='*60}")
print(f"CHANNEL SUMMARY: {channel_info['label']}")
print(f"{'='*60}")
print(f" Videos analyzed: {len(channel_results)}")
print(f" Total duration: {total_dur:.1f} min")
print(f" Usable speech: {total_speech:.1f} min")
print(f" Avg SNR: {avg_snr:.1f} dB")
print(f" Avg TTS Score: {avg_score:.1f}/8")
print(f" Multi-speaker risk: {multi_spk}/{len(channel_results)} videos")
print(f" High music risk: {music_high}/{len(channel_results)} videos")
# Save detailed results
os.makedirs(OUTPUT_DIR, exist_ok=True)
results_path = os.path.join(OUTPUT_DIR, "evaluation_results.json")
with open(results_path, "w") as f:
json.dump(all_results, f, indent=2, ensure_ascii=False)
# ============================================================
# FINAL COMPARISON
# ============================================================
print(f"\n\n{'='*60}")
print(f"FINAL COMPARISON")
print(f"{'='*60}")
print(f"{'Channel':<35} {'Score':>8} {'SNR':>8} {'Speech':>10} {'Speaker':>15} {'Music':>10}")
print(f"{'-'*35} {'-'*8} {'-'*8} {'-'*10} {'-'*15} {'-'*10}")
for channel_key, results in all_results.items():
label = CHANNELS[channel_key]['label']
if isinstance(results, list) and results:
avg_score = np.mean([r["tts_score"] for r in results])
avg_snr = np.mean([r["snr_db"] for r in results])
total_speech = sum(r.get("speech_min", 0) for r in results)
single = sum(1 for r in results if r.get("speaker_assessment") == "single_speaker")
spk_label = "single" if single >= len(results)/2 else "mixed"
high_music = sum(1 for r in results if r.get("music_risk") == "high")
music_label = "low" if high_music == 0 else "some" if high_music < len(results)/2 else "heavy"
print(f"{label:<35} {avg_score:>5.1f}/8 {avg_snr:>6.1f}dB {total_speech:>7.1f}min {spk_label:>15} {music_label:>10}")
else:
print(f"{label:<35} {'No data':>8}")
print(f"\nResults saved to: {results_path}")
print(f"\nDone! Paste the output above (or {results_path}) back to the assistant.")
|