File size: 1,127 Bytes
c6f2536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import yt_dlp
import gradio as gr

def get_captions(video_url, lang='en'):
    ydl_opts = {
        'skip_download': True,
        'writesubtitles': True,
        'subtitleslangs': [lang],
        'writeautomaticsub': True,
        'quiet': True,
        'forcejson': True
    }

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(video_url, download=False)
            subtitles = info.get('subtitles') or info.get('automatic_captions')
            if subtitles and lang in subtitles:
                subtitle_url = subtitles[lang][0]['url']
                return f"✅ Captions Found:\n{subtitle_url}"
            return "⚠️ No captions found for this language."
    except Exception as e:
        return f"❌ Error: {str(e)}"

gr.Interface(
    fn=get_captions,
    inputs=[
        gr.Textbox(label="YouTube Video URL"),
        gr.Textbox(value="en", label="Language Code (e.g., en, es, fr)")
    ],
    outputs="text",
    title="YouTube Caption Extractor",
    description="Paste a YouTube video URL and get the direct link to the captions (auto or uploaded)."
).launch()