【问题标题】:Play mp4 file through php in HTML5 Video Tag in Chrome?在 Chrome 的 HTML5 视频标签中通过 php 播放 mp4 文件?
【发布时间】:2015-03-02 13:43:09
【问题描述】:

我对 mp4 文件有疑问。 Html5 Video Tag 可以通过直接链接播放,但不能通过 PHP Header 播放(MP3 可以与 PHP Header 一起正常工作)。我已经在 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>

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

谢谢,

【问题讨论】:

    标签: php video mp4


    【解决方案1】:

    好的!我已经解决了我的问题:) 希望这会帮助任何和我有同样问题的人

    只需使用该代码:

    $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;
    }
    

    【讨论】:

      【解决方案2】:

      我认为您在 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>
      

      【讨论】:

      • 感谢您的回答 :) 我试过了,但还是不行
      • 您的 mp4 文件可能有问题。你试过另一个吗?
      • 是的,我尝试了其他文件。他们也没有工作:)
      猜你喜欢
      • 2016-04-26
      • 1970-01-01
      • 2013-05-19
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多