PHP wrapper for youtube-dl or yt-dlp.
Two goals:
- be close to the command line of youtube-dl
- minimize requests, while having the maximum amount of information
- php >= 7.4
- youtube-dl or yt-dlp
- Use the package manager composer to install Ytdl.
composer require flatgreen/ytdl
- Optional: Create a 'cache' directory (with read|write permissions), by default the cache directory is inside the system temporary directory.
require_once 'vendor/autoload.php';
use Flatgreen\Ytdl\Options;
use Flatgreen\Ytdl\Ytdl;
All in array manner. No URL here.
see : options for youtube-dl or options for yt-dlp
$ytdl_options = new Options();
// merge with default options
$ytdl_options->addOptions(['-f' => '18/worst']);
Instantiate the class, define a video url;
$ytdl = new Ytdl($ytdl_options);
// optional, change cache options
// default temporary cache directory and duration 3600 sec.
// no cache with ['duration' => 0]
$ytdl->setCache(['directory' => 'cache', 'duration' => 7200])
$webpage_url = 'https://www.youtube.com/watch?v=BaW_jenozKc';
Read the video informations
$info_dict = $ytdl->extractInfos($webpage_url);
$errors = $ytdl->getErrors();
If you want the url of the media ($info_dict['url']), you must pass a format ($ytdl_options->addOptions(['f' => 'some_format'])).
$info_dict = $ytdl->download($webpage_url);
// with a download directory
// $info_dict = $ytdl->download($webpage_url, $directory_to_download);
// with an explicit $info_dict
// $new_info_dict = $ytdl->download($webpage_url, $directory_to_download, $info_dict);
$errors = $ytdl->getErrors();
If you want to set the ytdl executable path, you need to pass the value like below. This will skip the automatic scan of the executable. It is your responsibility to set this path correctly.
$ytdl = new Ytdl($ytdl_options, null, 'usr/share/local/yt-dlp');
- example-0 with just '--version' option
- example-1 extract example
- example-1-1 extract from a playlist
- example-2 download a video (cache and download folders)
- example-2-2 download with a change of options
- example-3 download a playlist (no cache, not optimal)
- example-4 extract all informations from a playlist and download some videos with cache system.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
ytdl is licensed under the MIT License (MIT). Please see the license file for more information.