commandstring / yt-dlp
yt-dlp 的 PHP 封装
v3.0.0
2023-05-10 22:11 UTC
Requires
- php: ^8.1
- commandstring/utils: ^1.3
- react/async: ^4.0
- react/promise: ^2.9
Requires (Dev)
README
基于 Promise 的 PHP 封装 YtDlp,基于 ReactPHP 构建 YtDlp
注意:在开始使用前,您需要启动一个 React/Promise 循环,否则脚本将在命令执行完成之前崩溃!!!
创建 YtDlp
$yt = new YtDlp();
如果您不在系统路径中,也可以提供 Yt-Dlp 的路径。
CommandBuilder
您可以使用命令构建器来编程创建命令,YtDlp 的所有选项都存在于 \Yt\Dlp\Options
中(如果遗漏了任何内容,请提交问题)
您可以使用 YtDlp 实例创建一个新的 CommandBuilder
$command = $yt->newCommand($url)->addOption(Options::SKIP_DOWNLOAD)->addOption(Options::DUMP_JSON);
然后您可以使用 execute 方法执行命令。通过 then 方法,您可以检查处理成功/失败的命令执行
$command->execute()->then(function (string $result) { echo $result; }, function (CommandExecutionFailed $e) { echo (string)$e; });
已实现的命令
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;
返回音频/视频下载的完整路径
public function getInfo(string $url): PromiseInterface
返回包含视频信息的 stdClass 对象 可用字段
public function search(string $query, int $results = 1): PromiseInterface
返回所有结果数组,每个项目结构与 getInfo 方法类似
基本用法
此代码将下载查询中的第一个视频
<?php use React\EventLoop\Loop; use Yt\Dlp\Exceptions\CommandExecutionFailed; use Yt\Dlp\YtDlp; require_once "vendor/autoload.php"; $yt = new YtDlp(); $errHandler = function (CommandExecutionFailed $e) { echo (string)$e; }; $query = "amalee the worlds continuation"; $yt->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();