File size: 1,433 Bytes
30944a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import subprocess
from constantes import YOUTUBE_COOKIE_PATH
from file_util import File_Util


class Video_Util:
    def download_video_from_url(url: str, output_path: str, video_file_name: str) -> str:
        """Baixa o vídeo do YouTube usando yt-dlp."""
        video_path = f'{output_path}/{video_file_name}.%(ext)s'
        print(f"Baixando vídeo de {url} para {video_path}...")
        try:
            # Comando yt-dlp para baixar o melhor formato mp4
            command = [
                'yt-dlp',
                "--cookies", YOUTUBE_COOKIE_PATH,
                '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
                '-o', video_path,
                url
            ]
            result = subprocess.run(command, check=True, capture_output=True, text=True)
            lista_arquivos = File_Util.retirar_sufixo_codec_arquivo(output_path)
            print("Download de áudio concluído com sucesso.")
            return f"{output_path}/{lista_arquivos[0]}"
        except subprocess.CalledProcessError as e:
            print(f"Erro ao baixar o vídeo: {e}")
            print(f"Saída do erro: {e.stderr}")
            return False
        except FileNotFoundError:
            print("Erro: O comando 'yt-dlp' não foi encontrado. Certifique-se de que ele está instalado e no PATH do sistema.")
            print("Você pode instalá-lo com: pip install yt-dlp")
            return False