#!/usr/bin/env python3
"""
拼接完整双语视频（432句）
"""
import os
import subprocess

video_dir = "shitu_bilingual_video"
output_file = "shitu_bilingual_full.mp4"

# 生成片段列表文件
list_file = "shitu_temp/concat_full_list.txt"
if not os.path.exists("shitu_temp"):
    os.makedirs("shitu_temp")

print("生成视频拼接列表...")
with open(list_file, 'w', encoding='utf-8') as f:
    for i in range(432):
        idx = f"{i:03d}"
        video_file = os.path.join(os.path.abspath(video_dir), f"bilingual_video_{idx}.mp4")
        if os.path.exists(video_file):
            f.write(f"file '{video_file.replace('\\', '/')}'\n")
        else:
            print(f"  警告: 句{i}视频不存在")

# 拼接视频
print("\n拼接完整视频...")
cmd = [
    'ffmpeg', '-y',
    '-f', 'concat',
    '-safe', '0',
    '-i', list_file,
    '-c', 'copy',
    output_file
]

result = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8', errors='ignore')

if result.returncode == 0:
    # 获取总时长
    probe_cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration',
                 '-of', 'default=noprint_wrappers=1:nokey=1', output_file]
    probe_result = subprocess.run(probe_cmd, capture_output=True, text=True)
    duration = float(probe_result.stdout.strip()) if probe_result.returncode == 0 else 0

    print(f"\n完成！完整双语视频保存在: {output_file}")
    print(f"总时长: {duration:.2f}s ({duration/60:.2f}分钟)")
else:
    print(f"\n错误: {result.stderr[-300:]}")

# 清理临时文件
if os.path.exists(list_file):
    os.remove(list_file)
