norkunas/youtube-dl-php
PHP wrapper for youtube-dl/yt-dlp to download videos or extract audio from YouTube and other sites. Configure options, set output paths, run downloads, and parse results/errors from a simple, fluent API for CLI-driven media fetching.
Install Dependencies:
composer require norkunas/youtube-dl-php:^2.14
Ensure youtube-dl or yt-dlp is installed system-wide (preferred: yt-dlp for active development).
First Use Case: Download a video with default settings:
use YoutubeDl\YoutubeDl;
$yt = new YoutubeDl();
$collection = $yt->download(['url' => 'https://youtu.be/EXAMPLE']);
Key Files:
vendor/norkunas/youtube-dl-php/src/ for core classes.README.md for quickstart examples.tests/ for edge-case patterns (e.g., playlists, error handling).Basic Downloads:
$collection = $yt->download(
Options::create()
->url('https://youtu.be/EXAMPLE')
->downloadPath(storage_path('videos'))
);
foreach ($collection->getVideos() as $video) {
$video->getFile()->getPathname(); // Local file path
}
Audio Extraction:
$yt->download(
Options::create()
->url('https://youtu.be/EXAMPLE')
->extractAudio(true)
->audioFormat('mp3')
->audioQuality('0') // Best quality
);
Playlist Handling:
$playlist = $yt->download(['url' => 'https://youtu.be/playlist?list=EXAMPLE']);
$playlist->getVideos(); // Array of Video objects
Progress Tracking:
$yt->onProgress(function ($target, $percentage, $speed) {
Log::info("Downloading $target: $percentage% at $speed");
});
DownloadVideo::dispatch('https://youtu.be/EXAMPLE')->onQueue('downloads');
spatie/laravel-medialibrary for metadata management.Illuminate\Support\Str::of($url)->startsWith('https://').Binary Paths:
youtube-dl/yt-dlp in PATH. Override via:
$yt = new YoutubeDl(null, '/custom/path/to/yt-dlp');
$yt->getBinaryPath() to confirm.FFmpeg Dependencies:
extractAudio) requires ffmpeg/avconv and ffprobe/avprobe.sudo apt install ffmpeg (Linux) or official guides.Windows Compatibility:
:, /, etc., may fail. Use:
->forceWindowsCompatible(true)
Rate Limiting:
sleep(2); // Between downloads
$yt->download(['url' => 'EXAMPLE', '--verbose' => true]);
$video->getError() for failures (e.g., age-restricted content).Custom Process Builder:
class CustomProcessBuilder implements ProcessBuilderInterface {
public function build(?string $binPath, ?string $pythonPath, array $args): Process {
$process = new Process([$binPath, ...$args]);
$process->setTimeout(300); // 5-minute timeout
return $process;
}
}
$yt = new YoutubeDl(new CustomProcessBuilder());
Post-Processing:
$yt->download([
'url' => 'EXAMPLE',
'--postprocessor-args' => 'ffmpeg:-crf 23' // Custom FFmpeg args
]);
Metadata Extraction:
$video = $collection->getVideos()[0];
$video->getTitle();
$video->getDuration(); // in seconds
$video->getThumbnail(); // SplFileInfo
foreach ($urls as $url) {
$yt->download(['url' => $url, '--no-warnings' => true]);
}
$yt->download(['url' => 'EXAMPLE', '--write-thumbnail' => true]);
$playlist = $yt->download(['url' => 'PLAYLIST_URL']);
foreach ($playlist->getVideos() as $video) {
// Process each video
}
How can I help you explore Laravel packages today?