norkunas / youtube-dl-php
youtube-dl / yt-dlp wrapper for php
Installs: 219 677
Dependents: 19
Suggesters: 0
Security: 0
Stars: 463
Watchers: 20
Forks: 159
Open Issues: 13
Requires
- php: >=7.4.0
- ext-json: *
- symfony/filesystem: ^5.1|^6.0|^7.0
- symfony/polyfill-php80: ^1.28
- symfony/process: ^5.1|^6.0|^7.0
Requires (Dev)
- mikey179/vfsstream: ^1.6.11
- php-cs-fixer/shim: ^3.60
- phpstan/phpstan: ^1.11.8
- phpstan/phpstan-phpunit: ^1.4.0
- phpstan/phpstan-strict-rules: ^1.6.0
- symfony/phpunit-bridge: ^6.4.10
- v2.9.0
- v2.8.1
- v2.8.0
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- dev-master / 2.0.x-dev
- v2.0.2
- v2.0.1
- v2.0.0
- 1.x-dev
- v1.6.1
- v1.6.0
- v1.5.1
- v1.5.0
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0
- 0.x-dev
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.3
- v0.1.2
- v0.1
- dev-trim-filenames
This package is auto-updated.
Last update: 2024-12-09 04:52:49 UTC
README
A PHP wrapper for youtube-dl or yt-dlp.
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.