ytdlp_subtitle / fetchYoutubeSubtitle.py
lanbogao's picture
Remove unused and check subtitle_url.
de8dee7
raw
history blame
1.97 kB
import json
from typing import Optional
import yt_dlp
def getVttUrlFromSubtitles(item, lang='en', vttType="vtt"):
langs = item.keys()
key = lang if lang in langs else ('en' if 'en' in langs else list(langs)[0] )
for subtitle in item[key]:
if(subtitle.get("ext") == vttType):
return subtitle.get("url")
return None
async def fetchSubtitle(url: str, lang: Optional[str] = 'en', vttType="vtt") -> Optional[str]:
ydl_opts = {
"writesubtitles": True,
"allsubtitles": True,
"subtitleslangs": [lang] if lang else [],
"skip_download": True,
"socket_timeout": 20
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=False)
if info_dict.get("subtitles"):
# get first available subtitle
subtitle_url = getVttUrlFromSubtitles(info_dict.get("subtitles"), lang, vttType)
if subtitle_url:
with ydl.urlopen(subtitle_url) as subtitle:
return subtitle.read().decode()
if info_dict.get("automatic_captions"):
subtitle_url = getVttUrlFromSubtitles(info_dict.get("automatic_captions"), lang, vttType)
if subtitle_url:
with ydl.urlopen(subtitle_url) as subtitle:
return subtitle.read().decode()
return None
async def fetchSubtitleUrls(url: str) -> json:
ydl_opts = {
"writesubtitles": True,
"allsubtitles": True,
"skip_download": True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=False)
if info_dict.get("subtitles"):
langs = info_dict.get("subtitles").keys()
if not (len(langs) == 1 and "live_chat" in langs):
return info_dict.get("subtitles")
if info_dict.get("automatic_captions"):
return info_dict.get("automatic_captions")
return None