Play mp4 file through php in HTML5 Video Tag in Chrome?

二次信任 提交于 2019-12-18 05:21:00

问题


I have a problem with mp4 files. Html5 Video Tag can play it with direct link, but not with PHP Header (Mp3 is worked fine with PHP Header). I have tried almost of solution in Stack but still not resolve my problem :(

Hear is my code:

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>

I don't know why :( Please help me.

Thanks,


回答1:


OK! I've resolved my problem :) Hope this will help anyone has the same problem as me

Just use that code:

$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:


I think you've added unnecessary headers to your php file. Try this one:

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

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

readfile($local_file);

And a HTML code something like this

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

EDIT: This will also work for mp3 files when you change Content-Type header and html video type.

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


来源:https://stackoverflow.com/questions/24596450/play-mp4-file-through-php-in-html5-video-tag-in-chrome

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!