masih/youtubedownloader
PHP/Composer YouTube video downloader with CLI tool. Download videos (and playlists) and fetch info by URL or ID; supports setting download path and uses a writable cache directory. Requires PHP 5.5+.
Installation:
composer require masih/youtubedownloader
Ensure php-youtube-dl is installed on your system (dependency for CLI functionality).
First Use Case:
php artisan youtube:download --url="VIDEO_URL" --format="best" (if using Laravel Artisan).use Masih\YoutubeDownloader\Facades\YoutubeDownloader;
$url = "https://www.youtube.com/watch?v=EXAMPLE";
$video = YoutubeDownloader::download($url, ['format' => 'best']);
Where to Look First:
config/youtubedownloader.php (if generated) for default settings.app/Console/Commands/YoutubeDownloadCommand.php (if extending CLI).Basic Download:
$video = YoutubeDownloader::download($url, ['format' => 'mp4']);
// Returns path to downloaded file.
Batch Processing:
$urls = ['url1', 'url2'];
foreach ($urls as $url) {
YoutubeDownloader::download($url, ['output' => 'storage/downloads']);
}
Integration with Laravel Storage:
$path = YoutubeDownloader::download($url, ['output' => 'public/videos']);
Storage::disk('public')->put('videos/'.basename($path), file_get_contents($path));
Event Listeners:
YoutubeDownloader events (e.g., Downloading, Downloaded) via Laravel’s event system.public function handle(Downloaded $event) {
Log::info("Downloaded: " . $event->path);
}
Customizing Formats:
$options = [
'format' => 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best',
'merge_output_format' => 'mp4'
];
YoutubeDownloader::download($url, $options);
Admin Panel Integration:
public function download(Request $request) {
$video = YoutubeDownloader::download($request->url, ['format' => 'best']);
return response()->json(['path' => $video]);
}
Scheduled Downloads:
Schedule to download videos periodically:
$schedule->command('youtube:download --url="URL" --format="best"')->daily();
Queue Jobs:
DownloadYoutubeVideo::dispatch($url, ['format' => 'best']);
public function handle() {
YoutubeDownloader::download($this->url, $this->options);
}
YoutubeDownloadCommand for custom CLI logic (e.g., adding user authentication).$validated = $request->validate([
'url' => 'required|url',
'format' => 'nullable|string'
]);
Dependency Conflicts:
php-youtube-dl is installed system-wide. The package relies on youtube-dl for CLI operations.sudo apt-get install php-youtube-dl (Linux) or check youtube-dl docs.Rate Limiting:
File Overwrites:
output_template option to customize filenames:
['output' => 'storage/videos/%(title)s.%(ext)s']
Missing Facade:
php artisan vendor:publish --provider="Masih\YoutubeDownloader\YoutubeDownloaderServiceProvider"
Encoding Issues:
output_template with sanitize_filename:
['output_template' => 'storage/videos/%%(sanitize_filename)s.%(ext)s']
Verbose Output: Enable debug mode via config:
'debug' => env('YOUTUBE_DOWNLOADER_DEBUG', false),
Or pass --verbose in CLI.
Logging:
try-catch to log errors:
try {
YoutubeDownloader::download($url);
} catch (\Exception $e) {
Log::error("Download failed: " . $e->getMessage());
}
Testing:
https://www.youtube.com/watch?v=dQw4w9WgXcQ) for testing.Default Options:
config/youtubedownloader.php:
'default' => [
'format' => 'best',
'output' => storage_path('app/youtube'),
],
Environment Variables:
.env to customize paths:
YOUTUBE_DOWNLOADER_OUTPUT=storage_path('app/custom_videos')
Service Provider:
$this->app->bind('youtube-downloader', function () {
return new CustomYoutubeDownloader();
});
Custom Downloader:
Masih\YoutubeDownloader\YoutubeDownloader to add logic (e.g., metadata extraction):
class CustomYoutubeDownloader extends YoutubeDownloader {
public function getMetadata($url) {
// Custom logic
}
}
Hooks:
event(new Downloading($url, $options));
CLI Extensions:
YoutubeDownloadCommand:
protected $commands = [
'youtube:download',
'youtube:metadata', // Custom command
];
How can I help you explore Laravel packages today?