-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_transcript.py
More file actions
464 lines (372 loc) · 16.2 KB
/
fetch_transcript.py
File metadata and controls
464 lines (372 loc) · 16.2 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env python3
"""
fetch_transcript.py — YouTube transcript fetcher for yt-pull skill.
Usage:
# Single video
python3 fetch_transcript.py <video_id> [--lang en] [--output-dir ./out]
# List videos from a channel
python3 fetch_transcript.py --channel <channel_name_or_url> [--max-videos 10] [--output-dir ./out]
Outputs (single video):
transcript.txt — clean plain text
transcript.json — structured [{text, start, duration}, ...]
metadata.json — video title, channel, date, duration, views, description
Outputs (channel mode):
channel.json — list of [{id, title, upload_date, duration_string, view_count}, ...]
Primary method: youtube-transcript-api (lightweight, returns structured data)
Fallback method: yt-dlp (heavier, needs VTT cleaning, but more robust)
"""
import argparse
import json
import os
import re
import subprocess
import sys
import shutil
from pathlib import Path
VENV_DIR = Path("/tmp/yt-pull-venv")
def _get_venv_python() -> str | None:
"""Return the venv python path if the venv exists and is usable."""
venv_python = VENV_DIR / "bin" / "python3"
if venv_python.exists():
return str(venv_python)
return None
def _create_venv() -> str | None:
"""Create a persistent venv at VENV_DIR. Returns python path or None."""
if _get_venv_python():
return _get_venv_python()
try:
subprocess.run(
[sys.executable, "-m", "venv", str(VENV_DIR)],
capture_output=True, timeout=30, check=True,
)
print(f"[deps] Created venv at {VENV_DIR}", file=sys.stderr)
return _get_venv_python()
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
print(f"[deps] Failed to create venv: {e}", file=sys.stderr)
return None
def _pip_install(pip_python: str, package_name: str) -> bool:
"""Run pip install using a specific python interpreter."""
try:
subprocess.run(
[pip_python, "-m", "pip", "install", "--quiet", package_name],
capture_output=True, timeout=120, check=True,
)
return True
except (subprocess.CalledProcessError, FileNotFoundError, subprocess.TimeoutExpired):
return False
def ensure_package(package_name: str, import_name: str = None) -> bool:
"""Try to import a package; if missing, install it (creating a venv if needed)."""
import_name = import_name or package_name
try:
__import__(import_name)
return True
except ImportError:
pass
# If we're already running inside the venv, just pip install directly
if sys.prefix != sys.base_prefix:
if _pip_install(sys.executable, package_name):
return True
# Try pip install into current python (works on permissive systems)
if _pip_install(sys.executable, package_name):
try:
__import__(import_name)
return True
except ImportError:
pass
# Try uv
if shutil.which("uv"):
try:
subprocess.run(
["uv", "pip", "install", package_name],
capture_output=True, timeout=60, check=True,
)
__import__(import_name)
return True
except (subprocess.CalledProcessError, subprocess.TimeoutExpired, ImportError):
pass
# Create a venv and install there, then re-exec this script under the venv python
venv_python = _create_venv()
if venv_python and venv_python != sys.executable:
if _pip_install(venv_python, package_name):
# Re-exec this script under the venv python so imports work
print(f"[deps] Installed {package_name} in venv, re-launching...", file=sys.stderr)
os.execv(venv_python, [venv_python] + sys.argv)
# execv replaces the process — this line is never reached
return False
# ---------------------------------------------------------------------------
# Primary method: youtube-transcript-api
# ---------------------------------------------------------------------------
def fetch_with_transcript_api(video_id: str, lang: str) -> dict | None:
"""
Returns {"entries": [{text, start, duration}, ...], "source": "api"} or None.
"""
if not ensure_package("youtube-transcript-api", "youtube_transcript_api"):
return None
try:
from youtube_transcript_api import YouTubeTranscriptApi
api = YouTubeTranscriptApi()
transcript = api.fetch(video_id, languages=[lang])
entries = []
for entry in transcript:
entries.append({
"text": str(entry.text).strip(),
"start": float(entry.start),
"duration": float(entry.duration),
})
return {"entries": entries, "source": "youtube-transcript-api"}
except Exception as e:
print(f"[youtube-transcript-api] Failed: {e}", file=sys.stderr)
return None
# ---------------------------------------------------------------------------
# Fallback method: yt-dlp
# ---------------------------------------------------------------------------
def fetch_with_ytdlp(video_id: str, lang: str, tmp_dir: str) -> dict | None:
"""
Returns {"entries": [{text, start, duration}, ...], "source": "yt-dlp"} or None.
"""
ytdlp = shutil.which("yt-dlp")
if not ytdlp:
# Try to install
if not ensure_package("yt-dlp"):
return None
ytdlp = shutil.which("yt-dlp")
if not ytdlp:
# Might be installed but not on PATH — try via python module
ytdlp = None
url = f"https://www.youtube.com/watch?v={video_id}"
out_template = os.path.join(tmp_dir, "%(id)s")
# Try auto-subs first, then manual subs
for sub_flag in ["--write-auto-sub", "--write-sub"]:
cmd = [
ytdlp or sys.executable, *([] if ytdlp else ["-m", "yt_dlp"]),
sub_flag, "--sub-lang", lang,
"--skip-download", "--sub-format", "vtt",
"-o", out_template, url,
]
try:
subprocess.run(cmd, capture_output=True, timeout=120, check=True)
except (subprocess.CalledProcessError, FileNotFoundError, subprocess.TimeoutExpired):
continue
# Find the VTT file
for f in Path(tmp_dir).glob(f"{video_id}*.vtt"):
entries = parse_vtt(f.read_text())
if entries:
return {"entries": entries, "source": "yt-dlp (auto-subs)" if "auto" in sub_flag else "yt-dlp (manual-subs)"}
return None
def parse_vtt(vtt_text: str) -> list[dict]:
"""Parse VTT content into structured entries with timestamps."""
entries = []
lines = vtt_text.split("\n")
current_start = None
current_duration = 0.0
current_text_lines = []
for line in lines:
# Skip header lines
if line.startswith("WEBVTT") or line.startswith("Kind:") or line.startswith("Language:") or line.startswith("NOTE"):
continue
# Timestamp line
ts_match = re.match(r'(\d{2}:\d{2}:\d{2}\.\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2}\.\d{3})', line)
if ts_match:
# Save previous entry
if current_start is not None and current_text_lines:
text = clean_subtitle_text(" ".join(current_text_lines))
if text:
entries.append({"text": text, "start": current_start, "duration": current_duration})
current_start = parse_timestamp(ts_match.group(1))
end = parse_timestamp(ts_match.group(2))
current_duration = round(end - current_start, 3)
current_text_lines = []
continue
# Skip position/alignment lines
if re.match(r'^(align|position|line|size):', line.strip()):
continue
# Content line
stripped = line.strip()
if stripped:
current_text_lines.append(stripped)
# Save last entry
if current_start is not None and current_text_lines:
text = clean_subtitle_text(" ".join(current_text_lines))
if text:
entries.append({"text": text, "start": current_start, "duration": current_duration})
return entries
def parse_timestamp(ts: str) -> float:
"""Convert HH:MM:SS.mmm to seconds."""
parts = ts.split(":")
return int(parts[0]) * 3600 + int(parts[1]) * 60 + float(parts[2])
def clean_subtitle_text(text: str) -> str:
"""Strip HTML tags, inline timestamps, and normalize whitespace."""
text = re.sub(r'<[^>]+>', '', text) # HTML tags
text = re.sub(r'\s+', ' ', text).strip() # collapse whitespace
return text
# ---------------------------------------------------------------------------
# Metadata via yt-dlp
# ---------------------------------------------------------------------------
def fetch_metadata(video_id: str) -> dict | None:
"""Fetch video metadata via yt-dlp --dump-json."""
ytdlp = shutil.which("yt-dlp")
if not ytdlp:
ensure_package("yt-dlp")
ytdlp = shutil.which("yt-dlp")
url = f"https://www.youtube.com/watch?v={video_id}"
cmd = [
ytdlp or sys.executable, *([] if ytdlp else ["-m", "yt_dlp"]),
"--dump-json", "--skip-download", url,
]
try:
result = subprocess.run(cmd, capture_output=True, timeout=60, text=True, check=True)
d = json.loads(result.stdout)
return {
"title": d.get("title", ""),
"channel": d.get("channel", d.get("uploader", "")),
"upload_date": d.get("upload_date", ""),
"duration_string": d.get("duration_string", ""),
"description": d.get("description", "")[:500],
"view_count": d.get("view_count", 0),
}
except Exception as e:
print(f"[metadata] Failed: {e}", file=sys.stderr)
return None
# ---------------------------------------------------------------------------
# Text assembly
# ---------------------------------------------------------------------------
def entries_to_plain_text(entries: list[dict]) -> str:
"""Convert structured entries to clean, deduplicated plain text with paragraph breaks."""
seen_texts = []
for e in entries:
text = e["text"]
# Deduplicate consecutive identical lines
if not seen_texts or text != seen_texts[-1]:
seen_texts.append(text)
full_text = " ".join(seen_texts)
full_text = re.sub(r'\s+', ' ', full_text).strip()
# Break into paragraphs every ~4 sentences
sentences = re.split(r'(?<=[.!?])\s+', full_text)
paragraphs = []
current = []
for s in sentences:
current.append(s)
if len(current) >= 4:
paragraphs.append(" ".join(current))
current = []
if current:
paragraphs.append(" ".join(current))
return "\n\n".join(paragraphs)
# ---------------------------------------------------------------------------
# Channel listing
# ---------------------------------------------------------------------------
def resolve_channel_url(channel_input: str) -> str:
"""Resolve a channel name/handle/URL to a yt-dlp-compatible URL."""
# Already a full URL
if channel_input.startswith("http"):
# Ensure it ends with /videos for listing
url = channel_input.rstrip("/")
if not url.endswith("/videos"):
url += "/videos"
return url
# Handle with @ prefix
name = channel_input.lstrip("@")
return f"https://www.youtube.com/@{name}/videos"
def list_channel_videos(channel_input: str, max_videos: int = 10) -> list[dict] | None:
"""
List recent videos from a YouTube channel.
Returns [{id, title, upload_date, duration_string, view_count, url}, ...] or None.
"""
ytdlp = shutil.which("yt-dlp")
if not ytdlp:
ensure_package("yt-dlp")
ytdlp = shutil.which("yt-dlp")
channel_url = resolve_channel_url(channel_input)
print(f"Listing up to {max_videos} videos from {channel_url}...", file=sys.stderr)
cmd = [
ytdlp or sys.executable, *([] if ytdlp else ["-m", "yt_dlp"]),
"--flat-playlist", "--dump-json",
"--playlist-end", str(max_videos),
channel_url,
]
try:
result = subprocess.run(cmd, capture_output=True, timeout=120, text=True, check=True)
except (subprocess.CalledProcessError, FileNotFoundError, subprocess.TimeoutExpired) as e:
print(f"[channel] Failed to list videos: {e}", file=sys.stderr)
return None
videos = []
for line in result.stdout.strip().split("\n"):
if not line.strip():
continue
try:
d = json.loads(line)
videos.append({
"id": d.get("id", ""),
"title": d.get("title", ""),
"upload_date": d.get("upload_date", ""),
"duration_string": d.get("duration_string", d.get("duration", "")),
"view_count": d.get("view_count", 0),
"url": d.get("url", f"https://www.youtube.com/watch?v={d.get('id', '')}"),
})
except json.JSONDecodeError:
continue
return videos if videos else None
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main():
parser = argparse.ArgumentParser(description="Fetch YouTube transcript or list channel videos")
parser.add_argument("video_id", nargs="?", default=None, help="YouTube video ID (11 chars)")
parser.add_argument("--channel", default=None, help="Channel name, @handle, or URL to list videos from")
parser.add_argument("--max-videos", type=int, default=10, help="Max videos to list from channel (default: 10)")
parser.add_argument("--lang", default="en", help="Language code (default: en)")
parser.add_argument("--output-dir", default=".", help="Output directory")
args = parser.parse_args()
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# --- Channel mode: list videos ---
if args.channel:
videos = list_channel_videos(args.channel, args.max_videos)
if not videos:
print("ERROR: Could not list videos from channel.", file=sys.stderr)
sys.exit(1)
print(f"Found {len(videos)} videos", file=sys.stderr)
channel_path = output_dir / "channel.json"
channel_path.write_text(json.dumps(videos, indent=2, ensure_ascii=False))
print(f"Wrote {channel_path}", file=sys.stderr)
# Print summary to stdout for Claude to read
print(json.dumps(videos, indent=2, ensure_ascii=False))
return
# --- Single video mode ---
if not args.video_id:
parser.error("Either a video_id or --channel is required")
video_id = args.video_id
lang = args.lang
tmp_dir = f"/tmp/yt-pull-{video_id}"
os.makedirs(tmp_dir, exist_ok=True)
# --- Fetch transcript ---
print(f"Fetching transcript for {video_id} (lang={lang})...", file=sys.stderr)
result = fetch_with_transcript_api(video_id, lang)
if not result:
print("Primary method failed, trying yt-dlp fallback...", file=sys.stderr)
result = fetch_with_ytdlp(video_id, lang, tmp_dir)
if not result:
print("ERROR: Could not fetch transcript with any method.", file=sys.stderr)
sys.exit(1)
entries = result["entries"]
source = result["source"]
print(f"Got {len(entries)} entries via {source}", file=sys.stderr)
# --- Fetch metadata ---
print("Fetching metadata...", file=sys.stderr)
metadata = fetch_metadata(video_id)
# --- Write outputs ---
plain_text = entries_to_plain_text(entries)
txt_path = output_dir / "transcript.txt"
txt_path.write_text(plain_text)
print(f"Wrote {txt_path} ({len(plain_text)} chars, {len(plain_text.split())} words)", file=sys.stderr)
json_path = output_dir / "transcript.json"
json_path.write_text(json.dumps(entries, indent=2, ensure_ascii=False))
print(f"Wrote {json_path} ({len(entries)} entries)", file=sys.stderr)
if metadata:
meta_path = output_dir / "metadata.json"
meta_path.write_text(json.dumps(metadata, indent=2, ensure_ascii=False))
print(f"Wrote {meta_path}", file=sys.stderr)
# --- Print transcript to stdout for Claude to read ---
print(plain_text)
if __name__ == "__main__":
main()