大家应该都知道,B站上的视频是将video和audio分开的。 通过浏览器的抓包工具可以发现,在播放视频时会不断地请求30077.m4s和30280.m4s这两个URL,其实30280.m4s就是audio文件,而30077.m4s就是视频文件。 首先,先简单的请求一下这些URL。
import requests
from lxml import etree
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31",
"referer": "https://www.bilibili.com/video/BV1A34y1X7qj?spm_id_from=333.851.b_7265636f6d6d656e64.1",
"origin": "https://www.bilibili.com"
}
url = "https://cn-zjhz-cmcc-v-04.bilivideo.com/upgcxcode/42/38/408743842/408743842-1-30280.m4s?e=ig8euxZM2rNcNbdlhoNvNC8BqJIzNbfqXBvEqxTEto8BTrNvN0GvT90W5JZMkX_YN0MvXg8gNEV4NC8xNEV4N03eN0B5tZlqNxTEto8BTrNvNeZVuJ10Kj_g2UB02J0mN0B5tZlqNCNEto8BTrNvNC7MTX502C8f2jmMQJ6mqF2fka1mqx6gqj0eN0B599M=&uipk=5&nbs=1&deadline=1632917146&gen=playurlv2&os=vcache&oi=1880138981&trid=00015f2c380d11204ff59245612e0366968cu&platform=pc&upsig=b0fd7fd8273c21ba56dd76a297d611ef&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,platform&cdnid=4062&mid=691778703&bvc=vod&nettype=0&orderid=0,3&agrr=0&logo=80000000"
resp = requests.get(url=url, headers=headers)
resp.close()
file_name = "test.mp4"
with open(file="D:\\Python\\Blibli\\"+file_name, mode="wb") as fp:
fp.write(resp.content)
print("下载完毕!")
这是我爬取30280.m4s文件的代码,一开始我不确定它是audio还是video,所以我直接将它保存为mp4文件。结果: 没有视频,只有声音,这是个audio文件。
当我爬完30077.m4s文件后,用Windows自带的视频软件播放时,它竟然让我付费下载解编码器,这能忍,我直接下载了PotPlayer,成功播放了视频。 当我把30077.m4s这个URL放到网页源代码上匹配时,发现有结果且"id":80,这个id应该是URL的标识。 在网页源代码中能找到video和audio的URL,这下就简单多了。
我的思路
- 通过get请求,得到当前视频网页的源代码
- 解析网页源代码,得到video和audio的URL
- 请求video和audio的URL,下载video和audio文件
- 整合video和audio文件,得到完整的视频文件
开始编程。
import requests
import re
import os
url = input("请输入视频的网址:")
headers_1 = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31",
"origin": "https://www.bilibili.com"
}
resp_1 = requests.get(url=url, headers=headers_1)
resp_1.encoding = "utf-8"
resp_1.close()
url_video = re.findall(pattern='{"id":80,"baseUrl":"(.*?)","base_url":"', string=resp_1.text)[0]
url_audio = re.findall(pattern='{"id":30280,"baseUrl":"(.*?)","base_url":"', string=resp_1.text)[0]
video_name = re.findall(pattern='<h1 title="(.*?)" class="video-title">', string=resp_1.text)[0]
print("\n"+"视频URL:"+url_video+"\n\n", "音频URL:"+url_audio+"\n\n", "视频名称:"+video_name+"\n")
headers_2 = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31",
"origin": "https://www.bilibili.com",
"referer": url
}
urls = [url_video, url_audio]
a = ['/', '\\', ':', '*', '?', '"', '<', '>', '|']
new_video_name = []
for i in list(video_name):
if i not in a:
new_video_name.append(i)
else:
new_video_name.append('_')
new_video_name = ''.join(new_video_name)
for url in urls:
resp = requests.get(url=url, headers=headers_2)
resp.close()
if url == url_video:
with open(file="_"+new_video_name+".mp4", mode="wb") as fp:
fp.write(resp.content)
print("视频下载完毕!")
else:
with open(file="_"+new_video_name+".mp3", mode="wb") as fp:
fp.write(resp.content)
print("音频下载完毕!")
os.system("ffmpeg -i"+" "+"_"+new_video_name+".mp4"+" "+"-i"+" "+"_"+new_video_name+".mp3"+" "+"-acodec copy -vcodec copy"+" "+new_video_name+".mp4")
os.system('del'+' '+'_'+new_video_name+'.mp4')
os.system('del'+' '+'_'+new_video_name+'.mp3')
print("合并完毕!")
在编程过程中,我发现id为80是不止30077.m4s,还有30080.m4s,虽然通过30077.m4s来链接下载的video文件不能被Windows自带的视频软件播放,但通过30080.m4s下载到的video文件却可以。所以我在编码是请求的是30080.m4s这个URL。 在整合video和audio文件,得到完整的视频文件这个步骤中,我使用ffmpeg将video和audio文件结合,命令是:
ffmpeg -i video文件名 -i audio文件名 -acodec copy -vcodec copy 合并后的文件名
|