欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

(新)B站视频播放源地址获取及B站视频下载

程序员文章站 2022-05-11 09:05:51
...

突然之间,我们的B站视频播放失效了。之前直接使用的ibilibili,发现它的低清版本视频无法正常加载了。后来加上它的公众号,才发现,都不容易,十分感谢ibilibili站站长多年的坚持,尤其是听到他说这个站点每天受到几千人的攻击时。ibilibili站提供的高清视频下载器,下载m4s文件,最后再用ffmpeg转成mp4。
对于我们而言,视频的清晰度并不重要,我们需要播放直链,仅仅是为了播放和预览视频。下载B站视频本身并不是一个难事,所以下文我将首先介绍如何获取低清版直链,然后谈谈高清版下载如何是实现。

之前,通过下面的文章,实现了B站视频在其他站点的快速预览和播放。大大地方便了对素材的快速选择。之前的源地址获取接口失效后,急需一种新的获取方式。

1.播放源获取

通过伪装移动UA请求网页,如下

curl 'https://m.bilibili.com/video/av70663655.html?from=search' 
-H 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'  
--compressed

得到的请求结果中,找到下面这段关键信息

window.__INITIAL_STATE__ = {
	"playingMedia": "",
	"reduxAsyncConnect": {
		"loaded": true,
		"loadState": {
			"videoTag": {
				"loading": false,
				"loaded": true,
				"error": null
			},
			"videoInfo": {
				"loading": false,
				"loaded": true,
				"error": null
			}
		},
		"videoTag": [{
			"tag_id": 389808,
			"tag_name": "音乐现场",
			"cover": "http://i0.hdslb.com/bfs/archive/e86c8d10377e534b8a1617280c8cef62ae62c30c.jpg",
			"head_cover": "http://i0.hdslb.com/bfs/archive/3a8bc72ebc670579a0b97b68b973d8e5ed495b24.jpg",
			"content": "收录音乐实况表演视频,和音乐来一次最本质的亲密接触",
			"short_content": "和音乐来一次最本质的亲密接触",
			"type": 3,
			"state": 0,
			"ctime": 1436866637,
			"count": {
				"view": 0,
				"use": 136254,
				"atten": 62981
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}, {
			"tag_id": 11687,
			"tag_name": "综艺",
			"cover": "http://i0.hdslb.com/bfs/archive/e3c3689f4246c3a7c70f63ed79a786faf28a8ffe.png",
			"head_cover": "",
			"content": "",
			"short_content": "",
			"type": 3,
			"state": 0,
			"ctime": 1436866637,
			"count": {
				"view": 0,
				"use": 976243,
				"atten": 181572
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}, {
			"tag_id": 58183,
			"tag_name": "真人秀",
			"cover": "",
			"head_cover": "",
			"content": "",
			"short_content": "",
			"type": 3,
			"state": 0,
			"ctime": 1436866637,
			"count": {
				"view": 0,
				"use": 144418,
				"atten": 44876
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}, {
			"tag_id": 9374,
			"tag_name": "HIPHOP",
			"cover": "",
			"head_cover": "",
			"content": "",
			"short_content": "",
			"type": 3,
			"state": 0,
			"ctime": 1436866637,
			"count": {
				"view": 0,
				"use": 98376,
				"atten": 67805
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}, {
			"tag_id": 11905832,
			"tag_name": "Rhythm + Flow:Rapper",
			"cover": "",
			"head_cover": "",
			"content": "",
			"short_content": "",
			"type": 1,
			"state": 0,
			"ctime": 1569454310,
			"count": {
				"view": 0,
				"use": 1,
				"atten": 1
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}, {
			"tag_id": 21297,
			"tag_name": "嘻哈",
			"cover": "",
			"head_cover": "",
			"content": "",
			"short_content": "",
			"type": 0,
			"state": 0,
			"ctime": 1436866637,
			"count": {
				"view": 0,
				"use": 48813,
				"atten": 2951
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}, {
			"tag_id": 82755,
			"tag_name": "饶舌",
			"cover": "",
			"head_cover": "",
			"content": "",
			"short_content": "",
			"type": 0,
			"state": 0,
			"ctime": 1436866637,
			"count": {
				"view": 0,
				"use": 5844,
				"atten": 406
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}, {
			"tag_id": 401568,
			"tag_name": "NETFLIX",
			"cover": "",
			"head_cover": "",
			"content": "",
			"short_content": "",
			"type": 0,
			"state": 0,
			"ctime": 1436866637,
			"count": {
				"view": 0,
				"use": 2656,
				"atten": 591
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}, {
			"tag_id": 6782056,
			"tag_name": "嘻哈王",
			"cover": "",
			"head_cover": "",
			"content": "",
			"short_content": "",
			"type": 0,
			"state": 0,
			"ctime": 1522744265,
			"count": {
				"view": 0,
				"use": 625,
				"atten": 1
			},
			"is_atten": 0,
			"likes": 0,
			"hates": 0,
			"attribute": 0,
			"liked": 0,
			"hated": 0
		}],
		"videoInfo": {
			"bvid": "BV1bE411f72E",
			"aid": 70663655,
			"videos": 4,
			"tid": 71,
			"tname": "综艺",
			"copyright": 2,
			"pic": "http://i0.hdslb.com/bfs/archive/7675affb657fc8f2e6beb84a533d86512a8c4202.jpg",
			"title": "【美国有嘻哈】《嘻哈星节奏|Rhythm + Flow》|英语中字|卡迪·B / T.I. / 钱斯勒|NETFLIX|持续更新",
			"pubdate": 1570612692,
			"ctime": 1570612692,
			"desc": "Netflix\n国际巨星卡迪·B、饶舌钱斯勒、TI蒂普·哈里斯齐聚一堂,在Netflix 的首部音乐竞赛节目《嘻哈星节奏》中物色下一代亮眼嘻哈新秀。本节目邀来业界传奇人物共襄盛举,穿越数座城市,到洛杉矶、纽约、亚特兰大和芝加哥等嘻哈重镇挖掘才华洋溢的新人,并帮助这些未成名的艺术家崭露头角。",
			"state": 0,
			"attribute": 2113859,
			"duration": "03:37:32",
			"rights": {
				"bp": 0,
				"elec": 0,
				"download": 1,
				"movie": 0,
				"pay": 0,
				"hd5": 1,
				"no_reprint": 0,
				"autoplay": 1,
				"ugc_pay": 0,
				"is_cooperation": 0,
				"ugc_pay_preview": 0
			},
			"owner": {
				"mid": 314790542,
				"name": "Coun4",
				"face": "http://i1.hdslb.com/bfs/face/912697005111fde1a42ef83f390da30fc17a69a6.jpg"
			},
			"stat": {
				"aid": 70663655,
				"view": 86398,
				"danmaku": 5017,
				"reply": 253,
				"favorite": 3552,
				"coin": 957,
				"share": 2245,
				"now_rank": 0,
				"his_rank": 0,
				"like": 1917,
				"dislike": 0,
				"evaluation": ""
			},
			"dynamic": "#嘻哈星节奏##嘻哈王##Rhythm + Flow:Rapper#\n热爱音乐,平凡的你一样可以拥有不平凡的人生。\n给你展示自我的舞台,废话不多说,It's Showtime!\nRespect, peace & love.",
			"cid": 122426590,
			"dimension": {
				"width": 1920,
				"height": 1080,
				"rotate": 0
			},
			"no_cache": false,
			"pages": [{
				"cid": 122426590,
				"page": 1,
				"from": "vupload",
				"part": "EP01. 洛杉矶选秀",
				"duration": 3465,
				"vid": "",
				"weblink": "",
				"dimension": {
					"width": 1920,
					"height": 1080,
					"rotate": 0
				}
			}, {
				"cid": 122427982,
				"page": 2,
				"from": "vupload",
				"part": "EP02. 纽约选秀",
				"duration": 3133,
				"vid": "",
				"weblink": "",
				"dimension": {
					"width": 1920,
					"height": 1080,
					"rotate": 0
				}
			}, {
				"cid": 122429123,
				"page": 3,
				"from": "vupload",
				"part": "EP03. 亚特兰大选秀",
				"duration": 3181,
				"vid": "",
				"weblink": "",
				"dimension": {
					"width": 1920,
					"height": 1080,
					"rotate": 0
				}
			}, {
				"cid": 122430489,
				"page": 4,
				"from": "vupload",
				"part": "EP04. 芝加哥选秀",
				"duration": 3273,
				"vid": "",
				"weblink": "",
				"dimension": {
					"width": 1920,
					"height": 1080,
					"rotate": 0
				}
			}],
			"subtitle": {
				"allow_submit": false,
				"list": [{
					"id": 16997718492708868,
					"lan": "zh-Hant",
					"lan_doc": "中文(繁体)",
					"is_lock": false,
					"subtitle_url": "http://i0.hdslb.com/bfs/subtitle/9395320795b484c45173b13ff2736355ad112dc7.json",
					"author": {
						"mid": 0,
						"name": "",
						"sex": "",
						"face": "",
						"sign": "",
						"rank": 0,
						"birthday": 0
					}
				}, {
					"id": 16997839878488068,
					"lan": "en-US",
					"lan_doc": "英语(美国)",
					"is_lock": false,
					"subtitle_url": "http://i0.hdslb.com/bfs/subtitle/1ba653423487d65de099e55bd452ceace3a25919.json",
					"author": {
						"mid": 0,
						"name": "",
						"sex": "",
						"face": "",
						"sign": "",
						"rank": 0,
						"birthday": 0
					}
				}, {
					"id": 16998625400324098,
					"lan": "zh-Hans",
					"lan_doc": "中文(简体)",
					"is_lock": false,
					"subtitle_url": "http://i0.hdslb.com/bfs/subtitle/d99b718d326043dc16772b8f7e534ec68fc70880.json",
					"author": {
						"mid": 0,
						"name": "",
						"sex": "",
						"face": "",
						"sign": "",
						"rank": 0,
						"birthday": 0
					}
				}]
			},
			"originTitle": "【美国有嘻哈】《嘻哈星节奏|Rhythm + Flow》|英语中字|卡迪·B / T.I. / 钱斯勒|NETFLIX|持续更新",
			"reid": 5,
			"toptype": "娱乐",
			"initUrl": "//upos-hz-mirrorks3u.acgvideo.com/upgcxcode/90/65/122426590/122426590-1-6.mp4?e=ig8euxZM2rNcNbuVhwdVtWuVhwdVNEVEuCIv29hEn0l5QK==&uipk=5&nbs=1&deadline=1570938144&gen=playurl&os=ks3u&oi=3742040479&trid=06d1dfd471d34b8eb40c4cee6c79531ah&platform=html5&upsig=904b08c9cd9418a8acfd4384c8b05a30&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0",
			"status": "normal",
			"initCid": 122426590,
			"initDuration": 3465,
			"initPage": 1
		}
	},
	"authInfo": "",
	"listInfo": "",
	"audioReducer": null,
	"audiolistReducer": null,
	"experiment": {
		"normal": "experiment",
		"newOpenApp": 1,
		"videoHlv": "1"
	},
	"abserver": {
		"errorCode": 0,
		"expire": 1570931544623,
		"hit": {
			"hitGroup": "768_683_822"
		},
		"vars": [{
			"force": 0,
			"group": 768,
			"name": "playblock_dialog",
			"value": 1
		}, {
			"force": 0,
			"group": 683,
			"name": "H5_download_ex",
			"value": 0
		}, {
			"force": 0,
			"group": 822,
			"name": "copylink_abtest",
			"value": 1
		}]
	},
	"videoReducer": null,
	"videoTag": "",
	"rankingInfo": 0,
	"keywordInfo": "",
	"pageTitle": "",
	"partitionList": "",
	"tagInfo": "",
	"tagVideos": "",
	"tagSimilar": "",
	"userStatus": "",
	"status": "",
	"forbidden": false,
	"videoState": "init",
	"gameMode": ""
};

这其中的

"initUrl": "//upos-hz-mirrorks3u.acgvideo.com/upgcxcode/90/65/122426590/122426590-1-6.mp4?e=ig8euxZM2rNcNbuVhwdVtWuVhwdVNEVEuCIv29hEn0l5QK==&uipk=5&nbs=1&deadline=1570938144&gen=playurl&os=ks3u&oi=3742040479&trid=06d1dfd471d34b8eb40c4cee6c79531ah&platform=html5&upsig=904b08c9cd9418a8acfd4384c8b05a30&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0"

就是一个用于播放的链接,定位到一个mp4文件。在某些浏览器中该文件是没有办法播放,是由于reffer检查,如果要将该链接引入到其他站点播放,则需要设置成 no-reffer。

2.高清下载

you-get 想必很多人都熟悉了。

[email protected]:~$ you-get -u https://www.bilibili.com/video/av53788919?from=search&seid=18263359557066961621
[1] 6330
[email protected]:~$ site:                Bilibili
title:               初夏鞋子合集 | Shoes Collection | Meng Mao
stream:
    - format:        dash-flv
      container:     mp4
      quality:       高清 1080P
      size:          93.0 MiB (97562752 bytes)
    # download-with: you-get --format=dash-flv [URL]

User Agent: Python-urllib/3.5
Real URLs:
http://upos-hz-mirrorwcsu.acgvideo.com/upgcxcode/12/76/94087612/94087612-1-30077.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1570938738&gen=playurl&os=wcsu&oi=3742040479&trid=aea76d5dd80e481b991f0493f08f002eu&platform=pc&upsig=b6c4a378a7de6e8ce1148acb2c2f5879&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0
http://upos-hz-mirrorcosu.acgvideo.com/upgcxcode/12/76/94087612/94087612-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1570938738&gen=playurl&os=cosu&oi=3742040479&trid=aea76d5dd80e481b991f0493f08f002eu&platform=pc&upsig=44e8b21648f11edead3b09f784030012&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&mid=0

[1]+  已完成               you-get -u https://www.bilibili.com/video/av53788919?from=search

Real URLs:中包含m4s文件链接

但是要下载,只需要 you-get URL 即可,如下。

you-get https://www.bilibili.com/video/av53788919?from=search&seid=18263359557066961621

这篇文章诞生,感谢网友