tecuts commited on
Commit
61ee84f
·
verified ·
1 Parent(s): 1cfa229

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -70,34 +70,45 @@ def get_best_formats_with_fallback(data: dict, requested_quality: int):
70
  f for f in data["formats"]
71
  if f.get("vcodec") != "none" and f.get("acodec") == "none" and f.get("ext") == "mp4" and f.get("height")
72
  ]
73
- # Sort by height, descending
74
  video_formats.sort(key=lambda f: f["height"], reverse=True)
75
 
76
- # Find the best format that is less than or equal to the requested quality
77
  for f in video_formats:
78
  if f["height"] <= requested_quality:
79
  video_url = f["url"]
80
  logging.info(f"Selected video quality: {f['height']}p (requested <= {requested_quality}p)")
81
  break
82
 
83
- # If no suitable format was found, fallback to the lowest available quality
84
  if not video_url and video_formats:
85
- f = video_formats[-1] # Smallest resolution
86
  video_url = f["url"]
87
  logging.warning(f"Requested quality not available. Falling back to lowest available: {f['height']}p")
88
 
89
- # --- Audio Selection ---
90
  audio_url = None
 
91
  audio_formats = [
92
- f for f in data["formats"]
93
- if f.get("acodec") != "none" and f.get("vcodec") == "none" and "m4a" in f.get("format_id", "")
94
  ]
95
- audio_formats.sort(key=lambda f: f.get("abr", 0), reverse=True) # Sort by audio bitrate
 
96
  if audio_formats:
97
- audio_url = audio_formats[0]["url"]
98
- logging.info(f"Selected best available audio with bitrate {audio_formats[0].get('abr', 'N/A')}k.")
 
 
 
 
 
 
99
 
 
100
  if not video_url or not audio_url:
 
 
 
 
 
101
  raise ValueError("Could not find suitable video and/or audio streams from the Info API.")
102
 
103
  return video_url, audio_url
 
70
  f for f in data["formats"]
71
  if f.get("vcodec") != "none" and f.get("acodec") == "none" and f.get("ext") == "mp4" and f.get("height")
72
  ]
 
73
  video_formats.sort(key=lambda f: f["height"], reverse=True)
74
 
 
75
  for f in video_formats:
76
  if f["height"] <= requested_quality:
77
  video_url = f["url"]
78
  logging.info(f"Selected video quality: {f['height']}p (requested <= {requested_quality}p)")
79
  break
80
 
 
81
  if not video_url and video_formats:
82
+ f = video_formats[-1]
83
  video_url = f["url"]
84
  logging.warning(f"Requested quality not available. Falling back to lowest available: {f['height']}p")
85
 
86
+ # --- Audio Selection (Revised and Improved) ---
87
  audio_url = None
88
+ # Find all streams that are audio-only
89
  audio_formats = [
90
+ f for f in data.get("formats", [])
91
+ if f.get("acodec") not in (None, "none") and f.get("vcodec") == "none"
92
  ]
93
+
94
+ # Sort by audio bitrate (abr) to find the best quality
95
  if audio_formats:
96
+ audio_formats.sort(key=lambda f: f.get("abr", 0) or 0, reverse=True)
97
+ selected_audio = audio_formats[0]
98
+ audio_url = selected_audio.get("url")
99
+ logging.info(
100
+ f"Selected best audio: format_id {selected_audio.get('format_id')}, "
101
+ f"bitrate {selected_audio.get('abr', 'N/A')}k, "
102
+ f"codec {selected_audio.get('acodec')}"
103
+ )
104
 
105
+ # --- Final Check ---
106
  if not video_url or not audio_url:
107
+ # Add more detailed logging for easier debugging
108
+ if not video_url:
109
+ logging.error("Failed to find a suitable video_url.")
110
+ if not audio_url:
111
+ logging.error("Failed to find a suitable audio_url.")
112
  raise ValueError("Could not find suitable video and/or audio streams from the Info API.")
113
 
114
  return video_url, audio_url