OmarHusseinZaki commited on
Commit
9cb35e7
·
1 Parent(s): ab6db1e

replace youtube url with an invidious instance

Browse files
Files changed (1) hide show
  1. main.py +7 -3
main.py CHANGED
@@ -80,8 +80,12 @@ def download_audio_bytes(youtube_url: str) -> bytes:
80
  """
81
  Downloads the best audio-only format from a YouTube URL using yt-dlp and returns the raw audio data as bytes.
82
  """
 
 
 
 
83
 
84
- print(f"Attempting to download audio for: {youtube_url}")
85
  ydl_opts = {
86
  'format': 'bestaudio/best', # Prioritize best audio-only, fallback to best audio in general
87
  'noplaylist': True, # Don't download playlist if URL is part of one
@@ -107,7 +111,7 @@ def download_audio_bytes(youtube_url: str) -> bytes:
107
  # Or, a better way: get the direct audio URL first
108
 
109
  # --- Simpler & Often Better Approach: Get URL, then download with requests ---
110
- info = ydl.extract_info(youtube_url, download=False) # Get info without downloading yet
111
  best_audio_format = None
112
  for f in info.get('formats', []):
113
  # Look for formats processed by FFmpegExtractAudio or good audio codecs
@@ -128,7 +132,7 @@ def download_audio_bytes(youtube_url: str) -> bytes:
128
  # If you *don't* have ffmpeg in the Dockerfile, the postprocessor might fail here
129
  # Let's try the download anyway, it might work for some native formats
130
  # This path is less reliable without guaranteed ffmpeg.
131
- error_info = ydl.download([youtube_url]) # Try downloading directly (might need ffmpeg)
132
  # This part is complex - capturing output might need more work if direct URL fetch failed.
133
  # Let's raise an error if we couldn't get a direct URL for now.
134
  raise yt_dlp.utils.DownloadError("Could not extract a direct audio URL and ffmpeg may not be available.")
 
80
  """
81
  Downloads the best audio-only format from a YouTube URL using yt-dlp and returns the raw audio data as bytes.
82
  """
83
+ safe_url = youtube_url.replace(
84
+ "youtube.com/watch?v=",
85
+ "yewtu.be/watch?v="
86
+ )
87
 
88
+ print(f"Attempting to download audio for: {safe_url}")
89
  ydl_opts = {
90
  'format': 'bestaudio/best', # Prioritize best audio-only, fallback to best audio in general
91
  'noplaylist': True, # Don't download playlist if URL is part of one
 
111
  # Or, a better way: get the direct audio URL first
112
 
113
  # --- Simpler & Often Better Approach: Get URL, then download with requests ---
114
+ info = ydl.extract_info(safe_url, download=False) # Get info without downloading yet
115
  best_audio_format = None
116
  for f in info.get('formats', []):
117
  # Look for formats processed by FFmpegExtractAudio or good audio codecs
 
132
  # If you *don't* have ffmpeg in the Dockerfile, the postprocessor might fail here
133
  # Let's try the download anyway, it might work for some native formats
134
  # This path is less reliable without guaranteed ffmpeg.
135
+ error_info = ydl.download([safe_url]) # Try downloading directly (might need ffmpeg)
136
  # This part is complex - capturing output might need more work if direct URL fetch failed.
137
  # Let's raise an error if we couldn't get a direct URL for now.
138
  raise yt_dlp.utils.DownloadError("Could not extract a direct audio URL and ffmpeg may not be available.")