File size: 1,304 Bytes
c22c098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import yt_dlp
from transformers import pipeline
from gtts import gTTS

# Load pipelines
asr = pipeline("automatic-speech-recognition", model="openai/whisper-base", task="transcribe")
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")

def youtube_to_hindi_dub(url):
    # Step 1: Download audio from YouTube
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': 'audio.%(ext)s',
        'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3'}],
        'quiet': True
    }
    with yt_dlp.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    audio_path = "audio.mp3"

    # Step 2: Transcribe English
    result = asr(audio_path)
    english_text = result["text"]

    # Step 3: Translate to Hindi
    hindi_text = translator(english_text)[0]['translation_text']

    # Step 4: Convert to Hindi Audio
    tts = gTTS(hindi_text, lang="hi")
    tts.save("dubbed.mp3")

    return "dubbed.mp3"

# Gradio UI
iface = gr.Interface(
    fn=youtube_to_hindi_dub,
    inputs=gr.Textbox(label="YouTube Video Link"),
    outputs=gr.Audio(label="Dubbed Hindi Audio", type="filepath"),
    title="🎧 YouTube Video to Hindi Dubber",
    description="Paste an English YouTube link, and get Hindi AI voice dubbing."
)

iface.launch()