请在命令行下使用(需要什么功能自己改):
echo "\e[38;5;83m请输入视频番号(12位BVID):\e[0m" . PHP_EOL . "BVID:";
$bvid = fread(STDIN, 12);
$base_info_url = 'https://api.bilibili.com/x/web-interface/view?bvid=' . $bvid;
$base_info_json = curl($base_info_url);
if (empty($base_info_json)) {
exit("\e[38;5;1m连接建立失败!\e[0m");
}
echo "请求API地址" . "成功!" . PHP_EOL;
$base_info_array = json_decode($base_info_json, true);
echo "提取AID为:" . "\e[38;5;5m" . $base_info_array['data']['aid'] . "\e[0m" . PHP_EOL;
echo "一共 " . "\e[38;5;9m" . count($base_info_array['data']['pages']) . "\e[0m 个视频。" . PHP_EOL;
echo '------------------' . PHP_EOL;
echo "提取下载内容" . PHP_EOL;
echo "使用Cookie,不使用直接回车(下载720P需要登录,下载1080P60+需要大会员):" . PHP_EOL;
echo "Cookie:";
$cookie = fread(STDIN, 2048);
if (empty($cookie)) {
echo "没有使用Cookie!" . PHP_EOL;
}
echo "选择画质(默认下载最清晰画质,暂不支持自定义):" . PHP_EOL;
echo '------------------' . PHP_EOL;
foreach ($base_info_array['data']['pages'] as $download) {
$download_link_url = 'https://api.bilibili.com/x/player/playurl?bvid=' . $bvid . '&cid=' . $download['cid'] . '&qn=80&otype=json';
$download_info_json = curl($download_link_url);
$download_info_array = json_decode($download_info_json, true);
echo "正在为你下载第 \e[38;5;87m" . $download['page'] . "\e[0m 节:\e[38;5;187m" . $download['part'] . "\e[0m,文件大小:\e[38;5;137m" . sprintf("%0.2f MiB", $download_info_array['data']['durl'][0]['size'] / 1024 / 1024) . "\e[0m" . PHP_EOL;
echo "该视频支持 " . array_to_words($download_info_array['data']['accept_description'], '、') . " " . count($download_info_array['data']['accept_description']) . " 种清晰度,已自动为你选择最清晰的画质。" . PHP_EOL;
$header = [
'Cookie: ' . $cookie,
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36',
];
$referer = 'https://www.bilibili.com/video/' . $download['cid'];
$video = curl($download_info_array['data']['durl'][0]['url'], $header, $referer);
$file = __DIR__ . '/' . $base_info_array['data']['title'] . '/' . $download['part'] . '.flv';
if (!is_dir(dirname($file))) {
mkdir(dirname($file));
}
file_put_contents($file, $video);
echo "第 " . $download['page'] . " 节下载成功,文件已经存入:\e[38;5;37m" . $file . "\e[0m" . PHP_EOL;
}
function curl(string $url, array $header = [], string $referer = null): string
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_HEADER => false,
CURLOPT_HTTPHEADER => $header,
CURLOPT_REFERER => $referer,
//
]);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
function array_to_words(array $array, $delimit = ' ', $start = '', $end = ''): string
{
$string = '';
foreach ($array as $value) {
$string .= $value . $delimit;
}
$string = rtrim($string, $delimit);
return $string;
}
举例:
【凤凰传奇】歌曲MV合集(收藏)
你好!ICAC,请配合我们调查。