Using php to output an mp4 video

前端 未结 4 1445
感动是毒
感动是毒 2020-11-27 13:33

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.

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 14:17

    Here is a code snippet that will do what you want (from this question). The PHP solution seems more elegant, and it adds a more efficient solution that might work that uses the web server to serve the content.

    0||$end<$size)
      header('HTTP/1.0 206 Partial Content');
    else
      header('HTTP/1.0 200 OK');
    
    header("Content-Type: video/mp4");
    header('Accept-Ranges: bytes');
    header('Content-Length:'.($end-$begin));
    header("Content-Disposition: inline;");
    header("Content-Range: bytes $begin-$end/$size");
    header("Content-Transfer-Encoding: binary\n");
    header('Connection: close');
    
    $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();
    

    More Performance

    Note that this is not the most efficient way to do it, because the whole file needs to go through PHP, so you will just need to try how it goes for you.

    Assuming the reason you want to do this is to restrict access, and you need more efficiency later, you can use a flag for the web server.

    Apache with X-Sendfile module or lightty (nginx info here)

    $path = 'file.mp4';
    header("X-Sendfile: $path");
    die();
    

    This is a bit more advanced and you should only use it if you need it, but it is relaxing to know you have an upgrade option when you start out with something that is rather easy but has mediocre performance.

提交回复
热议问题