// ==UserScript== // @name bilibili 视频下载 // @version 0.01.21 // @license MIT // @description bilibili mp4 格式视频下载脚本,支持多种格式 // @icon https://static.hdslb.com/mobile/img/512.png // @author fwqaaq // @namespace https://github.com/fwqaaq/scripts // @match *://www.bilibili.com/video/BV* // @match *://m.bilibili.com/video/BV* // @run-at document-body // @grant GM.addStyle // @grant GM_addStyle // @require https://cdn.jsdelivr.net/npm/js-md5@0.8.3/src/md5.min.js // @downloadURL none // ==/UserScript== const downloadIcon = '' const mixinKeyEncTab = [ 46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42, 19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60, 51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, 36, 20, 34, 44, 52 ] const getMixinKey = (orig) => mixinKeyEncTab.map(n => orig[n]).join('').slice(0, 32) // 为请求参数进行 wbi 签名 function encWbi(params, img_key, sub_key) { const mixin_key = getMixinKey(img_key + sub_key), curr_time = Math.round(Date.now() / 1000), chr_filter = /[!'()*]/g Object.assign(params, { wts: curr_time }) // 添加 wts 字段 // 按照 key 重排参数 const query = Object .keys(params) .sort() .map(key => { // 过滤 value 中的 "!'()*" 字符 const value = params[key].toString().replace(chr_filter, '') return `${encodeURIComponent(key)}=${encodeURIComponent(value)}` }) .join('&') const wbi_sign = md5(query + mixin_key) // 计算 w_rid return query + '&w_rid=' + wbi_sign } // 获取最新的 img_key 和 sub_key async function getWbiKeys() { const response = await fetch('https://api.bilibili.com/x/web-interface/nav') if (!response.ok) throw new Error("获取 wbi 失败,你的 HTTP 响应状态码是 ${response.status}") const { code, data: { wbi_img: { img_url, sub_url } } } = await response.json() if (code === "-101") console.log("仅能下载 720p 以下的视频") return { img_key: img_url.slice( img_url.lastIndexOf('/') + 1, img_url.lastIndexOf('.') ), sub_key: sub_url.slice( sub_url.lastIndexOf('/') + 1, sub_url.lastIndexOf('.') ) } } // 获取 bvid 和 cid async function getBvidAndCid() { const bvid = window.location.pathname.split('/').find(item => item.startsWith("BV")) const res = await fetch(`https://api.bilibili.com/x/player/pagelist?bvid=${bvid}&jsonp=jsonp`) const { data: [{ cid }], code } = await res.json() if (!res.ok) throw new Error(`获取 cid 失败,HTTP 响应状态码是 ${res.status}. code 码是 ${code}`) return [bvid, cid] } // 通过 dom 节点设置进度 async function getVideo(dom, qn = 112, fnval = 1) { let res, name // 处理问题 try { const [bvid, cid] = await getBvidAndCid() const { img_key, sub_key } = await getWbiKeys() const params = { bvid, cid, qn, fnval, fnver: 0, fourk: 1 } const query = encWbi(params, img_key, sub_key) const url = await getVideoUrl(query) res = await fetch(url) name = bvid if (!res.ok) throw new Error(`视频下载失败,响应状态码为 ${res.status}`) } catch (e) { alert(`${e},请到 https://github.com/fwqaaq/scripts 提交报告`) throw new Error(e) } // 进度 const progress = (response) => { const total = response.headers.get("Content-Length") let loaded = 0 const [dataStream, progressStream] = response.body.tee() const reader = progressStream.getReader() const logger = (reader) => { reader.read().then(({ done, value }) => { if (done) { return } loaded += value.length dom.innerText = `${(loaded / total * 100).toFixed(0)}%` logger(reader) }) } logger(reader) return new Response(dataStream) } const blob = await progress(res).blob() const video = URL.createObjectURL(blob) dom.href = video dom.download = `${name}.mp4` dom.click() URL.revokeObjectURL(video) dom.remove() return true } async function getVideoUrl(query) { const res = await fetch(`https://api.bilibili.com/x/player/wbi/playurl?${query}`) if (!res.ok) throw new Error(`你的视频下载链接获取失败,你的响应状态码是 ${res.status}`) const { data: { durl: [{ url }] } } = await res.json() return url } function createDownloaderElement() { const downloader = document.createElement("div") downloader.classList.add("downloader-icon") const nav = document.createElement("nav") nav.classList.add("downloader-nav") nav.innerHTML = `