lanbogao commited on
Commit
3965709
1 Parent(s): 6e687fe

Add return duration if no subtitle.

Browse files
Files changed (1) hide show
  1. fetchYoutubeSubtitle.py +7 -5
fetchYoutubeSubtitle.py CHANGED
@@ -49,12 +49,13 @@ async def fetchSubtitlebyType(url: str, lang: Optional[str] = 'en', subType="vtt
49
  }
50
 
51
  title = "unknow"
 
52
  try:
53
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
54
  info_dict = ydl.extract_info(url, download=False)
55
  title = info_dict.get("title", "unknow")
56
- duration = info_dict.get("duration")
57
- duration = str(duration) if duration else ""
58
  if info_dict.get("extractor") == "youtube" and subType == "srt":
59
  subType = "xml"
60
 
@@ -68,7 +69,7 @@ async def fetchSubtitlebyType(url: str, lang: Optional[str] = 'en', subType="vtt
68
  return {"title": title, "duration": duration,"subtitle": subtitle}
69
  except Exception as e:
70
  return {"error": str(e)}
71
- return {"title": title,"error": "No subtitles"}
72
 
73
  def float_to_srt_time_format(d: float) -> str:
74
  """Convert decimal durations into proper srt format.
@@ -116,12 +117,13 @@ async def fetchSubtitleUrls(url: str) -> json:
116
  "skip_download": True,
117
  }
118
  title = "unknow"
 
119
  try:
120
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
121
  info_dict = ydl.extract_info(url, download=False)
122
  title = info_dict.get("title", "unknow")
123
- duration = info_dict.get("duration")
124
- duration = str(duration) if duration else ""
125
  if info_dict.get("subtitles"):
126
  langs = info_dict.get("subtitles").keys()
127
  if not (len(langs) == 1 and "live_chat" in langs):
 
49
  }
50
 
51
  title = "unknow"
52
+ duration = ""
53
  try:
54
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
55
  info_dict = ydl.extract_info(url, download=False)
56
  title = info_dict.get("title", "unknow")
57
+ seconds = info_dict.get("duration")
58
+ duration = str(seconds) if seconds else ""
59
  if info_dict.get("extractor") == "youtube" and subType == "srt":
60
  subType = "xml"
61
 
 
69
  return {"title": title, "duration": duration,"subtitle": subtitle}
70
  except Exception as e:
71
  return {"error": str(e)}
72
+ return {"title": title, "duration": duration, "error": "No subtitles"}
73
 
74
  def float_to_srt_time_format(d: float) -> str:
75
  """Convert decimal durations into proper srt format.
 
117
  "skip_download": True,
118
  }
119
  title = "unknow"
120
+ duration = ""
121
  try:
122
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
123
  info_dict = ydl.extract_info(url, download=False)
124
  title = info_dict.get("title", "unknow")
125
+ seconds = info_dict.get("duration")
126
+ duration = str(seconds) if seconds else ""
127
  if info_dict.get("subtitles"):
128
  langs = info_dict.get("subtitles").keys()
129
  if not (len(langs) == 1 and "live_chat" in langs):