PHP Example Code for Obtaining Video Duration

  • 2021-10-16 01:21:00
  • OfStack

The specific code is as follows:


/*
*  Get thumbnails of video files and video lengths 
* @date 2018-05-16
* @copyright
*/
// Get the total length time and creation time of the video file   Judging whether it is invalid according to the length of the video 
public function getTime($url)
{
  // Get the link after video redirection 
  $location = locationUrl($url);
  // Get video Content-Length
  $responseHead = get_data($location);
  $list1 = explode("Content-Length: ", $responseHead);
  $list2 = explode("Connection", $list1[1]);
  $list = explode("x", $list2[0]);
  return $list[0];
}
// Get the link after video redirection 
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;
  }
}
// Audit video  curl
function get_data($url){
  $oCurl = curl_init();
  // Analog browser 
  $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 );
  //  No need  POST  Mode request ,  It means through  GET  Request 
  curl_setopt($oCurl, CURLOPT_POST, false);
  $sContent = curl_exec($oCurl);
  //  Get the header size in the response result 
  $headerSize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);
  //  The header information content is obtained according to the header size 
  $header = substr($sContent, 0, $headerSize);
  curl_close($oCurl);
  return $header;
}

Summarize


Related articles: