Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Youtubedownloader Laravel Package

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+.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require masih/youtubedownloader
    

    Ensure php-youtube-dl is installed on your system (dependency for CLI functionality).

  2. First Use Case:

    • CLI: Run php artisan youtube:download --url="VIDEO_URL" --format="best" (if using Laravel Artisan).
    • Programmatic:
      use Masih\YoutubeDownloader\Facades\YoutubeDownloader;
      
      $url = "https://www.youtube.com/watch?v=EXAMPLE";
      $video = YoutubeDownloader::download($url, ['format' => 'best']);
      
  3. Where to Look First:

    • Facade API in the README.
    • config/youtubedownloader.php (if generated) for default settings.
    • app/Console/Commands/YoutubeDownloadCommand.php (if extending CLI).

Implementation Patterns

Usage Patterns

  1. Basic Download:

    $video = YoutubeDownloader::download($url, ['format' => 'mp4']);
    // Returns path to downloaded file.
    
  2. Batch Processing:

    $urls = ['url1', 'url2'];
    foreach ($urls as $url) {
        YoutubeDownloader::download($url, ['output' => 'storage/downloads']);
    }
    
  3. Integration with Laravel Storage:

    $path = YoutubeDownloader::download($url, ['output' => 'public/videos']);
    Storage::disk('public')->put('videos/'.basename($path), file_get_contents($path));
    
  4. Event Listeners:

    • Extend YoutubeDownloader events (e.g., Downloading, Downloaded) via Laravel’s event system.
    • Example listener:
      public function handle(Downloaded $event) {
          Log::info("Downloaded: " . $event->path);
      }
      
  5. Customizing Formats:

    $options = [
        'format' => 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best',
        'merge_output_format' => 'mp4'
    ];
    YoutubeDownloader::download($url, $options);
    

Workflows

  1. Admin Panel Integration:

    • Use the facade in a controller to handle video downloads triggered by user actions.
    • Example:
      public function download(Request $request) {
          $video = YoutubeDownloader::download($request->url, ['format' => 'best']);
          return response()->json(['path' => $video]);
      }
      
  2. Scheduled Downloads:

    • Use Laravel’s Schedule to download videos periodically:
      $schedule->command('youtube:download --url="URL" --format="best"')->daily();
      
  3. Queue Jobs:

    • Dispatch a job for async processing:
      DownloadYoutubeVideo::dispatch($url, ['format' => 'best']);
      
    • Job class:
      public function handle() {
          YoutubeDownloader::download($this->url, $this->options);
      }
      

Integration Tips

  • Laravel Artisan Commands: Extend YoutubeDownloadCommand for custom CLI logic (e.g., adding user authentication).
  • API Endpoints: Wrap the facade in a controller to expose download functionality via API.
  • Validation: Validate URLs and formats before passing to the facade:
    $validated = $request->validate([
        'url' => 'required|url',
        'format' => 'nullable|string'
    ]);
    

Gotchas and Tips

Pitfalls

  1. Dependency Conflicts:

    • Ensure php-youtube-dl is installed system-wide. The package relies on youtube-dl for CLI operations.
    • Fix: Run sudo apt-get install php-youtube-dl (Linux) or check youtube-dl docs.
  2. Rate Limiting:

    • YouTube may block requests if too many are made in a short time.
    • Fix: Implement delays between downloads or use proxies.
  3. File Overwrites:

    • Default behavior overwrites files with the same name.
    • Fix: Use output_template option to customize filenames:
      ['output' => 'storage/videos/%(title)s.%(ext)s']
      
  4. Missing Facade:

    • If using the facade without publishing config, defaults may not load.
    • Fix: Publish config first:
      php artisan vendor:publish --provider="Masih\YoutubeDownloader\YoutubeDownloaderServiceProvider"
      
  5. Encoding Issues:

    • Non-ASCII characters in filenames may cause issues.
    • Fix: Sanitize filenames or use output_template with sanitize_filename:
      ['output_template' => 'storage/videos/%%(sanitize_filename)s.%(ext)s']
      

Debugging

  1. Verbose Output: Enable debug mode via config:

    'debug' => env('YOUTUBE_DOWNLOADER_DEBUG', false),
    

    Or pass --verbose in CLI.

  2. Logging:

    • Wrap calls in try-catch to log errors:
      try {
          YoutubeDownloader::download($url);
      } catch (\Exception $e) {
          Log::error("Download failed: " . $e->getMessage());
      }
      
  3. Testing:

    • Use mock URLs (e.g., https://www.youtube.com/watch?v=dQw4w9WgXcQ) for testing.
    • Test edge cases: private videos, age-restricted content, live streams.

Config Quirks

  1. Default Options:

    • Override defaults in config/youtubedownloader.php:
      'default' => [
          'format' => 'best',
          'output' => storage_path('app/youtube'),
      ],
      
  2. Environment Variables:

    • Use .env to customize paths:
      YOUTUBE_DOWNLOADER_OUTPUT=storage_path('app/custom_videos')
      
  3. Service Provider:

    • Bind custom implementations if extending functionality:
      $this->app->bind('youtube-downloader', function () {
          return new CustomYoutubeDownloader();
      });
      

Extension Points

  1. Custom Downloader:

    • Extend Masih\YoutubeDownloader\YoutubeDownloader to add logic (e.g., metadata extraction):
      class CustomYoutubeDownloader extends YoutubeDownloader {
          public function getMetadata($url) {
              // Custom logic
          }
      }
      
  2. Hooks:

    • Use events to intercept downloads:
      event(new Downloading($url, $options));
      
  3. CLI Extensions:

    • Add subcommands to YoutubeDownloadCommand:
      protected $commands = [
          'youtube:download',
          'youtube:metadata', // Custom command
      ];
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
croct/coding-standard
croct/plug-php
nqxcode/phpmorphy
boundwize/pyrameter
testo/facade
headercat/phpstan-extension-ide-helper
yosymfony/parser-utils
innmind/black-box
babenkoivan/elastic-migrations
babenkoivan/elastic-adapter
develia/commons
dmstr/symfony-system-resources-bundle
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
renatomarinho/laravel-page-speed
develia/geo-bundle
austinheap/laravel-database-encryption
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle