File size: 2,742 Bytes
62a9f0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import os
import tempfile
from core.audio_processor import extract_audio
from core.subtitle_gen import generate_subtitles
from core.translator import translate_subtitles
from core.video_merger import merge_video_subtitles

# Available languages
LANGUAGES = {
    "English": "en",
    "Spanish": "es",
    "French": "fr",
    "German": "de",
    "Japanese": "ja",
    "Hindi": "hi"
}

def process_video(
    video_file,
    source_lang,
    target_langs,
    progress=gr.Progress()
):
    # Create temp directory
    with tempfile.TemporaryDirectory() as tmpdir:
        try:
            progress(0.1, desc="Extracting audio...")
            audio_path = extract_audio(video_file.name, tmpdir)
            
            progress(0.3, desc="Generating subtitles...")
            srt_path = generate_subtitles(audio_path, tmpdir)
            
            progress(0.5, desc="Translating subtitles...")
            translated_subs = translate_subtitles(
                srt_path, 
                [LANGUAGES[lang] for lang in target_langs],
                tmpdir
            )
            
            progress(0.7, desc="Generating output videos...")
            output_files = []
            for lang, sub_path in translated_subs.items():
                output_path = os.path.join(tmpdir, f"output_{lang}.mp4")
                merge_video_subtitles(
                    video_file.name,
                    sub_path,
                    output_path
                )
                output_files.append(output_path)
            
            progress(1.0, desc="Done!")
            return output_files
            
        except Exception as e:
            raise gr.Error(f"Processing failed: {str(e)}")

# Gradio interface
with gr.Blocks(title="Video Translator") as demo:
    gr.Markdown("# 🎥 Video Translation System")
    gr.Markdown("Upload a video to generate multilingual versions")
    
    with gr.Row():
        with gr.Column():
            video_input = gr.File(label="Upload Video", type="file")
            source_lang = gr.Dropdown(
                label="Source Language",
                choices=list(LANGUAGES.keys()),
                value="English"
            )
            target_langs = gr.CheckboxGroup(
                label="Target Languages",
                choices=list(LANGUAGES.keys()),
                value=["Spanish", "French"]
            )
            submit_btn = gr.Button("Translate Video")
        
        with gr.Column():
            output_videos = gr.Files(label="Translated Videos")
    
    submit_btn.click(
        fn=process_video,
        inputs=[video_input, source_lang, target_langs],
        outputs=output_videos
    )

if __name__ == "__main__":
    demo.launch()