欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP 获取视频时长的实例代码

程序员文章站 2022-11-21 11:37:22
具体代码如下所示: /* * 获得视频文件的缩略图和视频长度 * @date 2018-05-16 * @copyright */ //获得视频文件...

具体代码如下所示:

/*
* 获得视频文件的缩略图和视频长度
* @date 2018-05-16
* @copyright
*/
//获得视频文件的总长度时间和创建时间 根据视频长度判断是否失效
public function gettime($url)
{
  //获取视频重定向后的链接
  $location = locationurl($url);
  //获取视频content-length
  $responsehead = get_data($location);
  $list1 = explode("content-length: ", $responsehead);
  $list2 = explode("connection", $list1[1]);
  $list = explode("x", $list2[0]);
  return $list[0];
}
//获取视频重定向后的链接
function locationurl($url){
  $url_parts = @parse_url($url);
  if (!$url_parts) return false;
  if (!isset($url_parts['host'])) return false;
  if (!isset($url_parts['path'])) $url_parts['path'] = '/';
  $sock = fsockopen($url_parts['host'], (isset($url_parts['port']) ? (int)$url_parts['port'] : '80'), $errno, $errstr, 30);
  if (!$sock) return false;
  $request = "head " . $url_parts['path'] . (isset($url_parts['query']) ? '?'.$url_parts['query'] : '') . " http/1.1\r\n";
  $request .= 'host: ' . $url_parts['host'] . "\r\n";
  $request .= "connection: close\r\n\r\n";
  fwrite($sock, $request);
  $response = '';
  while(!feof($sock)) {
    $response .= fread($sock, 8192);
  }
  fclose($sock);
  if (preg_match('/^location: (.+?)$/m', $response, $matches)){
    if ( substr($matches[1], 0, 1) == "/" ){
      return $url_parts['scheme'] . "://" . $url_parts['host'] . trim($matches[1]);
    }
    else{
      return trim($matches[1]);
    }
  } else {
    return false;
  }
}
//审核视频 curl
function get_data($url){
  $ocurl = curl_init();
  //模拟浏览器
  $header[] = "deo.com";
  $user_agent = "mozilla/4.0 (linux; andro 6.0; nexus 5 build) appleweb/537.36 (khtml, like gecko)";
  curl_setopt($ocurl, curlopt_url, $url);
  curl_setopt($ocurl, curlopt_httpheader,$header);
  curl_setopt($ocurl, curlopt_header, true);
  curl_setopt($ocurl, curlopt_nobody, true);
  curl_setopt($ocurl, curlopt_useragent,$user_agent);
  curl_setopt($ocurl, curlopt_returntransfer, 1 );
  // 不用 post 方式请求, 意思就是通过 get 请求
  curl_setopt($ocurl, curlopt_post, false);
  $scontent = curl_exec($ocurl);
  // 获得响应结果里的:头大小
  $headersize = curl_getinfo($ocurl, curlinfo_header_size);
  // 根据头大小去获取头信息内容
  $header = substr($scontent, 0, $headersize);
  curl_close($ocurl);
  return $header;
}

总结

以上所述是小编给大家介绍的php 获取视频时长的实例代码,希望对大家有所帮助