- How do I install and configure `norkunas/youtube-dl-php` in a Laravel project?
- First, install the package via Composer: `composer require norkunas/youtube-dl-php`. Then, ensure `youtube-dl` or `yt-dlp` is installed on your server (preferably `yt-dlp` for active maintenance). For audio extraction, also install `ffmpeg` or `avconv`. No Laravel-specific configuration is needed beyond wrapping the package in a service class.
- Does this package support Laravel Queues for async video downloads?
- Yes, you can offload downloads to Laravel Queues by creating a job class (e.g., `HandleYoutubeDownloadJob`) that uses the package’s fluent API. Progress events can be emitted to update UI (e.g., Livewire/Tailwind) or trigger notifications. The package itself doesn’t enforce queue integration but works seamlessly with Laravel’s job system.
- What Laravel versions and PHP versions are officially supported?
- The package supports PHP 8.4+ and is compatible with Laravel 10+. It leverages Symfony Process, which aligns with Laravel’s dependency ecosystem. For older Laravel versions (e.g., 9.x), you may need to manually resolve Symfony Process dependencies or use a compatible fork.
- How do I handle errors when downloading videos in Laravel?
- Errors are returned via `$video->getError()`. Check this property after downloading and handle exceptions in your business logic. For example, log errors with Laravel’s `Log::error()` or display user-friendly messages. The package doesn’t auto-retry failed downloads, so implement retries in your job or service layer if needed.
- Can I use this package to download videos from platforms other than YouTube?
- Yes, the package wraps `youtube-dl`/`yt-dlp`, which supports hundreds of sites (e.g., Vimeo, Twitch, SoundCloud). Configure options like `--extract-audio` or `--format` to target specific platforms. Always verify the target site’s terms of service for automated downloads, as some may block or rate-limit scraping.
- How do I save downloaded files to Laravel Storage (e.g., S3, local disk)?
- The package returns `SplFileInfo` objects for downloaded files. Use Laravel’s `Storage` facade to move files to your desired disk. For example, after downloading, call `Storage::disk('s3')->put($video->getFile()->getFilename(), file_get_contents($video->getFile()->getPathname()))`. Note that the package doesn’t auto-bind to Laravel Storage, so handle this in your service layer.
- What are the risks of using external tools like `youtube-dl` in production?
- Risks include dependency instability (e.g., `youtube-dl` deprecations) and security concerns (CLI tool exposure). Mitigate by pinning versions in `composer.json`, using `yt-dlp` (more actively maintained), and running tools in a sandboxed environment (e.g., Docker). Always validate URLs and sanitize file paths to prevent command injection.
- How can I track download progress in real-time for a Laravel Livewire app?
- The package emits progress events via callbacks. Bind to these events in your `YoutubeDl` instance (e.g., `$yt->onProgress(function ($progress) { ... })`) and update Livewire properties or emit JavaScript events. For async downloads, store progress in a database or cache and poll it in your Livewire component.
- Is there a way to extract metadata (e.g., title, duration, thumbnail) without downloading the full video?
- Yes, use the `extractInfo` method instead of `download` to fetch metadata only. This returns a `Collection` of `Video` objects with properties like `getTitle()`, `getDuration()`, and `getThumbnail()`. This is useful for previews or search functionality without triggering downloads.
- What alternatives exist for downloading videos in Laravel if this package doesn’t fit my needs?
- Alternatives include direct CLI calls via `exec()` or `shell_exec()` (less safe), Laravel-specific packages like `spatie/laravel-youtube` (YouTube API only), or headless browsers like Puppeteer for JavaScript-heavy sites. For production, consider official APIs (e.g., YouTube Data API) if rate limits or terms of service allow.