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

Add duration(seconds).

Browse files
Files changed (1) hide show
  1. fetchYoutubeSubtitle.py +9 -6
fetchYoutubeSubtitle.py CHANGED
@@ -53,6 +53,8 @@ async def fetchSubtitlebyType(url: str, lang: Optional[str] = 'en', subType="vtt
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
  if info_dict.get("extractor") == "youtube" and subType == "srt":
57
  subType = "xml"
58
 
@@ -62,8 +64,8 @@ async def fetchSubtitlebyType(url: str, lang: Optional[str] = 'en', subType="vtt
62
  if subtitle_url:
63
  with ydl.urlopen(subtitle_url) as response:
64
  subtitle = xml_caption_to_srt(response.read().decode()) if subType == "xml" else response.read().decode()
65
- print("url{}, title:{} len(subtitle): {}".format(url, title, len(subtitle)))
66
- return {"title": title, "subtitle": subtitle}
67
  except Exception as e:
68
  return {"error": str(e)}
69
  return {"title": title,"error": "No subtitles"}
@@ -118,13 +120,14 @@ async def fetchSubtitleUrls(url: str) -> json:
118
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
119
  info_dict = ydl.extract_info(url, download=False)
120
  title = info_dict.get("title", "unknow")
121
-
 
122
  if info_dict.get("subtitles"):
123
  langs = info_dict.get("subtitles").keys()
124
  if not (len(langs) == 1 and "live_chat" in langs):
125
- return {"title": info_dict.get("title", "unknow"), "subtitles": info_dict.get("subtitles")}
126
  if info_dict.get("automatic_captions"):
127
- return {"title": info_dict.get("title", "unknow"), "subtitles": info_dict.get("automatic_captions")}
128
  except Exception as e:
129
  return {"error": str(e)}
130
- return {"title": title,"error": "No subtitles"}
 
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
 
 
64
  if subtitle_url:
65
  with ydl.urlopen(subtitle_url) as response:
66
  subtitle = xml_caption_to_srt(response.read().decode()) if subType == "xml" else response.read().decode()
67
+ print("url{}, title:{}, duration:{} len(subtitle): {}".format(url, title, duration, len(subtitle)))
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"}
 
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):
128
+ return {"title": title, "duration": duration, "subtitles": info_dict.get("subtitles")}
129
  if info_dict.get("automatic_captions"):
130
+ return {"title": title, "duration": duration, "subtitles": info_dict.get("automatic_captions")}
131
  except Exception as e:
132
  return {"error": str(e)}
133
+ return {"title": title, "duration": duration, "error": "No subtitles"}