#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查音频文件的内容是否正确对应字幕
"""

import os
import subprocess
import re


def get_audio_duration(audio_path):
    """
    获取音频文件时长（秒）
    """
    try:
        cmd = [
            "ffprobe",
            "-v", "quiet",
            "-show_entries", "format=duration",
            "-of", "default=noprint_wrappers=1:nokey=1",
            audio_path
        ]
        
        result = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8', errors='ignore')
        if result.returncode == 0:
            try:
                duration = float(result.stdout.strip())
                return duration
            except:
                return 0
        else:
            return 0
    except Exception as e:
        return 0


def check_audio_content():
    """
    检查音频文件的内容是否正确对应字幕
    """
    # 检查中文音频文件
    seg_dir = "我们需要知识分子吗_思考_segments"
    seg_files = []
    for filename in os.listdir(seg_dir):
        if filename.endswith('.mp3') and filename.startswith('seg'):
            match = re.match(r'seg(\d+)-', filename)
            if match:
                idx = int(match.group(1))
                seg_files.append((idx, filename))
    
    seg_files.sort(key=lambda x: x[0])
    
    print(f"找到 {len(seg_files)} 个中文音频文件")
    print("\n前10个中文音频文件及其时长:")
    for idx, filename in seg_files[:10]:
        audio_path = os.path.join(seg_dir, filename)
        duration = get_audio_duration(audio_path)
        print(f"idx: {idx}, filename: {filename}, duration: {duration:.2f} 秒")


if __name__ == "__main__":
    check_audio_content()
