Spaces:
Sleeping
Sleeping
Download transcript_utils.py from Samir433/YoutubeRAG: direct link, hf CLI and curl.
- Browser
- Download file 910 Bytes
-
https://huggingface.co/spaces/Samir433/YoutubeRAG/resolve/main/transcript_utils.py
- Command line
-
hf download hf://spaces/Samir433/YoutubeRAG/transcript_utils.py
-
curl -L -o transcript_utils.py https://huggingface.co/spaces/Samir433/YoutubeRAG/resolve/main/transcript_utils.py
910 Bytes
| import re | |
| from youtube_transcript_api import ( | |
| YouTubeTranscriptApi, | |
| TranscriptsDisabled, | |
| NoTranscriptFound | |
| ) | |
| def extract_video_id(youtube_url: str) -> str: | |
| """Extract the 11‑char YouTube ID from a URL via regex.""" | |
| pattern = r"(?:v=|youtu\.be/|/)([0-9A-Za-z_-]{11})" | |
| match = re.search(pattern, youtube_url) | |
| return match.group(1) if match else None | |
| def fetch_transcript(video_id: str, languages: list = ["en", "hi"]) -> str: | |
| """ | |
| Instantiate the YouTubeTranscriptApi and call `.fetch()` per language. | |
| Returns the concatenated text or None. | |
| """ | |
| api = YouTubeTranscriptApi() | |
| for lang in languages: | |
| try: | |
| snippets = api.fetch(video_id, languages=[lang]) | |
| return " ".join(s.text for s in snippets) | |
| except (TranscriptsDisabled, NoTranscriptFound, Exception): | |
| continue | |
| return None | |