File size: 5,002 Bytes
314503f
 
 
 
 
 
 
 
 
 
 
 
f77d0ec
75ce402
314503f
f77d0ec
314503f
4147b5e
 
314503f
4147b5e
314503f
 
 
 
 
 
 
 
f77d0ec
314503f
 
 
 
f77d0ec
 
 
 
 
 
 
 
 
 
 
 
314503f
 
 
 
 
 
 
 
 
 
 
 
75ce402
314503f
75ce402
 
 
314503f
 
 
4147b5e
 
314503f
 
75ce402
314503f
 
 
4147b5e
314503f
 
 
 
 
 
 
 
 
 
 
 
 
75ce402
4147b5e
 
 
 
75ce402
 
4147b5e
75ce402
4147b5e
 
 
75ce402
4147b5e
 
 
 
 
 
75ce402
4147b5e
 
 
 
75ce402
 
 
 
4147b5e
 
 
 
 
314503f
 
 
 
 
f77d0ec
314503f
 
 
75ce402
 
314503f
 
 
 
75ce402
 
4147b5e
314503f
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# -*- coding: utf-8 -*-
"""
Script para baixar um vídeo do YouTube, extrair frames, analisar com GPT-4o e contar aves.
"""

import os
import subprocess
import cv2
import base64
import time
import json
import re 
import shutil
from openai import OpenAI


# --- Configurações (Substitua os placeholders) ---
VIDEO_URL = "https://www.youtube.com/watch?v=1htKBjuUWec"  # Substitua pela URL do vídeo do YouTube
OUTPUT_DIR = "./audio_analysis_output" # Diretório para salvar o áudio
AUDIO_FILENAME = "downloaded_audio"
TRANSCRIPT_FILENAME = "transcript.txt"

# Verifica se a URL foi definida
if VIDEO_URL == "URL_DO_SEU_VIDEO_AQUI":
    print("AVISO: A URL do vídeo não foi definida. Por favor, edite o script e insira a URL desejada.")
    # exit(1)

# --- Funções ---

def create_or_clear_output_directory():
    """Cria o diretório de saída se não existir."""
    if not os.path.exists(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)
        print(f"Diretório criado: {OUTPUT_DIR}")
    else:    
        # Limpa todos os arquivos e subdiretórios
        for filename in os.listdir(OUTPUT_DIR):
            file_path = os.path.join(OUTPUT_DIR, filename)
            try:
                if os.path.isfile(file_path) or os.path.islink(file_path):
                    os.unlink(file_path)
                elif os.path.isdir(file_path):
                    shutil.rmtree(file_path)
            except Exception as e:
                print(f"Erro ao excluir {file_path}: {e}")
        print(f"Diretório limpo: {OUTPUT_DIR}")
        
def retirar_sufixo_codec_arquivo(directory) -> None:
    for filename in os.listdir(directory):
        # Procura padrão como ".f123" antes da extensão
        new_filename = re.sub(r'\.f\d{3}(?=\.\w+$)', '', filename)
        if new_filename != filename:
            old_path = os.path.join(directory, filename)
            new_path = os.path.join(directory, new_filename)
            os.rename(old_path, new_path)
            print(f"Renomeado: {filename}{new_filename}")
            

def download_audio(url):
    """Baixa apenas o áudio do YouTube usando yt-dlp."""
     
    output_path = f'{OUTPUT_DIR}/{AUDIO_FILENAME}.%(ext)s'
    
    print(f"Baixando áudio de {url} para {output_path}...")
    try:
        # Comando yt-dlp para baixar o melhor áudio disponível e convertê-lo para mp3
        
        
        command = [
            'yt-dlp',
            '-f', 'bestaudio[ext=m4a]',
            '-o', output_path,
            url
        ]
        
        result = subprocess.run(command, check=True, capture_output=True, text=True)
        retirar_sufixo_codec_arquivo(OUTPUT_DIR)
        
        print("Download de áudio concluído com sucesso.")
        return True
    except subprocess.CalledProcessError as e:
        print(f"Erro ao baixar o áudio: {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.")
        return False

def extract_text_from_audio() -> str:
    """
    Usa a API Whisper da OpenAI para transcrever o áudio em texto com quebras de linha naturais,
    removendo timestamps e IDs. Salva em arquivo .txt se o caminho for fornecido.
    """
    client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
    
    try:
        audio_path = f"{OUTPUT_DIR}/{AUDIO_FILENAME}.m4a"
        print(f"Iniciando transcrição (formato SRT simplificado): {audio_path}")

        with open(audio_path, "rb") as audio_file:
            transcription = client.audio.transcriptions.create(
                model="whisper-1",
                file=audio_file,
                response_format="srt"
            )

        # Remove linhas com números e timestamps
        lines = transcription.splitlines()
        only_text = [line.strip() for line in lines if not re.match(r"^\d+$", line) and "-->" not in line]
        formatted_text = "\n".join(only_text)

        # Salva em .txt se desejado
        output_txt_path = f"{OUTPUT_DIR}/{TRANSCRIPT_FILENAME}"
        with open(output_txt_path, "w", encoding="utf-8") as f:
            f.write(formatted_text)
        print(f"Transcrição salva em: {output_txt_path}")

        return formatted_text
    except Exception as e:
        print(f"Erro ao transcrever áudio: {e}")
        return ""


# --- Atualização do Bloco Principal --- 
# (Adicionar inicialização do cliente OpenAI e o loop de análise)
if __name__ == "__main__":
    create_or_clear_output_directory()    
    # Etapa 1: Baixar o vídeo
    video_downloaded_or_exists = False
    if VIDEO_URL != "URL_DO_SEU_VIDEO_AQUI":
        if download_audio(VIDEO_URL):
            print(f"AUDIO salvo em: {OUTPUT_DIR}")
            video_downloaded_or_exists = True
        else:
            print("Falha no download do vídeo. Pulando etapas dependentes.")
    else:
        print("Vídeo não informado")
    extract_text_from_audio()