<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once 'config.php';
global $pdo;

echo "<h1>视频编码检查</h1>";

$stmt = $pdo->prepare("SELECT video_id, video_name, storage_path, file_name FROM video_assets WHERE video_type='opening' ORDER BY created_at DESC LIMIT 3;");
$stmt->execute();
$videos = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($videos as $video) {
    $filePath = __DIR__ . '/' . $video['storage_path'] . $video['file_name'];
    
    echo "<h2>" . $video['video_name'] . "</h2>";
    echo "<p>文件路径: " . $filePath . "</p>";
    
    if (!file_exists($filePath)) {
        echo "<p style='color:red'>文件不存在!</p>";
        continue;
    }
    
    $command = escapeshellcmd('ffprobe -v quiet -print_format json -show_streams "' . $filePath . '"');
    $output = shell_exec($command);
    
    if ($output) {
        $info = json_decode($output, true);
        if ($info && isset($info['streams'])) {
            foreach ($info['streams'] as $stream) {
                echo "<h3>" . ($stream['codec_type'] === 'video' ? '视频流' : '音频流') . "</h3>";
                echo "<table border='1' cellpadding='5'>";
                echo "<tr><td>编码</td><td>" . ($stream['codec_name'] ?? '未知') . "</td></tr>";
                echo "<tr><td>编码类型</td><td>" . ($stream['codec_long_name'] ?? '未知') . "</td></tr>";
                echo "<tr><td>容器格式</td><td>" . ($stream['codec_tag_string'] ?? '未知') . "</td></tr>";
                if (isset($stream['width'])) {
                    echo "<tr><td>分辨率</td><td>" . $stream['width'] . " x " . $stream['height'] . "</td></tr>";
                }
                if (isset($stream['duration'])) {
                    echo "<tr><td>时长</td><td>" . $stream['duration'] . "秒</td></tr>";
                }
                if (isset($stream['r_frame_rate'])) {
                    echo "<tr><td>帧率</td><td>" . $stream['r_frame_rate'] . "</td></tr>";
                }
                echo "</table>";
                
                if ($stream['codec_type'] === 'video') {
                    $codec = strtolower($stream['codec_name']);
                    if ($codec === 'h264' || $codec === 'mpeg4') {
                        echo "<p style='color:green'>✓ 视频编码兼容浏览器 (H.264/MPEG4)</p>";
                    } else if ($codec === 'h265' || $codec === 'hevc') {
                        echo "<p style='color:red'>✗ 视频编码可能不兼容浏览器 (H.265/HEVC)，建议转换为H.264</p>";
                    } else {
                        echo "<p style='color:orange'>⚠ 视频编码: " . $codec . "，请测试兼容性</p>";
                    }
                }
            }
        } else {
            echo "<p>无法解析视频信息</p>";
        }
    } else {
        echo "<p>ffprobe命令执行失败，请检查ffmpeg是否已安装</p>";
    }
    
    echo "<hr>";
}

echo "<h2>浏览器兼容性说明</h2>";
echo "<ul>";
echo "<li><strong>H.264 (AVC)</strong>: 所有现代浏览器支持</li>";
echo "<li><strong>H.265 (HEVC)</strong>: Safari支持，Chrome/Edge有限支持</li>";
echo "<li><strong>VP9</strong>: Chrome/Edge/Firefox支持，Safari不支持</li>";
echo "<li><strong>AV1</strong>: 最新浏览器支持</li>";
echo "</ul>";
?>