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

echo "<h1>Range请求测试</h1>";

require_once 'config.php';
global $pdo;

$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 2;");
$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;

foreach ($videos as $video) {
    $filePath = __DIR__ . '/' . $video['storage_path'] . $video['file_name'];
    $fileExists = file_exists($filePath);
    
    echo "<h2>" . $video['video_name'] . "</h2>";
    
    if (!$fileExists) {
        echo "<p style='color:red'>文件不存在</p>";
        continue;
    }
    
    $fileSize = filesize($filePath);
    echo "<p>文件大小: " . round($fileSize / 1024 / 1024, 1) . "MB</p>";
    
    $relativeUrl = $video['storage_path'] . $video['file_name'];
    $directUrl = $baseUrl . '/' . $relativeUrl;
    $streamUrl = $baseUrl . '/stream_video.php?id=' . $video['video_id'];
    
    echo "<h3>直接播放（原方式）</h3>";
    echo "<p>URL: " . $directUrl . "</p>";
    echo "<video width='400' controls>";
    echo "<source src='" . $relativeUrl . "' type='video/mp4'>";
    echo "不支持";
    echo "</video>";
    
    echo "<h3>流式播放（新方式）</h3>";
    echo "<p>URL: " . $streamUrl . "</p>";
    echo "<video width='400' controls>";
    echo "<source src='stream_video.php?id=" . $video['video_id'] . "' type='video/mp4'>";
    echo "不支持";
    echo "</video>";
    
    echo "<hr>";
}

echo "<h2>测试结果分析</h2>";
echo "<p><strong>如果直接播放无法播放，但流式播放可以：</strong>说明是服务器不支持Range请求</p>";
echo "<p><strong>如果两种方式都无法播放：</strong>说明是视频编码格式问题</p>";
echo "<p><strong>如果两种方式都可以播放：</strong>说明之前的问题已经解决</p>";
?>
