cstr commited on
Commit
5f48e16
·
verified ·
1 Parent(s): 89cebe2
Files changed (1) hide show
  1. app.py +49 -24
app.py CHANGED
@@ -56,6 +56,9 @@ def download_audio(url, method_choice):
56
  if 'youtube.com' in parsed_url.netloc or 'youtu.be' in parsed_url.netloc:
57
  # Use YouTube download methods
58
  audio_file = download_youtube_audio(url, method_choice)
 
 
 
59
  else:
60
  # Use direct download methods
61
  audio_file = download_direct_audio(url, method_choice)
@@ -65,6 +68,27 @@ def download_audio(url, method_choice):
65
  except Exception as e:
66
  logging.error(f"Error downloading audio: {str(e)}")
67
  return f"Error: {str(e)}", False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  def download_youtube_audio(url, method_choice):
70
  """
@@ -113,38 +137,36 @@ def youtube_dl_method(url):
113
  logging.error(f"Error in youtube_dl_method: {str(e)}")
114
  return None
115
 
116
- def yt_dlp_method(url):
117
  """
118
- Downloads audio using yt-dlp.
119
 
120
  Args:
121
- url (str): The YouTube URL.
122
 
123
  Returns:
124
  str: Path to the downloaded audio file, or None if failed.
125
  """
126
- logging.info("Using yt-dlp method")
 
 
 
 
 
 
 
 
 
 
 
 
127
  try:
128
- ydl_opts = {
129
- 'format': 'bestaudio/best',
130
- 'postprocessors': [{
131
- 'key': 'FFmpegExtractAudio',
132
- 'preferredcodec': 'mp3',
133
- 'preferredquality': '192',
134
- }],
135
- 'outtmpl': '%(id)s.%(ext)s',
136
- 'quiet': True,
137
- 'no_warnings': True,
138
- }
139
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
140
- info = ydl.extract_info(url, download=True)
141
- output_file = ydl.prepare_filename(info)
142
- if output_file.endswith('.webm') or output_file.endswith('.mp4'):
143
- output_file = output_file.rsplit('.', 1)[0] + '.mp3'
144
- logging.info(f"Downloaded YouTube audio: {output_file}")
145
- return output_file
146
  except Exception as e:
147
- logging.error(f"Error in yt_dlp_method: {str(e)}")
148
  return None
149
 
150
  def pytube_method(url):
@@ -252,11 +274,11 @@ def requests_method(url):
252
 
253
  def download_direct_audio(url, method_choice):
254
  """
255
- Downloads audio from a direct URL using the specified method.
256
 
257
  Args:
258
  url (str): The direct URL of the audio file.
259
- method_choice (str): The method to use for downloading ('wget', 'requests').
260
 
261
  Returns:
262
  str: Path to the downloaded audio file, or None if failed.
@@ -265,6 +287,9 @@ def download_direct_audio(url, method_choice):
265
  methods = {
266
  'wget': wget_method,
267
  'requests': requests_method,
 
 
 
268
  }
269
  method = methods.get(method_choice)
270
  if method is None:
 
56
  if 'youtube.com' in parsed_url.netloc or 'youtu.be' in parsed_url.netloc:
57
  # Use YouTube download methods
58
  audio_file = download_youtube_audio(url, method_choice)
59
+ elif parsed_url.scheme == 'rtsp':
60
+ # Use RTSP download methods
61
+ audio_file = download_rtsp_audio(url)
62
  else:
63
  # Use direct download methods
64
  audio_file = download_direct_audio(url, method_choice)
 
68
  except Exception as e:
69
  logging.error(f"Error downloading audio: {str(e)}")
70
  return f"Error: {str(e)}", False
71
+
72
+ def download_rtsp_audio(url):
73
+ """
74
+ Downloads audio from an RTSP URL using FFmpeg.
75
+
76
+ Args:
77
+ url (str): The RTSP URL.
78
+
79
+ Returns:
80
+ str: Path to the downloaded audio file, or None if failed.
81
+ """
82
+ logging.info("Using FFmpeg to download RTSP stream")
83
+ output_file = tempfile.mktemp(suffix='.mp3')
84
+ command = ['ffmpeg', '-i', url, '-acodec', 'libmp3lame', '-ab', '192k', '-y', output_file]
85
+ try:
86
+ subprocess.run(command, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
87
+ logging.info(f"Downloaded RTSP audio to: {output_file}")
88
+ return output_file
89
+ except Exception as e:
90
+ logging.error(f"Error downloading RTSP audio: {str(e)}")
91
+ return None
92
 
93
  def download_youtube_audio(url, method_choice):
94
  """
 
137
  logging.error(f"Error in youtube_dl_method: {str(e)}")
138
  return None
139
 
140
+ def yt_dlp_direct_method(url):
141
  """
142
+ Downloads audio using yt-dlp (supports various protocols and sites).
143
 
144
  Args:
145
+ url (str): The URL of the audio or webpage containing audio.
146
 
147
  Returns:
148
  str: Path to the downloaded audio file, or None if failed.
149
  """
150
+ logging.info("Using yt-dlp direct method")
151
+ output_file = tempfile.mktemp(suffix='.mp3')
152
+ ydl_opts = {
153
+ 'format': 'bestaudio/best',
154
+ 'outtmpl': output_file,
155
+ 'quiet': True,
156
+ 'no_warnings': True,
157
+ 'postprocessors': [{
158
+ 'key': 'FFmpegExtractAudio',
159
+ 'preferredcodec': 'mp3',
160
+ 'preferredquality': '192',
161
+ }],
162
+ }
163
  try:
 
 
 
 
 
 
 
 
 
 
 
164
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
165
+ ydl.download([url])
166
+ logging.info(f"Downloaded audio to: {output_file}")
167
+ return output_file
 
 
 
168
  except Exception as e:
169
+ logging.error(f"Error in yt_dlp_direct_method: {str(e)}")
170
  return None
171
 
172
  def pytube_method(url):
 
274
 
275
  def download_direct_audio(url, method_choice):
276
  """
277
+ Downloads audio from a direct URL or podcast URL using the specified method.
278
 
279
  Args:
280
  url (str): The direct URL of the audio file.
281
+ method_choice (str): The method to use for downloading ('wget', 'requests', 'yt-dlp', 'ffmpeg', 'aria2').
282
 
283
  Returns:
284
  str: Path to the downloaded audio file, or None if failed.
 
287
  methods = {
288
  'wget': wget_method,
289
  'requests': requests_method,
290
+ 'yt-dlp': yt_dlp_direct_method,
291
+ 'ffmpeg': ffmpeg_method,
292
+ 'aria2': aria2_method,
293
  }
294
  method = methods.get(method_choice)
295
  if method is None: