lanbogao commited on
Commit
60c9382
1 Parent(s): 2c6ebc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -9,6 +9,14 @@ import yt_dlp
9
  langs = ["None"] + sorted(list(whisper.tokenizer.LANGUAGES.values()))
10
  model_size = list(whisper._MODELS.keys())
11
 
 
 
 
 
 
 
 
 
12
  def get_subtitle(url, lang='en'):
13
  # Download subtitles if available
14
  ydl_opts = {
@@ -23,9 +31,10 @@ def get_subtitle(url, lang='en'):
23
  if video_id is None:
24
  return None
25
  subtitle_file = f"{video_id}.{lang}.vtt"
26
- if os.path.exists(subtitle_file):
27
- subtitle_text = subprocess.check_output(['webvtt-to-text', subtitle_file]).decode('utf-8')
28
- return subtitle_text
 
29
  return None
30
 
31
  def download_audio(video_url, quality: str = '128', speed: float = None):
 
9
  langs = ["None"] + sorted(list(whisper.tokenizer.LANGUAGES.values()))
10
  model_size = list(whisper._MODELS.keys())
11
 
12
+
13
+ @app.get("/subtitle")
14
+ async def get_subtitle(url: str):
15
+ # Download the subtitle with download_subtitle()
16
+ subtitle_url = download_subtitle(url)
17
+ # Stream the subtitle as a response
18
+ return StreamingResponse(requests.get(subtitle_url, stream=True).iter_content(chunk_size=1024))
19
+
20
  def get_subtitle(url, lang='en'):
21
  # Download subtitles if available
22
  ydl_opts = {
 
31
  if video_id is None:
32
  return None
33
  subtitle_file = f"{video_id}.{lang}.vtt"
34
+ with open(subtitle_file, 'r') as f:
35
+ subtitle_content = f.read()
36
+ subtitle_content = re.sub(r"<[^>]+>", "", subtitle_content)
37
+ return subtitle_content
38
  return None
39
 
40
  def download_audio(video_url, quality: str = '128', speed: float = None):