Demais ajustes na função

This commit is contained in:
LeoMortari
2025-09-28 01:54:28 -03:00
parent 8577e45b1d
commit 6f3798d435

46
main.py
View File

@@ -156,15 +156,29 @@ def download_video(
target = f"https://www.youtube.com/watch?v={videoId}" target = f"https://www.youtube.com/watch?v={videoId}"
video_id = videoId video_id = videoId
quality_map = { quality_presets = {
"low": "bestvideo[height<=480]+bestaudio/best[height<=480]", "low": [
"medium": "bestvideo[height<=720]+bestaudio/best[height<=720]", "bestvideo[height<=480]+bestaudio/best[height<=480]",
"high": "bestvideo+bestaudio/best" "best[height<=480]",
"best"
],
"medium": [
"bestvideo[height<=720]+bestaudio/best[height<=720]",
"best[height<=720]",
"best"
],
"high": [
"bestvideo+bestaudio/best",
"best"
]
} }
qualidade = qualidade.lower() qualidade = qualidade.lower()
if qualidade not in quality_map: if qualidade not in quality_presets:
raise HTTPException(status_code=400, detail="Qualidade deve ser: low, medium ou high") raise HTTPException(status_code=400, detail="Qualidade deve ser: low, medium ou high")
format_options = quality_presets[qualidade]
videos_dir = "/app/videos" videos_dir = "/app/videos"
os.makedirs(videos_dir, exist_ok=True) os.makedirs(videos_dir, exist_ok=True)
@@ -172,19 +186,27 @@ def download_video(
output_template = os.path.join(videos_dir, f"{unique_id}.%(ext)s") output_template = os.path.join(videos_dir, f"{unique_id}.%(ext)s")
ydl_opts = { ydl_opts = {
"format": quality_map[qualidade], 'outtmpl': output_template,
"outtmpl": output_template,
'quiet': True, 'quiet': True,
'no_warnings': True, 'no_warnings': True,
'skip_download': True,
'nocheckcertificate': True, 'nocheckcertificate': True,
'ignoreerrors': True, 'ignoreerrors': False,
'no_color': True, 'no_color': True,
'extract_flat': 'in_playlist', 'extract_flat': 'in_playlist',
'force_generic_extractor': True, 'force_generic_extractor': True,
'allow_unplayable_formats': True, 'allow_unplayable_formats': True,
} }
def try_download(ydl, format_spec, attempt=1):
ydl.params['format'] = format_spec
try:
return ydl.extract_info(target, download=True)
except Exception as e:
if attempt < len(format_options):
return try_download(ydl, format_options[attempt], attempt + 1)
raise
try: try:
with YoutubeDL(ydl_opts) as ydl: with YoutubeDL(ydl_opts) as ydl:
base = ydl.extract_info(target, download=False) base = ydl.extract_info(target, download=False)
@@ -210,7 +232,7 @@ def download_video(
filename = f"{clean_title}_{qualidade}.mp4" filename = f"{clean_title}_{qualidade}.mp4"
final_path = os.path.join(videos_dir, filename) final_path = os.path.join(videos_dir, filename)
print('Info ok') print('Informações do vídeo obtidas com sucesso')
if os.path.exists(final_path): if os.path.exists(final_path):
return { return {
@@ -218,9 +240,9 @@ def download_video(
"filename": filename "filename": filename
} }
print('Lets download') print(f'Iniciando download com qualidade: {qualidade}')
result = ydl.extract_info(target, download=True) result = try_download(ydl, format_options[0])
if "requested_downloads" in result and len(result["requested_downloads"]) > 0: if "requested_downloads" in result and len(result["requested_downloads"]) > 0:
real_file_path = result["requested_downloads"][0]["filepath"] real_file_path = result["requested_downloads"][0]["filepath"]