cstr commited on
Commit
22f720d
1 Parent(s): f1aba6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -15
app.py CHANGED
@@ -45,11 +45,11 @@ logging.info(f"Using device: {device}")
45
  def download_audio(url, method_choice):
46
  """
47
  Downloads audio from a given URL using the specified method.
48
-
49
  Args:
50
  url (str): The URL of the audio.
51
  method_choice (str): The method to use for downloading audio.
52
-
53
  Returns:
54
  tuple: (path to the downloaded audio file, is_temp_file), or (None, False) if failed.
55
  """
@@ -58,20 +58,28 @@ def download_audio(url, method_choice):
58
  try:
59
  if 'youtube.com' in parsed_url.netloc or 'youtu.be' in parsed_url.netloc:
60
  audio_file = download_youtube_audio(url, method_choice)
 
 
 
 
61
  elif parsed_url.scheme == 'rtsp':
62
  audio_file = download_rtsp_audio(url)
 
 
 
 
63
  else:
64
  audio_file = download_direct_audio(url, method_choice)
65
-
66
- if not audio_file or not os.path.exists(audio_file):
67
- error_msg = f"Failed to download audio from {url} using method {method_choice}"
68
- logging.error(error_msg)
69
- return None, False
70
  return audio_file, True
71
  except Exception as e:
72
  error_msg = f"Error downloading audio from {url} using method {method_choice}: {str(e)}"
73
  logging.error(error_msg)
74
  return None, False
 
75
 
76
  def download_youtube_audio(url, method_choice):
77
  """
@@ -96,13 +104,13 @@ def download_youtube_audio(url, method_choice):
96
  logging.error(f"Error downloading using {method_choice}: {str(e)}")
97
  return None
98
 
99
- def yt_dlp_method(url):
100
  """
101
  Downloads YouTube audio using yt-dlp and saves it to a temporary file.
102
-
103
  Args:
104
  url (str): The YouTube URL.
105
-
106
  Returns:
107
  str: Path to the downloaded audio file, or None if failed.
108
  """
@@ -118,20 +126,51 @@ def yt_dlp_method(url):
118
  'preferredquality': '192',
119
  }],
120
  'quiet': False,
121
- 'no_warnings': True,
122
- 'logger': logging.getLogger(), # Capture yt-dlp logs
 
123
  }
124
  try:
125
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
126
  info = ydl.extract_info(url, download=True)
 
 
 
127
  output_file = ydl.prepare_filename(info)
128
  output_file = os.path.splitext(output_file)[0] + '.mp3'
129
- logging.info(f"Downloaded YouTube audio: {output_file}")
130
- return output_file
 
 
 
 
 
131
  except Exception as e:
132
  logging.error(f"yt-dlp failed to download audio: {str(e)}")
133
  return None
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  def pytube_method(url):
136
  """
137
  Downloads audio from a YouTube URL using pytube and saves it to a temporary file.
@@ -460,7 +499,7 @@ def transcribe_audio(input_source, pipeline_type, model_id, dtype, batch_size, d
460
  # Input source is a URL
461
  audio_path, is_temp_file = download_audio(input_source, download_method)
462
  if not audio_path:
463
- error_msg = f"Error downloading audio from {input_source} using method {download_method}"
464
  logging.error(error_msg)
465
  yield verbose_messages + error_msg, "", None
466
  return
 
45
  def download_audio(url, method_choice):
46
  """
47
  Downloads audio from a given URL using the specified method.
48
+
49
  Args:
50
  url (str): The URL of the audio.
51
  method_choice (str): The method to use for downloading audio.
52
+
53
  Returns:
54
  tuple: (path to the downloaded audio file, is_temp_file), or (None, False) if failed.
55
  """
 
58
  try:
59
  if 'youtube.com' in parsed_url.netloc or 'youtu.be' in parsed_url.netloc:
60
  audio_file = download_youtube_audio(url, method_choice)
61
+ if not audio_file:
62
+ error_msg = f"Failed to download audio from {url} using method {method_choice}. Ensure yt-dlp is up to date."
63
+ logging.error(error_msg)
64
+ return None, False
65
  elif parsed_url.scheme == 'rtsp':
66
  audio_file = download_rtsp_audio(url)
67
+ if not audio_file:
68
+ error_msg = f"Failed to download RTSP audio from {url}"
69
+ logging.error(error_msg)
70
+ return None, False
71
  else:
72
  audio_file = download_direct_audio(url, method_choice)
73
+ if not audio_file:
74
+ error_msg = f"Failed to download audio from {url} using method {method_choice}"
75
+ logging.error(error_msg)
76
+ return None, False
 
77
  return audio_file, True
78
  except Exception as e:
79
  error_msg = f"Error downloading audio from {url} using method {method_choice}: {str(e)}"
80
  logging.error(error_msg)
81
  return None, False
82
+
83
 
84
  def download_youtube_audio(url, method_choice):
85
  """
 
104
  logging.error(f"Error downloading using {method_choice}: {str(e)}")
105
  return None
106
 
107
+ ef yt_dlp_method(url):
108
  """
109
  Downloads YouTube audio using yt-dlp and saves it to a temporary file.
110
+
111
  Args:
112
  url (str): The YouTube URL.
113
+
114
  Returns:
115
  str: Path to the downloaded audio file, or None if failed.
116
  """
 
126
  'preferredquality': '192',
127
  }],
128
  'quiet': False,
129
+ 'no_warnings': False,
130
+ 'logger': MyLogger(), # Use a custom logger to capture yt-dlp logs
131
+ 'progress_hooks': [my_hook], # Hook to capture download progress and errors
132
  }
133
  try:
134
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
135
  info = ydl.extract_info(url, download=True)
136
+ if 'entries' in info:
137
+ # Can be a playlist or a list of videos
138
+ info = info['entries'][0]
139
  output_file = ydl.prepare_filename(info)
140
  output_file = os.path.splitext(output_file)[0] + '.mp3'
141
+ if os.path.exists(output_file):
142
+ logging.info(f"Downloaded YouTube audio: {output_file}")
143
+ return output_file
144
+ else:
145
+ error_msg = "yt-dlp did not produce an output file."
146
+ logging.error(error_msg)
147
+ return None
148
  except Exception as e:
149
  logging.error(f"yt-dlp failed to download audio: {str(e)}")
150
  return None
151
 
152
+ class MyLogger(object):
153
+ """
154
+ Custom logger for yt-dlp to capture logs and errors.
155
+ """
156
+ def debug(self, msg):
157
+ logging.debug(msg)
158
+ def info(self, msg):
159
+ logging.info(msg)
160
+ def warning(self, msg):
161
+ logging.warning(msg)
162
+ def error(self, msg):
163
+ logging.error(msg)
164
+
165
+ def my_hook(d):
166
+ """
167
+ Hook function to capture yt-dlp download progress and errors.
168
+ """
169
+ if d['status'] == 'finished':
170
+ logging.info('Download finished, now converting...')
171
+ elif d['status'] == 'error':
172
+ logging.error(f"Download error: {d['filename']}")
173
+
174
  def pytube_method(url):
175
  """
176
  Downloads audio from a YouTube URL using pytube and saves it to a temporary file.
 
499
  # Input source is a URL
500
  audio_path, is_temp_file = download_audio(input_source, download_method)
501
  if not audio_path:
502
+ error_msg = f"Error downloading audio from {input_source} using method {download_method}. Check logs for details."
503
  logging.error(error_msg)
504
  yield verbose_messages + error_msg, "", None
505
  return