Update app.py
Browse files
app.py
CHANGED
|
@@ -47,23 +47,70 @@ async def cleanup_file(filepath: Path):
|
|
| 47 |
logging.error(f"Error during cleanup of {filepath.parent}: {e}")
|
| 48 |
|
| 49 |
def get_best_formats_with_fallback(data: dict, requested_quality: int):
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
video_formats.sort(key=lambda f: f["height"], reverse=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
for f in video_formats:
|
| 56 |
-
if f["height"] <= requested_quality:
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if audio_formats:
|
| 60 |
audio_formats.sort(key=lambda f: f.get("abr", 0) or 0, reverse=True)
|
| 61 |
-
selected_audio = audio_formats[0]
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
if not video_url or not audio_url:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
raise ValueError("Could not find suitable video and/or audio streams from the Info API.")
|
| 67 |
return video_url, audio_url
|
| 68 |
|
| 69 |
|
|
|
|
| 47 |
logging.error(f"Error during cleanup of {filepath.parent}: {e}")
|
| 48 |
|
| 49 |
def get_best_formats_with_fallback(data: dict, requested_quality: int):
|
| 50 |
+
"""
|
| 51 |
+
Parses the Info API response to find the best matching video format
|
| 52 |
+
with a robust fallback, and the best audio format.
|
| 53 |
+
"""
|
| 54 |
+
if "formats" not in data:
|
| 55 |
+
raise ValueError("The 'formats' key is missing from the Info API response.")
|
| 56 |
+
|
| 57 |
+
# --- Video Selection (Revised and Improved) ---
|
| 58 |
+
video_url = None
|
| 59 |
+
# Filter for any video-only stream that has resolution info
|
| 60 |
+
video_formats = [
|
| 61 |
+
f for f in data.get("formats", [])
|
| 62 |
+
if f.get("vcodec") not in (None, "none") and f.get("acodec") == "none" and f.get("height")
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
# Sort by height from best to worst
|
| 66 |
video_formats.sort(key=lambda f: f["height"], reverse=True)
|
| 67 |
+
|
| 68 |
+
if not video_formats:
|
| 69 |
+
raise ValueError("Could not find any suitable video-only streams in the API response.")
|
| 70 |
+
|
| 71 |
+
# Try to find the best format that is at or below the requested quality
|
| 72 |
+
selected_format = None
|
| 73 |
for f in video_formats:
|
| 74 |
+
if f["height"] <= requested_quality:
|
| 75 |
+
selected_format = f
|
| 76 |
+
break # Found the best possible match
|
| 77 |
+
|
| 78 |
+
# If no match was found (e.g., user requested 144p but only 360p+ is available),
|
| 79 |
+
# then default to the absolute best quality available.
|
| 80 |
+
if selected_format is None:
|
| 81 |
+
selected_format = video_formats[0] # Fallback to the best overall
|
| 82 |
+
logging.warning(
|
| 83 |
+
f"Requested quality ({requested_quality}p) is not available. "
|
| 84 |
+
f"Falling back to best available quality: {selected_format.get('height')}p."
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
video_url = selected_format.get("url")
|
| 88 |
+
logging.info(
|
| 89 |
+
f"Selected video: {selected_format.get('height')}p "
|
| 90 |
+
f"(format_id: {selected_format.get('format_id')}, ext: {selected_format.get('ext')})"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# --- Audio Selection ---
|
| 95 |
+
audio_url = None
|
| 96 |
+
audio_formats = [
|
| 97 |
+
f for f in data.get("formats", [])
|
| 98 |
+
if f.get("acodec") not in (None, "none") and f.get("vcodec") == "none"
|
| 99 |
+
]
|
| 100 |
if audio_formats:
|
| 101 |
audio_formats.sort(key=lambda f: f.get("abr", 0) or 0, reverse=True)
|
| 102 |
+
selected_audio = audio_formats[0]
|
| 103 |
+
audio_url = selected_audio.get("url")
|
| 104 |
+
logging.info(
|
| 105 |
+
f"Selected best audio: format_id {selected_audio.get('format_id')}, "
|
| 106 |
+
f"bitrate {selected_audio.get('abr', 'N/A')}k, "
|
| 107 |
+
f"codec {selected_audio.get('acodec')}"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# --- Final Check ---
|
| 111 |
if not video_url or not audio_url:
|
| 112 |
+
raise ValueError("Could not find a suitable video and/or audio stream.")
|
| 113 |
+
|
|
|
|
| 114 |
return video_url, audio_url
|
| 115 |
|
| 116 |
|