Commit Β·
2323e40
1
Parent(s): 5680194
Feature: YouTube heatmap extraction for viral segment detection
Browse files404 unmodified lines40540640740840941041112 unmodified lines424425426427428429491 unmodified lines921922923924925926404 unmodified linesraise RuntimeError(f"Download failed after {max_retries} attempts:\n{last_err}")# ββ Step 2: Video info ββββββββββββββββββββββββββββββββββββββββββββββββββββββββdef get_video_info(video_path: Path):"""Return (duration, width, height) via ffprobe JSON."""r = subprocess.run(12 unmodified linesreturn duration, width, height# ββ Step 3: Audio energy analysis ββββββββββββββββββββββββββββββββββββββββββββdef extract_audio_energy(video_path: Path, duration: float):"""491 unmodified linestry:# 1. Downloadvideo_path = download_video(youtube_url)# 2. Infoduration, width, height = get_video_info(video_path)404 unmodified lines40540640740840941012 unmodified lines423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505491 unmodified lines997998999100010011002100310041005404 unmodified linesraise RuntimeError(f"Download failed after {max_retries} attempts:\n{last_err}")def get_video_info(video_path: Path):"""Return (duration, width, height) via ffprobe JSON."""r = subprocess.run(12 unmodified linesreturn duration, width, heightdef get_youtube_heatmap(video_path: Path, url: str):"""Fetch YouTube's 'Most Replayed' heatmap data using yt-dlp.Returns: [{start_time: float, end_time: float, score: float}] or []"""print("[>>] Fetching YouTube heatmap...", flush=True)# We use the same hardened bypass settings as download_videoinfo_json_path = video_path.with_suffix(".info.json")cmd = ["yt-dlp","--force-ipv4","--ignore-config","--no-cache-dir","--user-agent", _USER_AGENT,"--extractor-args", "youtube:player_client=web,tv,ios;player_skip=web_embedded_check","--remote-components", "ejs:github","--no-check-certificates","--geo-bypass","--add-header", "Accept-Language:en-US,en;q=0.9","--add-header", "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","--write-info-json","--skip-download","-o", str(video_path.with_suffix("")), # This will result in filename.info.jsonurl,]# Add cookies if availablecookies_path = _find_cookies_file()if cookies_path:cmd[1:1] = ["--cookies", cookies_path]# Add Proxy if definedif YTDLP_PROXY:cmd[1:1] = ["--proxy", YTDLP_PROXY]try:# Run yt-dlp to get JSONsubprocess.run(cmd, capture_output=True, text=True, timeout=30)if not info_json_path.exists():print("[!] Heatmap info JSON not found.", flush=True)return []with open(info_json_path, "r", encoding="utf-8") as f:data = json.load(f)heatmap = data.get("heatmap")if not heatmap:print("[!] No heatmap data found in YouTube metadata.", flush=True)return []# Normalize scores to 0.0 - 1.0max_val = max((item.get("value", 0) for item in heatmap), default=1.0)if max_val == 0: max_val = 1.0normalized = []for item in heatmap:normalized.append({"start_time": float(item["start_time"]),"end_time": float(item["end_time"]),"score": round(float(item["value"]) / max_val, 4)})print(f"[OK] Extracted {len(normalized)} heatmap segments.", flush=True)# Cleanuptry: info_json_path.unlink()except: passreturn normalizedexcept Exception as e:print(f"[!] Heatmap extraction error: {e}", flush=True)return []# ββ Step 3: Audio energy analysis ββββββββββββββββββββββββββββββββββββββββββββdef extract_audio_energy(video_path: Path, duration: float):"""491 unmodified linestry:# 1. Downloadvideo_path = download_video(youtube_url)# 1.1 Heatmap (New Viral Signalling)heatmap_data = get_youtube_heatmap(video_path, youtube_url)# 2. Infoduration, width, height = get_video_info(video_path)
server.py+80-1