zoukankan      html  css  js  c++  java
  • PHP 获取视频时长方法

    因为最近做http://www.sdfymj.com/video/ 这个视频站点,需要用到获取视频时长的方法,今天顺便分享给大家

    //获得视频文件的总长度时间和创建时间 根据视频长度判断是否失效
    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
    ";
      $request .= 'Host: ' . $url_parts['host'] . "
    ";
      $request .= "Connection: Close
    
    ";
      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;
    }
    

      

  • 相关阅读:
    SQL over关键字(转载)
    XSLT学习(转载)
    js动态添加options(转载)
    ASP.NET中如何防范SQL注入式攻击(转载)
    Wordpress SEO robots
    RegistryKey类的学习(转载)
    C# Lambda Expressions 简介(转载)
    ‍ps技巧184条(转载)
    2010年年终总结
    JavaScript(文件对象/锚点对象/链接对象)(转载)
  • 原文地址:https://www.cnblogs.com/68xi/p/13453829.html
Copyright © 2011-2022 走看看