import gradio as gr
import time

def start_stream():
    return "assets/sample.mp4"  # 返回示例视频路径

def stop_stream():
    return None

def add_subtitle(text, font, size, position):
    return f"字幕已添加：{text} ({font}, {size}px, {position})", None

def process_video(progress=gr.Progress()):
    progress(0, "开始处理视频...")
    for i in range(10):
        time.sleep(0.2)
        progress((i + 1) / 10, f"正在处理第 {i+1}/10 段...")
    return "处理完成", "assets/processed.mp4"

with gr.Blocks(title="口播视频剪辑工具") as demo:
    gr.Markdown("# 口播视频剪辑工具")
    
    with gr.Row():
        # 左侧控制面板
        with gr.Column(scale=1):
            # 推流控制
            with gr.Group():
                gr.Markdown("## 推流控制")
                start_btn = gr.Button("开始推流", variant="primary")
                stop_btn = gr.Button("停止推流")
                original_video = gr.Video(label="原始视频预览", interactive=False)
            
            # 字幕设置
            with gr.Group():
                gr.Markdown("## 字幕设置")
                subtitle_text = gr.Textbox(label="字幕内容", placeholder="输入字幕文字")
                with gr.Row():
                    font_selector = gr.Dropdown(["黑体", "宋体", "微软雅黑", "Arial"], 
                                              label="字体选择", value="黑体")
                    font_size = gr.Slider(10, 72, value=32, label="字体大小")
                    position_selector = gr.Dropdown(["顶部居中", "底部居中", "自定义"], 
                                                  label="字幕位置", value="底部居中")
                add_subtitle_btn = gr.Button("添加字幕")
            
            # 剪辑控制
            with gr.Group():
                gr.Markdown("## 剪辑处理")
                start_clip_btn = gr.Button("开启剪辑", variant="primary")
                progress_output = gr.Textbox(label="处理进度", interactive=False)

        # 右侧结果预览
        with gr.Column(scale=2):
            gr.Markdown("## 剪辑结果预览")
            processed_video = gr.Video(label="处理结果", interactive=False)
    
    # 事件绑定
    start_btn.click(
        start_stream,
        outputs=original_video
    )
    
    stop_btn.click(
        stop_stream,
        outputs=original_video
    )
    
    add_subtitle_btn.click(
        add_subtitle,
        inputs=[subtitle_text, font_selector, font_size, position_selector],
        outputs=[progress_output, processed_video]
    )
    
    start_clip_btn.click(
        process_video,
        outputs=[progress_output, processed_video]
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=6688)