File size: 5,017 Bytes
728b987
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac900c1
 
728b987
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import asyncio
import subprocess
import gradio as gr
import assemblyai as aai
import edge_tts
from static_ffmpeg import add_paths

# Static FFmpeg Setup
add_paths()

# AssemblyAI Key
ASSEMBLYAI_API_KEY = "0cc0d50376e5421da2b362efca91725a"
aai.settings.api_key = ASSEMBLYAI_API_KEY

def process_video(video_path):
    if not video_path or not os.path.exists(video_path):
        return "Video file တင်ပေးပါ သို့မဟုတ် ဖိုင်မတွေ့ပါ။", ""
    try:
        transcriber = aai.Transcriber()
        transcript = transcriber.transcribe(video_path)
        formatted_inputs = []
        for idx, sentence in enumerate(transcript.get_sentences()):
            start_sec = round(sentence.start / 1000, 1)
            end_sec = round(sentence.end / 1000, 1)
            max_limit = round(end_sec - start_sec, 1)
            formatted_inputs.append(f"ID: {idx+1} | Time Frame: {start_sec}s to {end_sec}s (Max Limit: {max_limit}s) | Text: \"{sentence.text}\"")
        prompt_header = (
            "You are a professional Burmese dubbing adapter. Translate each English segment into natural, spoken Burmese for voice-over. "
            "Preserve the meaning, emotion, names, and order. Return ONLY valid JSON in this exact shape: {\"translations\":[\"...\"]}. "
            "The array must have one string for every segment, in matching ID order. Keep each translation concise enough to fit its time frame and never add commentary.\n\n"
            "Translate these timestamped segments:\n"
        )
        full_prompt = prompt_header + "\n".join(formatted_inputs)
        return full_prompt, "Subtitle Extraction အောင်မြင်ပါပြီ။ အထက်ပါ Prompt ကို Gemini App တွင် ဘာသာပြန်ပါ။"
    except Exception as e:
        return f"Error ဖြစ်သွားပါသည်: {str(e)}", "AssemblyAI API Key မှန်မမှန် ပြန်စစ်ပါ။"

async def generate_dubbed_video_async(video_path, json_str, voice_option):
    try:
        data = json.loads(json_str)
        translations = data.get("translations", [])
    except Exception as e:
        return None, f"JSON Format မှားယွင်းနေပါသည်: {str(e)}"
    
    selected_voice = "my-MM-ThihaNeural" if "Thiha" in voice_option else "my-MM-NilarNeural"
    transcriber = aai.Transcriber()
    transcript = transcriber.transcribe(video_path)
    sentences = list(transcript.get_sentences())
    
    if len(translations) != len(sentences):
        return None, f"စာကြောင်း အရေအတွက် မတူပါ (Original: {len(sentences)}, Gemini: {len(translations)})"
        
    audio_files = []
    for idx, (sentence, burmese_text) in enumerate(zip(sentences, translations)):
        audio_path = f"seg_{idx}.mp3"
        communicate = edge_tts.Communicate(burmese_text, selected_voice)
        await communicate.save(audio_path)
        audio_files.append(audio_path)
        
    output_video = "dubbed_output.mp4"
    cmd = f"ffmpeg -y -i \"{video_path}\" -i \"{audio_files[0]}\" -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 \"{output_video}\""
    subprocess.run(cmd, shell=True)
    return output_video, f"Video Dubbing ပြီးပါပြီ။ (အသုံးပြုထားသော အသံ - {voice_option})"

def generate_dubbed_video(video_path, json_str, voice_option):
    return asyncio.run(generate_dubbed_video_async(video_path, json_str, voice_option))

with gr.Blocks(title="Movie Recap Dubbing Tool") as demo:
    gr.Markdown("## 🎬 Semi-Manual Video Dubbing System (Free Space)")
    video_input = gr.Video(label="Upload Video File")
    extract_btn = gr.Button("၁။ Subtitles & Prompt ထုတ်ယူရန်", variant="primary")
    # show_copy_button ကို ဖြုတ်ပေးထားပါသည်
    prompt_output = gr.Textbox(label="Gemini App ထဲ Copy ကူးထည့်ရမည့် Prompt Block", lines=10)
    status_1 = gr.Textbox(label="Status")
    extract_btn.click(process_video, inputs=[video_input], outputs=[prompt_output, status_1])
    
    gr.Markdown("---")
    with gr.Row():
        json_input = gr.Textbox(label="၂။ Gemini App မှ ရလာသော JSON Output Paste ထည့်ရန်", lines=6)
        voice_dropdown = gr.Dropdown(choices=["အမျိုးသားအသံ (Thiha)", "အမျိုးသမီးအသံ (Nilar)"], value="အမျိုးသားအသံ (Thiha)", label="TTS အသံ ရွေးရန်")
    render_btn = gr.Button("၃။ မြန်မာ အသံသွင်းပြီး Video ထုတ်ယူရန်", variant="primary")
    video_output = gr.Video(label="Final Dubbed Video")
    status_2 = gr.Textbox(label="Status")
    render_btn.click(generate_dubbed_video, inputs=[video_input, json_input, voice_dropdown], outputs=[video_output, status_2])

demo.launch(server_name="0.0.0.0")