通过PHP在iPhone / iPad上显示视频 [英] Show Video on iPhone/iPad through PHP

查看:158
本文介绍了通过PHP在iPhone / iPad上显示视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过视频标记显示mp4,视频的源URL是YII应用程序的URL。之前我正在使用Yii:app-> request-> sendFile,但视频无法在iPad / iPhone上运行,所以现在我正在尝试使用下面的代码自己发送标题,但仍无法正常工作。

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;

我甚至尝试从 http://mobiforge.com/developing/story/content-delivery-mobile-devices 但问题是$即使请求来自iPhone / iPad,_SERVER ['HTTP_RANGE']也总是 null

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也试过这个解决方案 mp4文件通过php不播放为html5视频但无济于事..

I also tried this solution here mp4 file through php not playing as html5 video but to no avail again..

上述代码适用于网络浏览器。此外,如果我直接从iPhone / iPad访问.mp4,它也能正常工作,因此视频本身不会出现问题

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

请帮忙吗?

推荐答案

您的问题可能有多种原因。
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)我用过这个答案,通过php发送视频的小改动。这是我的video.php文件:

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();
}

而且html非常简单:

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.

PS对不起视频。 :)我找不到一个。

这篇关于通过PHP在iPhone / iPad上显示视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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