File size: 2,176 Bytes
ca4a74b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import tempfile
import os
from processor import translate_and_dub

st.set_page_config(page_title="Video Translator", page_icon="📺")

st.title("📺 Video Translator")

# --- SIDEBAR ---
with st.sidebar:
    mode = st.radio("Chuyển ngữ", ('Tiếng Việt -> Trung Quốc', 'Trung Quốc -> Tiếng Việt'))
    gender = st.radio("Giọng", ('Nữ', 'Nam'))
    target_lang = "Chinese" if mode == 'Tiếng Việt -> Trung Quốc' else "Vietnamese"

# --- MAIN ---
uploaded_file = st.file_uploader("Upload Video", type=['mp4', 'mov'])

if uploaded_file is not None:
    # Save Upload
    tfile = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') 
    tfile.write(uploaded_file.read())
    video_path = tfile.name

    st.video(video_path) # Show original
    
    if st.button('🚀 Dịch video'):
        with st.spinner('Đang xử lý...'):
            try:
                # Get paths for Video and Subtitle file
                dubbed_video, subtitle_file, segments = translate_and_dub(video_path, target_lang, gender)
                
                st.success("Dịch thành công!")
                
                st.subheader("Video đã dịch (Bật CC 💬 trong player)")
                
                # THIS IS THE KEY CHANGE:
                # We pass the video AND the subtitle file path
                st.video(dubbed_video, subtitles=subtitle_file)
                
                col1, col2 = st.columns(2)
                with col1:
                    with open(dubbed_video, 'rb') as f:
                        st.download_button('⬇️ Tải Video', f, file_name="dubbed_video.mp4")
                with col2:
                    with open(subtitle_file, 'rb') as f:
                        st.download_button('⬇️ Tải Subtitles', f, file_name="subtitles.vtt")

                # Show text transcript below
                with st.expander("Xem bản dịch"):
                    for seg in segments:
                        st.write(f"**{seg['start']}s**: {seg['text']}")

            except Exception as e:
                st.error(f"Error: {e}")