问题
I would like to show an mp4 through the video tag and the source URL of the video is a URL to a YII application. I was using Yii:app->request->sendFile before but the video wasn't working on the iPad/iPhone so now I'm trying to send the headers myself using the code below but still not working.
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file_path = "video.mp4";
$file_mime_type = finfo_file($finfo, $file_path);
$file_size = filesize($file_path);
header("HTTP/1.1 206 Partial Content");
header("Accept-Ranges: bytes");
header("Content-Type: $file_mime_type");
header("Content-Length: $file_size");
header("Content-Range: bytes 0-".($file_size-1)."/$file_size");
readfile($file_path);
exit;
I even tried to implement the rangeDownload function from http://mobiforge.com/developing/story/content-delivery-mobile-devices but the problem is that the $_SERVER['HTTP_RANGE'] is always null
even when the request is coming from an iPhone/iPad.
I also tried this solution here mp4 file through php not playing as html5 video but to no avail again..
The code above works fine for a web browser. Also if I access the .mp4 directly from the iPhone/iPad it works fine too so it is not a problem with the video itself
Any help please?
回答1:
Your problem may be from several reason.
1) You can't create a right video. Try to use a string like this:
c:\utils\ffmpeg\bin\ffmpeg -i MVI_7386.MOV -acodec aac -ac 2 -strict experimental -b:a 160k -s 640x480 -vcodec libx264 -preset slow -profile:v baseline -level 30 -maxrate 10000000 -bufsize 10000000 -b:v 1200k -pix_fmt yuv420p -f mp4 -threads 2 -async 1 -vsync 1 -y video.ipad.mp4
2) I used this answer with small changes for send video via php. Here is my video.php file:
<?php
$path = './video.ipad.mp4';
if (file_exists($path)) {
$size=filesize($path);
$fm=@fopen($path,'rb');
if(!$fm) {
// You can also redirect here
header ("HTTP/1.1 404 Not Found");
die();
}
$begin=0;
$end=$size;
if(isset($_SERVER['HTTP_RANGE'])) {
if(preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i',
$_SERVER['HTTP_RANGE'],$matches)){
$begin=intval($matches[1]);
if(!empty($matches[2])) {
$end=intval($matches[2]);
}
}
}
if($begin>0||$end<$size) header('HTTP/1.1 206 Partial Content');
else header('HTTP/1.1 200 OK');
header("Content-Type: video/mp4");
header('Content-Length:'.($end-$begin));
header("Content-Range: bytes $begin-$end/$size");
$cur=$begin;
fseek($fm,$begin,0);
while(!feof($fm)&&$cur<$end&&(connection_status()==0)) {
print fread($fm,min(1024*16,$end-$cur));
$cur+=1024*16;
usleep(1000);
}
die();
}
And the html is very simple:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>test</title>
</head>
<body>
<video controls>
<source src="./video.php" type="video/mp4">
</video>
</body>
</html>
You can see what I done here.
P.S. Sorry for video. :) I could not find anoter one.
来源:https://stackoverflow.com/questions/17966493/show-video-on-iphone-ipad-through-php