Mais ajustes de função
This commit is contained in:
72
main.py
72
main.py
@@ -156,29 +156,16 @@ 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_presets = {
|
quality_targets = {
|
||||||
"low": [
|
"low": 480,
|
||||||
"bestvideo[height<=480]+bestaudio/best[height<=480]",
|
"medium": 720,
|
||||||
"best[height<=480]",
|
"high": 1080
|
||||||
"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_presets:
|
if qualidade not in quality_targets:
|
||||||
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)
|
||||||
|
|
||||||
@@ -189,7 +176,6 @@ def download_video(
|
|||||||
'outtmpl': output_template,
|
'outtmpl': output_template,
|
||||||
'quiet': True,
|
'quiet': True,
|
||||||
'no_warnings': True,
|
'no_warnings': True,
|
||||||
'nocheckcertificate': True,
|
|
||||||
'ignoreerrors': False,
|
'ignoreerrors': False,
|
||||||
'no_color': True,
|
'no_color': True,
|
||||||
'extract_flat': 'in_playlist',
|
'extract_flat': 'in_playlist',
|
||||||
@@ -197,15 +183,39 @@ def download_video(
|
|||||||
'allow_unplayable_formats': True,
|
'allow_unplayable_formats': True,
|
||||||
}
|
}
|
||||||
|
|
||||||
def try_download(ydl, format_spec, attempt=1):
|
def get_best_format(ydl, target_height):
|
||||||
ydl.params['format'] = format_spec
|
info = ydl.extract_info(target, download=False)
|
||||||
|
if not info or 'formats' not in info:
|
||||||
|
return 'best'
|
||||||
|
|
||||||
try:
|
formats = info['formats']
|
||||||
return ydl.extract_info(target, download=True)
|
best_format = None
|
||||||
except Exception as e:
|
best_height = 0
|
||||||
if attempt < len(format_options):
|
|
||||||
return try_download(ydl, format_options[attempt], attempt + 1)
|
for f in formats:
|
||||||
raise
|
if f.get('height') and f.get('acodec') != 'none':
|
||||||
|
if f['height'] <= target_height and f['height'] > best_height:
|
||||||
|
best_format = f
|
||||||
|
best_height = f['height']
|
||||||
|
|
||||||
|
if not best_format:
|
||||||
|
video_format = None
|
||||||
|
audio_format = None
|
||||||
|
|
||||||
|
for f in formats:
|
||||||
|
if f.get('vcodec') != 'none' and f.get('acodec') == 'none':
|
||||||
|
if f.get('height') and f['height'] <= target_height and (not video_format or f['height'] > video_format.get('height', 0)):
|
||||||
|
video_format = f
|
||||||
|
|
||||||
|
for f in formats:
|
||||||
|
if f.get('acodec') != 'none' and f.get('vcodec') == 'none':
|
||||||
|
if not audio_format or f.get('tbr', 0) > audio_format.get('tbr', 0):
|
||||||
|
audio_format = f
|
||||||
|
|
||||||
|
if video_format and audio_format:
|
||||||
|
return f"{video_format['format_id']}+{audio_format['format_id']}"
|
||||||
|
|
||||||
|
return best_format['format_id'] if best_format else 'best'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with YoutubeDL(ydl_opts) as ydl:
|
with YoutubeDL(ydl_opts) as ydl:
|
||||||
@@ -240,9 +250,13 @@ def download_video(
|
|||||||
"filename": filename
|
"filename": filename
|
||||||
}
|
}
|
||||||
|
|
||||||
print(f'Iniciando download com qualidade: {qualidade}')
|
print(f'Buscando melhor formato disponível para qualidade: {qualidade}')
|
||||||
|
|
||||||
result = try_download(ydl, format_options[0])
|
best_format = get_best_format(ydl, target_height)
|
||||||
|
print(f'Melhor formato encontrado: {best_format}')
|
||||||
|
|
||||||
|
ydl.params['format'] = best_format
|
||||||
|
result = ydl.extract_info(target, download=True)
|
||||||
|
|
||||||
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"]
|
||||||
|
|||||||
Reference in New Issue
Block a user