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

Youtube Dl Php Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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).

  2. First Use Case: Download a video with default settings:

    use YoutubeDl\YoutubeDl;
    
    $yt = new YoutubeDl();
    $collection = $yt->download(['url' => 'https://youtu.be/EXAMPLE']);
    
  3. 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).

Implementation Patterns

Core Workflows

  1. 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
    }
    
  2. Audio Extraction:

    $yt->download(
        Options::create()
            ->url('https://youtu.be/EXAMPLE')
            ->extractAudio(true)
            ->audioFormat('mp3')
            ->audioQuality('0') // Best quality
    );
    
  3. Playlist Handling:

    $playlist = $yt->download(['url' => 'https://youtu.be/playlist?list=EXAMPLE']);
    $playlist->getVideos(); // Array of Video objects
    
  4. Progress Tracking:

    $yt->onProgress(function ($target, $percentage, $speed) {
        Log::info("Downloading $target: $percentage% at $speed");
    });
    

Integration Tips

  • Queue Jobs: Use Laravel Queues to offload downloads:
    DownloadVideo::dispatch('https://youtu.be/EXAMPLE')->onQueue('downloads');
    
  • Storage: Pair with spatie/laravel-medialibrary for metadata management.
  • Validation: Sanitize URLs with Illuminate\Support\Str::of($url)->startsWith('https://').

Gotchas and Tips

Pitfalls

  1. Binary Paths:

    • Defaults to youtube-dl/yt-dlp in PATH. Override via:
      $yt = new YoutubeDl(null, '/custom/path/to/yt-dlp');
      
    • Debug: Check $yt->getBinaryPath() to confirm.
  2. FFmpeg Dependencies:

    • Audio extraction (extractAudio) requires ffmpeg/avconv and ffprobe/avprobe.
    • Fix: Install via sudo apt install ffmpeg (Linux) or official guides.
  3. Windows Compatibility:

    • Filenames with :, /, etc., may fail. Use:
      ->forceWindowsCompatible(true)
      
  4. Rate Limiting:

    • YouTube may block rapid requests. Add delays:
      sleep(2); // Between downloads
      

Debugging

  • Logs: Enable verbose output:
    $yt->download(['url' => 'EXAMPLE', '--verbose' => true]);
    
  • Errors: Check $video->getError() for failures (e.g., age-restricted content).

Extension Points

  1. 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());
    
  2. Post-Processing:

    $yt->download([
        'url' => 'EXAMPLE',
        '--postprocessor-args' => 'ffmpeg:-crf 23' // Custom FFmpeg args
    ]);
    
  3. Metadata Extraction:

    $video = $collection->getVideos()[0];
    $video->getTitle();
    $video->getDuration(); // in seconds
    $video->getThumbnail(); // SplFileInfo
    

Pro Tips

  • Batch Processing: Loop through URLs:
    foreach ($urls as $url) {
        $yt->download(['url' => $url, '--no-warnings' => true]);
    }
    
  • Thumbnails: Extract with:
    $yt->download(['url' => 'EXAMPLE', '--write-thumbnail' => true]);
    
  • Playlists: Iterate with:
    $playlist = $yt->download(['url' => 'PLAYLIST_URL']);
    foreach ($playlist->getVideos() as $video) {
        // Process each video
    }
    
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor