norkunas/youtube-dl-php

youtube-dl / yt-dlp wrapper for php

Maintainers

πŸ‘ norkunas

Package info

github.com/norkunas/youtube-dl-php

pkg:composer/norkunas/youtube-dl-php

Statistics

Installs: 352 542

Dependents: 19

Suggesters: 0

Stars: 505

Open Issues: 14

v2.14.1 2026-05-24 17:00 UTC

Requires

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 55583e54987de14a5a8d1fdf1ff3271640ecbded

  • Tomas NorkΕ«nas <norkunas.tom.woop@gmail.com>

youtubeyoutube-dlyt-dlp


README

A PHP wrapper for youtube-dl or yt-dlp.

πŸ‘ Latest Stable Version
πŸ‘ Latest Unstable Version
πŸ‘ Total Downloads
πŸ‘ CI Status
πŸ‘ License

Install

First step is to download the youtube-dl or yt-dlp.

Second step is to install the wrapper using Composer:

composer require norkunas/youtube-dl-php:dev-master

Download video

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use YoutubeDl\Options;
use YoutubeDl\YoutubeDl;

$yt = new YoutubeDl();

$collection = $yt->download(
 Options::create()
 ->downloadPath('/path/to/downloads')
 ->url('https://www.youtube.com/watch?v=oDAw7vW7H0c')
);

foreach ($collection->getVideos() as $video) {
 if ($video->getError() !== null) {
 echo "Error downloading video: {$video->getError()}.";
 } else {
 echo $video->getTitle(); // Will return Phonebloks
 // $video->getFile(); // \SplFileInfo instance of downloaded file
 }
}

Download only audio (requires ffmpeg or avconv and ffprobe or avprobe)

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use YoutubeDl\Options;
use YoutubeDl\YoutubeDl;

$yt = new YoutubeDl();
$collection = $yt->download(
 Options::create()
 ->downloadPath('/path/to/downloads')
 ->extractAudio(true)
 ->audioFormat('mp3')
 ->audioQuality('0') // best
 ->output('%(title)s.%(ext)s')
 ->url('https://www.youtube.com/watch?v=oDAw7vW7H0c')
);

foreach ($collection->getVideos() as $video) {
 if ($video->getError() !== null) {
 echo "Error downloading video: {$video->getError()}.";
 } else {
 $video->getFile(); // audio file
 }
}

Download progress

<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use YoutubeDl\YoutubeDl;

$yt = new YoutubeDl();
$yt->onProgress(static function (?string $progressTarget, string $percentage, string $size, string $speed, string $eta, ?string $totalTime): void {
 echo "Download file: $progressTarget; Percentage: $percentage; Size: $size";
 if ($speed) {
 echo "; Speed: $speed";
 }
 if ($eta) {
 echo "; ETA: $eta";
 }
 if ($totalTime !== null) {
 echo "; Downloaded in: $totalTime";
 }
});

Custom Process Instantiation

<?php

declare(strict_types=1);

namespace App\YoutubeDl;

use Symfony\Component\Process\Process;
use YoutubeDl\Process\ProcessBuilderInterface;

class ProcessBuilder implements ProcessBuilderInterface
{
 public function build(?string $binPath, ?string $pythonPath, array $arguments = []): Process
 {
 $process = new Process([$binPath, $pythonPath, ...$arguments]);
 // Set custom timeout or customize other things..
 $process->setTimeout(60);

 return $process;
 }
}
<?php

declare(strict_types=1);

use App\YoutubeDl\ProcessBuilder;
use YoutubeDl\YoutubeDl;

$processBuilder = new ProcessBuilder();

// Provide your custom process builder as the first argument.
$yt = new YoutubeDl($processBuilder);

Questions?

If you have any questions please open a discussion.

License

This library is released under the MIT License. See the bundled LICENSE file for details.