<?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, duration, is_active FROM video_assets WHERE video_type='opening' ORDER BY created_at DESC LIMIT 5;");
$stmt->execute();
$videos = $stmt->fetchAll(PDO::FETCH_ASSOC);

$isSecure = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
$scheme = $isSecure ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'];
$scriptDir = dirname(dirname($_SERVER['PHP_SELF']));
$scriptDir = str_replace('\\', '/', $scriptDir);
if ($scriptDir === '/' || $scriptDir === '\\') {
    $scriptDir = '';
}
$baseUrl = $scheme . '://' . $host . $scriptDir;

echo "<h2>站点信息</h2>";
echo "<p>HTTP_HOST: " . $host . "</p>";
echo "<p>Base URL: " . $baseUrl . "</p>";

echo "<h2>视频列表</h2>";

foreach ($videos as $video) {
    $filePath = __DIR__ . '/' . $video['storage_path'] . $video['file_name'];
    $fileExists = file_exists($filePath);
    
    echo "<div style='border: 1px solid #ccc; padding: 15px; margin-bottom: 15px;'>";
    echo "<h3>" . $video['video_name'] . "</h3>";
    echo "<p>ID: " . $video['video_id'] . "</p>";
    echo "<p>存储路径: " . $video['storage_path'] . "</p>";
    echo "<p>文件名: " . $video['file_name'] . "</p>";
    
    if ($fileExists) {
        $fileSize = filesize($filePath);
        $finfo = new finfo(FILEINFO_MIME_TYPE);
        $mime = $finfo->file($filePath);
        
        $handle = fopen($filePath, 'rb');
        $fileHeader = '';
        if ($handle) {
            $fileHeader = bin2hex(fread($handle, 32));
            fclose($handle);
        }
        
        echo "<p>文件大小: " . round($fileSize / 1024 / 1024, 1) . "MB</p>";
        echo "<p>MIME类型: " . $mime . "</p>";
        echo "<p>文件头(前32字节): " . $fileHeader . "</p>";
        
        $isMp4 = strpos($fileHeader, '0000001866747970') === 0 || 
                 strpos($fileHeader, '0000002066747970') === 0;
        echo "<p>是否有效MP4: " . ($isMp4 ? '<span style="color:green">是 ✓</span>' : '<span style="color:red">否 ✗</span>') . "</p>";
        
        $relativeUrl = $video['storage_path'] . $video['file_name'];
        $playUrl = $baseUrl . '/' . $relativeUrl;
        
        echo "<h4>播放测试</h4>";
        echo "<p>相对路径: " . $relativeUrl . "</p>";
        echo "<p>完整URL: " . $playUrl . "</p>";
        echo "<video width='600' controls>";
        echo "<source src='" . $relativeUrl . "' type='video/mp4'>";
        echo "您的浏览器不支持视频播放";
        echo "</video>";
        
        if (!$isMp4) {
            echo "<p style='color:red; font-weight:bold;'>⚠ 警告：文件头不是有效的MP4格式，可能导致无法播放</p>";
        }
    } else {
        echo "<p style='color:red'>文件不存在!</p>";
    }
    
    echo "</div>";
}

echo "<h2>常见问题</h2>";
echo "<ul>";
echo "<li><strong>文件头检查</strong>: MP4文件应以 '0000001866747970' 或 '0000002066747970' 开头</li>";
echo "<li><strong>MIME类型</strong>: 应为 'video/mp4'</li>";
echo "<li><strong>编码要求</strong>: 浏览器只支持 H.264 + AAC 编码的MP4文件</li>";
echo "<li><strong>H.265/HEVC</strong>: 大多数浏览器不支持，需要转换为H.264</li>";
echo "</ul>";

echo "<h2>格式转换建议</h2>";
echo "<p>如果视频无法播放，可以使用ffmpeg转换格式：</p>";
echo "<pre>ffmpeg -i input.mp4 -c:v libx264 -c:a aac -strict -2 output.mp4</pre>";
echo "<p>这条命令将视频转换为标准的H.264+AAC编码，确保所有浏览器都能播放</p>";
?>
