b站有三种视频格式,且现在下载的视频都是影视频分离的形式,且都是m4s格式,需要同时把video和audio下载下来,用ffmpeg合成一下即可,所有的请求头都需要添加User-Agent

  1. https://www.bilibili.com/video/BV1QT4y1w7HS
  2. https://www.bilibili.com/bangumi/play/ep326599
  3. https://www.bilibili.com/bangumi/play/ss34319
# 音视频合成
ffmpeg -i ./video.m4s -i ./audio.m4s -codec copy ./video.mp4

第一种BV Link to heading

对于BV格式视频,视频的下载地址就在网页源代码中,直接请求url地址即可,请求头中带上已登录账号的cookie发送请求就可以拿到视频下载地址,如果你的账号是大会员,拿到的视频下载地址中就会有大会员专享视频。拿到下载地址后,发送一个请求头中带referer的请求(值为url)就可以下载了。下面我以node.js为例子

1

const got = require('got');
const stream = require('stream');
const {promisify} = require('util');
const pipeline = promisify(stream.pipeline);
const fs = require('fs');

// 获取下载地址
(async () => {
	try {
		const response = await got('https://www.bilibili.com/video/BV1k54y1C7R6',{
      headers: {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
        'cookie': '_uuid=D87F5597-9F58-9410-7C9A-EC6EE452205A44601infoc; buvid3=69498676-73DD-451C-A606-02CA33B5AEE553917infoc; CURRENT_FNVAL=16; LIVE_BUVID=AUTO2115924816109117; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1596722341; CURRENT_QUALITY=116; blackside_state=1; finger=481832271; sid=ax362991; DedeUserID=14899614; DedeUserID__ckMd5=6fb380360cc351f0; SESSDATA=122d1191%2C1614940578%2C4f586*91'
      }
    });
    console.log('success')
    // bv系列
    let str = response.body.match(/\<script\>window\.\_\_playinfo\_\_\=([\s\S]*?)\<\/script\>\<script\>window\.\_\_INITIAL\_STATE\_\_\=/)[1];
    console.log(str)
	} catch (error) {
    console.log('error')
		console.log(error);
	}
})()

// 下载视频
const options = {
  headers: {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0',
    'referer': 'https://www.bilibili.com/video/BV1k54y1C7R6'
  }
};

const url = 'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/43/27/229752743/229752743-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1600025170&gen=playurl&os=kodobv&oi=975672835&trid=a9b798c7f52640c68c3850b379ab524bp&platform=pc&upsig=65c15861b8e6dc6f46012dc21bf034c5&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=14899614&orderid=0,3&agrr=0&logo=80000000';

(async () => {
  await pipeline(
    got.stream(url, options)
      .on('downloadProgress', progress => {
        console.log(progress)
      })
      .on('error', error => {
        console.log(error)
      }),
    fs.createWriteStream('./video/audio2.m4s')
  );
})();

第二种EP Link to heading

ep系列视频,下载视频和bv类似,都是在网页源代码中找到视频下载地址,不同到是,ep视频下载地址获取规则和bv有一点区别而且ep系列还需要在源代码中找到当前视频到BV号,BV号在下载视频时需要作为请求头到referer

const got = require('got');
const stream = require('stream');
const {promisify} = require('util');
const pipeline = promisify(stream.pipeline);
const fs = require('fs');

(async () => {
	try {
		const response = await got('https://www.bilibili.com/bangumi/play/ep337019',{
      headers: {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',
        'cookie': '_uuid=D87F5597-9F58-9410-7C9A-EC6EE452205A44601infoc; buvid3=69498676-73DD-451C-A606-02CA33B5AEE553917infoc; CURRENT_FNVAL=16; LIVE_BUVID=AUTO2115924816109117; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1596722341; CURRENT_QUALITY=116; blackside_state=1; finger=481832271; sid=ax362991; DedeUserID=14899614; DedeUserID__ckMd5=6fb380360cc351f0; SESSDATA=122d1191%2C1614940578%2C4f586*91'
      }
    });
    console.log('success')
    // ep系列
    let str = response.body.match(/\<script\>window\.\_\_playinfo\_\_\=([\s\S]*?)\<\/script\>\<script\>window\.\_\_BILI\_CONFIG/)[1];
    console.log(str)
	} catch (error) {
    console.log('error')
		console.log(error);
	}
})()


const options = {
  headers: {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:56.0) Gecko/20100101 Firefox/56.0',
    'referer': 'https://www.bilibili.com/video/BV1wh411R7M5'
  }
};

const url = 'https://upos-sz-mirrorkodo.bilivideo.com/upgcxcode/43/27/229752743/229752743-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1600025170&gen=playurl&os=kodobv&oi=975672835&trid=a9b798c7f52640c68c3850b379ab524bp&platform=pc&upsig=65c15861b8e6dc6f46012dc21bf034c5&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=14899614&orderid=0,3&agrr=0&logo=80000000';

(async () => {
  await pipeline(
    got.stream(url, options)
      .on('downloadProgress', progress => {
        console.log(progress)
      })
      .on('error', error => {
        console.log(error)
      }),
    fs.createWriteStream('./video/audio2.m4s')
  );
})();

第三种SS Link to heading

ss系列视频一般是番剧电影居多,下载方法也略有不同

  1. 通过网页源代码获取到视频bvid cid epid这几个值

2

  1. 请求接口,拿到下载地址

2

2

  1. 发送请求下载即可

本文所有实例完整代码查看