Youtube video downloader
Youtube Downloader is PSR-0 compliant and can be installed using composer. Simply add masih/youtubedownloader
to your composer.json file.
{
"require": {
"masih/youtubedownloader": "~1.6"
}
}
and run composer update
command
Needs PHP 5.5 or newer. Tested with PHP 5.5, 5.6, 7.0, hhvm and nightly
To get video infos, you should instantiate YoutubeDownloader
with video url or video id.
for example for http://youtube.com/watch?v=gmFn62dr0D8
, video id is gmFn62dr0D8
<?php
include 'vendor/autoload.php';
use Masih\YoutubeDownloader\YoutubeDownloader;
$youtube = new YoutubeDownloader('gmFn62dr0D8');
$result = $youtube->getVideoInfo();
header('Content-type: application/json');
print json_encode($result);
the getVideoInfo()
method will return an object containing video title, images, url and itag
of all formats (full and adaptive), ...
Video formats are in two category; Full & adaptive
in Full formats, videos and sounds are muxed and are in one file. but in Adaptive formats, videos and sounds are in separated urls.
Each format has it's own itag
. it's just an identifier
the download()
method gets itag
of a format and downloads it.
if no itag
is passed, it will download highest quality of Full format.
<?php
set_time_limit(0); // Downloading a video may take a lot time
include 'vendor/autoload.php';
use Masih\YoutubeDownloader\YoutubeDownloader;
$youtube = new YoutubeDownloader('gmFn62dr0D8');
$youtube->download();
Download progress is available via onProgress
parameter.
it's a closure and has two parameters $downloadedBytes
and $fileSize
.
<?php
$youtube = new YoutubeDownloader('gmFn62dr0D8');
$youtube->onProgress = function ($downloadedBytes, $fileSize) {
if ($fileSize > 0)
echo 'Downloaded ' . $downloadedBytes . ' of ' . $fileSize . ' bytes [%' . number_format($downloadedBytes * 100 / $fileSize, 2) . '].' . "\n";
else
echo 'Downloading...'; // File size is unknown, so just keep downloading
};
$youtube->download();
Download complete event is available via onComplete
parameter.
it's a closure and has two parameters $filePath
and $fileSize
.
<?php
$youtube = new YoutubeDownloader('gmFn62dr0D8');
$youtube->onComplete = function ($filePath, $fileSize) {
echo 'Downloading of ' . $fileSize . ' bytes has been completed. It is saved in ' . $filePath . "\n";
};
$youtube->download();
MIT, see LICENSE.