請在指令行下使用(需要什麼功能自己改):
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,請配合我們調查。