Demais ajustes na função
This commit is contained in:
46
main.py
46
main.py
@@ -156,15 +156,29 @@ def download_video(
|
||||
target = f"https://www.youtube.com/watch?v={videoId}"
|
||||
video_id = videoId
|
||||
|
||||
quality_map = {
|
||||
"low": "bestvideo[height<=480]+bestaudio/best[height<=480]",
|
||||
"medium": "bestvideo[height<=720]+bestaudio/best[height<=720]",
|
||||
"high": "bestvideo+bestaudio/best"
|
||||
quality_presets = {
|
||||
"low": [
|
||||
"bestvideo[height<=480]+bestaudio/best[height<=480]",
|
||||
"best[height<=480]",
|
||||
"best"
|
||||
],
|
||||
"medium": [
|
||||
"bestvideo[height<=720]+bestaudio/best[height<=720]",
|
||||
"best[height<=720]",
|
||||
"best"
|
||||
],
|
||||
"high": [
|
||||
"bestvideo+bestaudio/best",
|
||||
"best"
|
||||
]
|
||||
}
|
||||
|
||||
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")
|
||||
|
||||
format_options = quality_presets[qualidade]
|
||||
|
||||
videos_dir = "/app/videos"
|
||||
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")
|
||||
|
||||
ydl_opts = {
|
||||
"format": quality_map[qualidade],
|
||||
"outtmpl": output_template,
|
||||
'outtmpl': output_template,
|
||||
'quiet': True,
|
||||
'no_warnings': True,
|
||||
'skip_download': True,
|
||||
'nocheckcertificate': True,
|
||||
'ignoreerrors': True,
|
||||
'ignoreerrors': False,
|
||||
'no_color': True,
|
||||
'extract_flat': 'in_playlist',
|
||||
'force_generic_extractor': 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:
|
||||
with YoutubeDL(ydl_opts) as ydl:
|
||||
base = ydl.extract_info(target, download=False)
|
||||
@@ -210,7 +232,7 @@ def download_video(
|
||||
filename = f"{clean_title}_{qualidade}.mp4"
|
||||
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):
|
||||
return {
|
||||
@@ -218,9 +240,9 @@ def download_video(
|
||||
"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:
|
||||
real_file_path = result["requested_downloads"][0]["filepath"]
|
||||
|
||||
Reference in New Issue
Block a user