如何使用php curl获取youtube视频直接链接文件大小信息?

时间:2015-02-25 17:30:47

标签: php curl youtube

我想在直接链接上显示youtube视频大小的大小。我有一个98 MB大小的mp4格式文件的链接。我想在您浏览此链接时显示尺寸。

直接网址链接:

$url ='https://r4---sn-a8au-p5qs.googlevideo.com/videoplayback?signature=9216B94F5EA16F023DE3D34C6881F8AFA20E1EBA.4B56920C4B2BAB848AC847E0EB4C1E01FB01685C&itag=22&ratebypass=yes&expire=1424904527&id=o-AA8OyXA6gxAGHzkwqf94rIO0LTrhD8iHuUc9lMI9ED76&pl=46&fexp=905657%2C907263%2C916942%2C923382%2C927622%2C934601%2C934954%2C9406984%2C943917%2C947225%2C947240%2C947601%2C948124%2C951703%2C952302%2C952605%2C952612%2C952901%2C955301%2C957201%2C959701&mm=31&ipbits=0&dur=1366.192&mt=1424882845&ms=au&key=yt5&upn=SIiUMnjm5o0&source=youtube&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmm%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&mv=m&initcwndbps=1567500&ip=2001%3A4802%3A7805%3A101%3Abe76%3A4eff%3Afe20%3A31e4&sver=3&requiressl=yes&title=PHP%3A+Create+Your+Own+MVC+%28Part+1%29';

php curl code我用来获取代码

function retrieve_remote_file_size($url){
     $ch = curl_init($url);
     curl_setopt($ch, CURLOPT_URL,$url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_HEADER, TRUE);
     curl_setopt($ch, CURLOPT_NOBODY, TRUE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     $data = curl_exec($ch);
     $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

     curl_close($ch);
     return $size;
}

它给了我1419.如何获得正确的文件大小?

2 个答案:

答案 0 :(得分:0)

getID3支持视频格式。请参阅:http://getid3.sourceforge.net/

$getID3 = new getID3;
 $file = $getID3->analyze($filename);
 echo("Filesize: ".$file['filesize']." bytes<br />");

注意:在此之前必须包含getID3类!见上面的链接。

如果您能够修改服务器上的PHP安装,则为此目的的PHP扩展名为ffmpeg-php。请参阅:http://ffmpeg-php.sourceforge.net/

编辑:

在这里找到一些相关信息:

这是获得远程文件大小的最佳方式(我发现)。请注意,HEAD请求不会获取请求的实际主体,它们只是检索标头。因此,对100MB的资源发出HEAD请求将花费与HEAD请求相同的时间量到1KB的资源。     

  $curl = curl_init( $url );

  // Issue a HEAD request and follow any redirects.
  curl_setopt( $curl, CURLOPT_NOBODY, true );
  curl_setopt( $curl, CURLOPT_HEADER, true );
  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
  curl_setopt( $curl, CURLOPT_USERAGENT, get_user_agent_string() );

  $data = curl_exec( $curl );
  curl_close( $curl );

  if( $data ) {
    $content_length = "unknown";
    $status = "unknown";

    if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
      $status = (int)$matches[1];
    }

    if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
      $content_length = (int)$matches[1];
    }

    // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    if( $status == 200 || ($status > 300 && $status <= 308) ) {
      $result = $content_length;
    }
  }

   return $result;
    }
    ?>

用法:

$file_size = curl_get_file_size(   "http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file" );

答案 1 :(得分:0)

您获得的1491字节是重定向的长度。添加以下curl选项:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True);

根据the documentation,默认情况下应该是真的,但对我来说并非如此。