Ubuntu commited on
Commit
e9a46be
·
1 Parent(s): ff3234b

change downloading

Browse files
Files changed (2) hide show
  1. app.py +5 -19
  2. requirements.txt +1 -2
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import requests
3
- from yt_dlp import YoutubeDL
4
  import os
5
  import tempfile
6
  from typing import Optional
@@ -14,29 +14,15 @@ 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
- # Updated command to use a different method for downloading
18
- yt_dlp_command = [
19
- "yt-dlp",
20
- "-f", "bestaudio/best",
21
- "-x", # Extract audio
22
- "--audio-format", "mp3",
23
- "-o", os.path.join(output_dir, "%(title)s.%(ext)s"), # Changed output filename to use title
24
- url
25
- ]
26
-
27
  try:
28
- print(' '.join(yt_dlp_command))
29
- result = subprocess.run(yt_dlp_command, capture_output=True, text=True, check=True)
30
-
31
- # Get the output filepath from the last line of stdout
32
- audio_file = os.path.join(output_dir, f"{result.stdout.splitlines()[-1].strip()}.mp3") # Adjusted to match new output
33
 
34
  print(f"Successfully downloaded: {audio_file}")
35
  return audio_file
36
- except subprocess.CalledProcessError as e:
37
- raise Exception(f"Error downloading YouTube audio: {e.stderr}")
38
  except Exception as e:
39
- raise Exception(f"Unexpected error: {str(e)}")
40
 
41
 
42
  def run_asr(audio_file, youtube_url):
 
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
 
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):
requirements.txt CHANGED
@@ -1,2 +1 @@
1
- yt-dlp
2
-
 
1
+ pytube