【问题标题】:mp4 file through php not playing as html5 video通过php的mp4文件不作为html5视频播放
【发布时间】:2011-12-01 21:55:45
【问题描述】:

我正在尝试通过 PHP 输出一个 mp4 视频文件。 当它通过 Flash 播放器(例如 flowplayer)使用时,它工作得很好。 但是当我尝试将它用作 html5 视频标签的源或直接调用 php 文件时,它不起作用。

我使用的代码如下:

        $filesize = filesize($file);
        header("Content-Type: video/mp4");

        if ( empty($_SERVER['HTTP_RANGE']) )
        {
            header("Content-Length: $filesize");
            readfile($file);
        }
        else //violes rfc2616, which requires ignoring  the header if it's invalid
        {   
            rangeDownload($file);
        }

rangeDownload 函数来自http://mobiforge.com/developing/story/content-delivery-mobile-devices 附录A。

即使我使用 Content-Range 标头 (Content-Range:bytes 0-31596111/31596112),它仍然无法下载 30.13 MB 的视频。

【问题讨论】:

  • 那么,你的 php.ini max_execution_time 比视频长吗?您是否探究了静态文件和已读取文件之间的其他标头差异(Firebug)?
  • 我建议检查 php.ini 中的最大内存设置 - 似乎您用于向客户端发送视频的两个函数都将整个文件读入内存然后将其发送给用户,您可能需要阅读并使用小块(例如 2MB)发送。
  • mp4 不推荐用于 html5 视频。在这里查看哪些浏览器当前支持 html5 视频的哪些视频文件类型的图表。 en.wikipedia.org/wiki/HTML5_video
  • @mario 我已将 'max_execution_time' 设置为 0,但没有任何改变。 Alex Z:memory_limit 是 250M,我相信已经足够了

标签: php mp4 flowplayer


【解决方案1】:

我终于找到了让它工作的方法

header("Content-Type: $mediatype");

if ( empty($_SERVER['HTTP_RANGE']) )
{
    header("Content-Length: $filesize");

    $fh = fopen($file, "rb") or die("Could not open file: " .$file);

    # output file
    while(!feof($fh))
    {
         # output file without bandwidth limiting
        echo fread($fh, $filesize);
    }
    fclose($fh);
}
else //violes rfc2616, which requires ignoring  the header if it's invalid
{   
     rangeDownload($file);
}

它在 php 文件的直接链接和 html5 视频标签内工作。
但是为了在 Flowplayer 中工作(可能在其他 flash/html5 播放器中),您需要添加一个 mp4 扩展名(例如 view.php?id=XXX&file=type.mp4)

【讨论】:

    【解决方案2】:

    这可能与您的浏览器以及它用于查看视频文件的插件有关,即)quicktime。它与 Flash 一起工作的原因是 Flash 处理缓冲和时间同步等。通常不建议让浏览器处理播放媒体文件,因为这完全取决于浏览器配置和它们安装的插件。

    一些浏览器会自动下载媒体文件,它完全可以由浏览器和最终用户配置。

    【讨论】:

    • 感谢 Matthew 的建议,但直接访问时,mp4 视频在 Safari/Chrome 中完美播放。问题是当我出于安全原因将它包装在 PHP 文件中时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2012-07-20
    • 2012-05-16
    相关资源
    最近更新 更多