https://github.com/commandstring/ytdlp-php
A PHP wrapper for YtDlp
https://github.com/commandstring/ytdlp-php
Last synced: about 2 months ago
JSON representation
A PHP wrapper for YtDlp
- Host: GitHub
- URL: https://github.com/commandstring/ytdlp-php
- Owner: CommandString
- License: mit
- Created: 2023-01-04T14:52:26.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-04T23:27:24.000Z (over 2 years ago)
- Last Synced: 2025-01-25T10:33:00.556Z (11 months ago)
- Language: PHP
- Homepage:
- Size: 51.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# commandstring/yt-dlp
A promised-based PHP wrapper for [YtDlp](https://github.com/yt-dlp/yt-dlp) built off ReactPHP
**NOTE BEFORE USING YOU WILL NEED TO START A REACT/PROMISE LOOP OR THE SCRIPT WILL DIE BEFORE THE COMMAND FINISHES EXECUTING!!!**
# Creating YtDlp
```php
$yt = new YtDlp();
```
You can supply a path to your Yt-Dlp as well if it's not in your path.
# CommandBuilder
You can use the command builder to create commands programmatically, all options for YtDlp exist inside `\Yt\Dlp\Options` (if I'm missing anything please open an issue)
You can create a new CommandBuilder with your YtDlp instance
```php
$command = $yt->newCommand($url)->addOption(Options::SKIP_DOWNLOAD)->addOption(Options::DUMP_JSON);
```
You can then use the execute method to execute. With the then method following that you can check the handle a success/failed command execution
```php
$command->execute()->then(function (string $result) {
echo $result;
}, function (CommandExecutionFailed $e) {
echo (string)$e;
});
```
# Already implemented commands
```php
public function downloadVideo(string $url, string $outputPath, string $customName, string $format = "mp4"): PromiseInterface;
public function downloadAudio(string $url, string $outputPath, string $customName, string $format = "mp3"): PromiseInterface;
```
returns the full path of where the audio/video was downloaded
```php
public function getInfo(string $url): PromiseInterface
```
returns an stdClass with the video info [Available Fields](https://github.com/yt-dlp/yt-dlp#output-template)
```php
public function search(string $query, int $results = 1): PromiseInterface
```
Returns an array of all the results, each item has a structure similar to the getInfo method
# Basic Usage
This code will download the first videos from the query
```php
search($query, 5)->then(function ($results) use ($yt, $errHandler) {
foreach ($results as $video) {
$id = $video->id;
$yt->downloadVideo($id, __DIR__, $id)->then(null, $errHandler);
}
});
Loop::get()->run();
```