razhan commited on
Commit
21d67c0
·
verified ·
1 Parent(s): 3eeb914

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -167,6 +167,7 @@ import torch
167
 
168
  import gradio as gr
169
  import yt_dlp as youtube_dl
 
170
  from transformers import pipeline
171
  from transformers.pipelines.audio_utils import ffmpeg_read
172
 
@@ -206,7 +207,7 @@ def _return_yt_html_embed(yt_url):
206
  return HTML_str
207
 
208
  def download_yt_audio(yt_url, filename):
209
- info_loader = youtube_dl.YoutubeDL()
210
 
211
  # try:
212
  # info = info_loader.extract_info(yt_url, download=False)
@@ -228,20 +229,24 @@ def download_yt_audio(yt_url, filename):
228
  # file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
229
  # raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
230
 
231
- ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
232
 
233
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
234
- try:
235
- ydl.download([yt_url])
236
- except youtube_dl.utils.ExtractorError as err:
237
- raise gr.Error(str(err))
 
 
 
238
 
239
  @spaces.GPU
240
  def yt_transcribe(yt_url, task="transcribe", max_filesize=75.0):
241
  html_embed_str = _return_yt_html_embed(yt_url)
242
 
243
  with tempfile.TemporaryDirectory() as tmpdirname:
244
- filepath = os.path.join(tmpdirname, "video.mp4")
 
245
  download_yt_audio(yt_url, filepath)
246
  with open(filepath, "rb") as f:
247
  inputs = f.read()
 
167
 
168
  import gradio as gr
169
  import yt_dlp as youtube_dl
170
+ import pytube as pt
171
  from transformers import pipeline
172
  from transformers.pipelines.audio_utils import ffmpeg_read
173
 
 
207
  return HTML_str
208
 
209
  def download_yt_audio(yt_url, filename):
210
+ # info_loader = youtube_dl.YoutubeDL()
211
 
212
  # try:
213
  # info = info_loader.extract_info(yt_url, download=False)
 
229
  # file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
230
  # raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
231
 
232
+ # ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
233
 
234
+ # with youtube_dl.YoutubeDL(ydl_opts) as ydl:
235
+ # try:
236
+ # ydl.download([yt_url])
237
+ # except youtube_dl.utils.ExtractorError as err:
238
+ # raise gr.Error(str(err))
239
+ yt = pt.YouTube(yt_url)
240
+ stream = yt.streams.filter(only_audio=True)[0]
241
+ stream.download(filename=filename)
242
 
243
  @spaces.GPU
244
  def yt_transcribe(yt_url, task="transcribe", max_filesize=75.0):
245
  html_embed_str = _return_yt_html_embed(yt_url)
246
 
247
  with tempfile.TemporaryDirectory() as tmpdirname:
248
+ # filepath = os.path.join(tmpdirname, "video.mp4")
249
+ filepath = os.path.join(tmpdirname, "audio.mp3")
250
  download_yt_audio(yt_url, filepath)
251
  with open(filepath, "rb") as f:
252
  inputs = f.read()