在Chrome中的HTML5视频标签中通过php播放mp4文件?

时间:2014-07-06 13:42:01

标签: php video mp4

我的mp4文件有问题。 Html5视频标签可以使用直接链接播放,但不能使用PHP标头播放(Mp3可以正常使用PHP标头)。我在Stack中尝试了几乎所有的解决方案,但仍然无法解决我的问题:(

听到我的代码:

PHP mp4.php

    error_reporting(0);

    $local_file = 'z.mp4';//'b.mp3';
    $download_file = 'out.mp4';
    // send headers
    header('Cache-control: private');
    header('Content-Type: video/mp4');
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$download_file);
    header('Accept-Ranges: bytes');
    header("Content-Transfer-Encoding: binary");
    readfile($local_file);

HTML

     <video controls="" autoplay="" name="media">
          <source src="http://localhost/videos/mp4.php" type="video/mp4">
     </video>

我不知道为什么:(请帮助我。

谢谢,

2 个答案:

答案 0 :(得分:6)

OK!我已经解决了我的问题:)希望这会帮助任何人遇到和我一样的问题

只需使用该代码:

$file = 'local_file';
$newfile = 'outFile';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($newfile));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

答案 1 :(得分:3)

我认为你已经为你的php文件添加了不必要的标题。试试这个:

$local_file = 'z.mp4';
$size = filesize($local_file);

header("Content-Type: video/mp4");
header("Content-Length: ".$size);

readfile($local_file);

这样的HTML代码

<video width="480" height="320" controls>
  <source src="mp4.php" type="video/mp4">
</video>

编辑:当您更改Content-Type标头和html视频类型时,这也适用于mp3文件。

<video width="480" height="320" controls>
  <source src="mp3.php" type="audio/mpeg">
</video>
相关问题