Erfan commited on
Commit
79e1abd
·
1 Parent(s): 0e4bd6e

fix: use format_sort instead of strict height filters in -f

Browse files

- Drop [height<=720] strict filters from format strings
- Add format_sort with res:720/vcodec:h264/acodec:aac
- Fixes 'Requested format not available' when no exact 720p stream exists

Files changed (1) hide show
  1. bot/youtube.py +8 -3
bot/youtube.py CHANGED
@@ -170,13 +170,17 @@ def _has_cookies() -> bool:
170
 
171
  async def _try_ytdlp(url: str, quality: str = "audio"):
172
  if quality == "audio":
173
- formats = ["bestaudio/best", "bestaudio", "worstaudio"]
 
174
  elif quality == "1080p":
175
- formats = ["bv*[height<=1080]+ba/b[height<=1080]", "bestvideo[height<=1080]+bestaudio/best[height<=1080]", "best[height<=1080]"]
 
176
  elif quality == "720p":
177
- formats = ["bv*[height<=720]+ba/b[height<=720]", "bestvideo[height<=720]+bestaudio/best[height<=720]", "best[height<=720]"]
 
178
  else:
179
  formats = ["bv*+ba/b", "bestvideo+bestaudio/best", "best"]
 
180
  clients = ["android_embedded", "android", "web", "tv", "ios", "web_creator", "music"]
181
  cookies = _has_cookies()
182
  for client in clients:
@@ -185,6 +189,7 @@ async def _try_ytdlp(url: str, quality: str = "audio"):
185
  _log(f"Trying yt-dlp client={client} fmt={fmt}{' (with cookies)' if cookies else ''}")
186
  opts = {
187
  "format": fmt,
 
188
  "outtmpl": os.path.join(DOWNLOAD_DIR, f"{uuid.uuid4()}.%(ext)s"),
189
  "quiet": True,
190
  "no_warnings": True,
 
170
 
171
  async def _try_ytdlp(url: str, quality: str = "audio"):
172
  if quality == "audio":
173
+ formats = ["bestaudio/best", "bestaudio"]
174
+ sort = ["acodec:aac"]
175
  elif quality == "1080p":
176
+ formats = ["bv*+ba/b", "bestvideo+bestaudio/best", "best"]
177
+ sort = ["res:1080", "vcodec:h264", "acodec:aac"]
178
  elif quality == "720p":
179
+ formats = ["bv*+ba/b", "bestvideo+bestaudio/best", "best"]
180
+ sort = ["res:720", "vcodec:h264", "acodec:aac"]
181
  else:
182
  formats = ["bv*+ba/b", "bestvideo+bestaudio/best", "best"]
183
+ sort = ["vcodec:h264", "acodec:aac"]
184
  clients = ["android_embedded", "android", "web", "tv", "ios", "web_creator", "music"]
185
  cookies = _has_cookies()
186
  for client in clients:
 
189
  _log(f"Trying yt-dlp client={client} fmt={fmt}{' (with cookies)' if cookies else ''}")
190
  opts = {
191
  "format": fmt,
192
+ "format_sort": sort,
193
  "outtmpl": os.path.join(DOWNLOAD_DIR, f"{uuid.uuid4()}.%(ext)s"),
194
  "quiet": True,
195
  "no_warnings": True,