File size: 2,471 Bytes
a13cae7
7517ea5
a13cae7
cc3b298
9b290a2
8f41d61
a13cae7
cc3b298
 
 
 
 
 
a13cae7
9b290a2
 
 
 
 
cc3b298
 
9b290a2
 
 
a13cae7
7517ea5
cc3b298
a13cae7
cc3b298
a13cae7
cc3b298
 
a13cae7
9b290a2
 
 
 
8f41d61
a13cae7
 
 
 
cc3b298
 
8f41d61
a13cae7
 
 
 
 
 
 
 
 
 
cb7453d
cc3b298
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
import re
import gradio as gr
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api._errors import TranscriptsDisabled
import traceback

def extract_video_id(youtube_url):
    # Regex para extrair o ID do vídeo da URL do YouTube
    match = re.search(r'(?:youtu\.be\/|(?:www\.)?youtube\.com\/(?:watch\?v=|embed\/|v\/|.+\?v=))([^&]{11})', youtube_url)
    video_id = match.group(1) if match else None
    print(f"URL recebida: {youtube_url}")
    print(f"ID do vídeo extraído: {video_id}")
    return video_id

def get_available_transcripts(video_id):
    try:
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        available_languages = [t.language_code for t in transcript_list]
        return f"Transcrições disponíveis: {available_languages}"
    except TranscriptsDisabled:
        return "Não há transcrições disponíveis para este vídeo."
    except Exception as e:
        return f"Erro ao listar transcrições: {str(e)}"

def get_transcript(video_id, language="en"):
    try:
        print(f"Tentando obter transcrição para o vídeo ID: {video_id} no idioma: {language}")
        transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=[language])
        print("Transcrição obtida com sucesso.")
        return "\n".join([f"{t['start']}: {t['text']}" for t in transcript])
    except TranscriptsDisabled:
        return "Erro: As legendas estão desabilitadas para este vídeo."
    except Exception as e:
        available_transcripts = get_available_transcripts(video_id)
        error_message = f"Erro ao obter transcrição: {str(e)}\n{available_transcripts}\n"
        error_message += traceback.format_exc()
        return error_message

def gradio_interface(youtube_url, language):
    video_id = extract_video_id(youtube_url)
    if not video_id:
        return "Erro: URL inválida. Por favor, insira um link válido do YouTube."
    transcript = get_transcript(video_id, language)
    return transcript

iface = gr.Interface(
    fn=gradio_interface,
    inputs=[
        gr.Textbox(label="URL do Vídeo (YouTube)", placeholder="Ex: https://www.youtube.com/watch?v=tl1jHm0qC_4"),
        gr.Dropdown(label="Idioma", choices=["en", "pt", "es", "fr"], value="en")
    ],
    outputs="text",
    title="Obter Transcrição de Vídeos do YouTube",
    description="Insira a URL de um vídeo do YouTube e selecione o idioma da transcrição."
)

iface.launch(share=True)