Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return size of each video with result #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/YouTubeDownloader.php
Expand Up @@ -136,10 +136,14 @@ public function parsePlayerResponse($player_response, $js_code)
// some videos do not need to be decrypted!
if (isset($item['url'])) {

$size = $this->getFileSize($item['url']);

$return[] = array(
'url' => $item['url'],
'itag' => $itag,
'format' => $parser->parseItagInfo($itag)
'format' => $parser->parseItagInfo($itag),
'size' => $size

);

continue;
Expand Down Expand Up @@ -208,4 +212,28 @@ public function getDownloadLinks($video_id, $selector = false)

return $result;
}

public function getFileSize($url)
{
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);

$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);

curl_close($ch);
return $this->convertToReadableSize($size);

}

public function convertToReadableSize($size)
{
$base = log($size) / log(1024);
$suffix = array("", "KB", "MB", "GB", "TB");
$f_base = floor($base);
return round(pow(1024, $base - floor($base)), 1) . ' '.$suffix[$f_base];
}
}