使用PHP输出一个MP4视频 [英] Using php to output an mp4 video

查看:226
本文介绍了使用PHP输出一个MP4视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个项目,要求用户隐藏视频,但仍然能够看到它们(通过使用php)。这是我到目前为止所得到的:



video.php文件包含:

 <?php 
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,'path / to / movie.mp4');
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_HEADER,0);
$ out = curl_exec($ ch);
curl_close($ ch);


header('Content-type:video / mp4');
header('Content-type:video / mpeg');
header('Content-disposition:inline');
头(Content-Transfer-Encoding:binary);
header(Content-Length:.filesize($ out));
echo $ out;
exit();
?>

和应该显示的html文件使用的是html5。现在这里的东西..当我直接嵌入此(不)它的作品。但它不能在我的iPhone上工作,并且不能在标签中工作......如果我使用直接文件而不是php包装,那么在我的iPhone上一切正常......



所以我想我的问题是这样的:什么是适当的header()信息来完美地复制可以通过iPhone和HMTL5流式传输的mp4?



解决方案来源于: http://mobiforge.com/developing / story / content-delivery-mobile-devices

video.php文件:

 <?php 
$ file ='path / to / videofile.mp4';
$ fp = @fopen($ file,'rb');

$ size = filesize($ file); //文件大小
$ length = $ size; //内容长度
$ start = 0; //开始字节
$ end = $ size - 1; //结束字节

header('Content-type:video / mp4');
header(Accept-Ranges:0- $ length);
if(isset($ _ SERVER ['HTTP_RANGE'])){

$ c_start = $ start;
$ c_end = $ end;

list(,$ range)= explode('=',$ _SERVER ['HTTP_RANGE'],2);
if(strpos($ range,',')!== false){
header('HTTP / 1.1 416 Requested Range Not Satisfiable');
header(Content-Range:bytes $ start- $ end / $ size);
退出;
}
if($ range ==' - '){
$ c_start = $ size - substr($ range,1);
} else {
$ range = explode(' - ',$ range);
$ c_start = $ range [0];
$ c_end =(isset($ range [1])&& is_numeric($ range [1]))? $ range [1]:$ size;
}
$ c_end =($ c_end> $ end)? $结束:$ c_end;
if($ c_start> $ c_end || $ c_start> $ size - 1 || $ c_end> = $ size){
header('HTTP / 1.1 416 Requested Range Not Satisfiable') ;
header(Content-Range:bytes $ start- $ end / $ size);
退出;
}
$ start = $ c_start;
$ end = $ c_end;
$ length = $ end - $ start + 1;
fseek($ fp,$ start);
header('HTTP / 1.1 206 Partial Content');
}
header(Content-Range:bytes $ start- $ end / $ size);
header(Content-Length:。$ length);


$ buffer = 1024 * 8; $($ fp)&&($ p = ftell($ fp))< = $ end){

if($ p + $ buffer> $ end){
$ buffer = $ end - $ p + 1;
}
set_time_limit(0);
echo fread($ fp,$ buffer);
flush();
}

fclose($ fp);
exit();
?>


解决方案

Iphones对音频和视频使用称为字节范围要求。请参阅此链接以获取解决方案。它在附录A中。

http:// mobiforge.com/developing/story/content-delivery-mobile-devices


Ok basically I have a project that requires that videos are hidden from the users while still able to see them (by using php). here's what i got so far:

The video.php file has this:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'path/to/movie.mp4');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HEADER, 0);
$out = curl_exec($ch);
curl_close($ch);


header('Content-type: video/mp4');
header('Content-type: video/mpeg');
header('Content-disposition: inline');
header("Content-Transfer-Encoding:­ binary");
header("Content-Length: ".filesize($out));
echo $out;
exit();
?>

and the html file that is supposed to display this is using html5 as it would expect. now here's the thing.. when I straight embed this (not ) it works. but it doesn't work on my iPhone and doesn't work in the tag... if I use the direct file instead of the php wrapper, everything works fine, on my iPhone too...

so I guess my question for this one is this: what are the proper header() information to perfectly replicate an mp4 that can be streamed via iPhone and HMTL5?

Solution derived from: http://mobiforge.com/developing/story/content-delivery-mobile-devices

video.php file:

<?php
$file = 'path/to/videofile.mp4';
$fp = @fopen($file, 'rb');

$size   = filesize($file); // File size
$length = $size;           // Content length
$start  = 0;               // Start byte
$end    = $size - 1;       // End byte

header('Content-type: video/mp4');
header("Accept-Ranges: 0-$length");
if (isset($_SERVER['HTTP_RANGE'])) {

    $c_start = $start;
    $c_end   = $end;

    list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2);
    if (strpos($range, ',') !== false) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    if ($range == '-') {
        $c_start = $size - substr($range, 1);
    }else{
        $range  = explode('-', $range);
        $c_start = $range[0];
        $c_end   = (isset($range[1]) && is_numeric($range[1])) ? $range[1] : $size;
    }
    $c_end = ($c_end > $end) ? $end : $c_end;
    if ($c_start > $c_end || $c_start > $size - 1 || $c_end >= $size) {
        header('HTTP/1.1 416 Requested Range Not Satisfiable');
        header("Content-Range: bytes $start-$end/$size");
        exit;
    }
    $start  = $c_start;
    $end    = $c_end;
    $length = $end - $start + 1;
    fseek($fp, $start);
    header('HTTP/1.1 206 Partial Content');
}
header("Content-Range: bytes $start-$end/$size");
header("Content-Length: ".$length);


$buffer = 1024 * 8;
while(!feof($fp) && ($p = ftell($fp)) <= $end) {

    if ($p + $buffer > $end) {
        $buffer = $end - $p + 1;
    }
    set_time_limit(0);
    echo fread($fp, $buffer);
    flush();
}

fclose($fp);
exit();
?>

解决方案

Iphones use something called byte-ranges for audio and video requests. See this link for a solution. It's in Appendix A.

http://mobiforge.com/developing/story/content-delivery-mobile-devices

这篇关于使用PHP输出一个MP4视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆