Ubuntu commited on
Commit
6b81e0d
1 Parent(s): e9a46be

update the download

Browse files
Files changed (1) hide show
  1. app.py +12 -7
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import gradio as gr
2
  import requests
3
- from pytube import YouTube # Import pytube
4
- import os
5
- import tempfile
6
  from typing import Optional
7
  import json
8
  import subprocess
@@ -14,17 +12,24 @@ def download_youtube_audio(url: str, output_dir: Optional[str] = None) -> str:
14
  if output_dir is None:
15
  output_dir = tempfile.gettempdir()
16
 
 
 
 
 
 
 
 
 
17
  try:
18
- yt = YouTube(url) # Create a YouTube object
19
- audio_stream = yt.streams.filter(only_audio=True).first() # Get the first audio stream
20
- audio_file = audio_stream.download(output_path=output_dir, filename=f"{yt.title}.mp3") # Download audio
21
 
22
  print(f"Successfully downloaded: {audio_file}")
23
  return audio_file
24
  except Exception as e:
25
  raise Exception(f"Error downloading YouTube audio: {str(e)}")
26
 
27
-
28
  def run_asr(audio_file, youtube_url):
29
  temp_file = None
30
  try:
 
1
  import gradio as gr
2
  import requests
3
+ import youtube_dl # Import youtube_dl
 
 
4
  from typing import Optional
5
  import json
6
  import subprocess
 
12
  if output_dir is None:
13
  output_dir = tempfile.gettempdir()
14
 
15
+ ydl_opts = {
16
+ 'format': 'bestaudio/best',
17
+ 'extractaudio': True, # Download only audio
18
+ 'audioformat': 'mp3', # Convert to mp3
19
+ 'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'), # Output filename
20
+ 'noplaylist': True, # Only download single video, not playlist
21
+ }
22
+
23
  try:
24
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
25
+ info_dict = ydl.extract_info(url, download=True) # Download audio
26
+ audio_file = os.path.join(output_dir, f"{info_dict['title']}.mp3") # Construct the file path
27
 
28
  print(f"Successfully downloaded: {audio_file}")
29
  return audio_file
30
  except Exception as e:
31
  raise Exception(f"Error downloading YouTube audio: {str(e)}")
32
 
 
33
  def run_asr(audio_file, youtube_url):
34
  temp_file = None
35
  try: