前言
平台的web端是没有开放下载的,所以我们从网页元素里找到视频链接。
但是平台的音频和视频是分离的,所以要找到两个链接下载到本地,然后进行合并操作后保存。
1、获取视频 url
选择一个普通正常的视频,不需要大会员的那种
2、发送请求
定义一个请求函数,用来获取页面元素
def get_url(url):
"""
获取网页元素文本
:param url:
:return: 返回url页面元素
"""
head = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62'
}
res = requests.get(url=url, headers=head)
return res
3、数据解析
定义一个函数来获取视频标题,视频画面,音频的链接
video里的backupUrl就是它的视频链接
audio里的backupUrl就是它的音频链接
def get_json_url(url):
res = get_url(url=url).text
title = re.findall('<title data-vue-meta="true">(.*?)</title>', res)[0] # 获取视频标题
play_info = json.loads(re.findall('<script>window.__playinfo__=(.*?)<script>', res)[0][0:-9])
audio = play_info['data']['dash']['audio'][0]['backupUrl'][0]
video = play_info['data']['dash']['video'][0]['backupUrl'][0]
tit_au_vi = [title, video, audio]
return tit_au_vi
4、本地保存
定义一个函数,用上面获取的链接来进行下载保存成二进制文件
def save(url):
json_data = get_json_url(url=url)
title = json_data[0]
video_content = get_url(url=json_data[1]).content
audio_content = get_url(url=json_data[2]).content
with open(title + '.mp4', mode='wb') as file:
file.write(video_content)
with open(title + '.mp3', mode='wb') as file:
file.write(audio_content)
5、音视频合并
ffmpeg是一款非常好用处理音视频的工具包
pip install ffmpeg
def merge(title):
audio = ffmpeg.input(f'{title}.mp3')
video = ffmpeg.input(f'{title}.mp4')
print("合并视音频")
out = ffmpeg.output(video, audio, f'E:/material/{title}.mp4')
out.run()
os.remove(f'{title}.mp3')
os.remove(f'{title}.mp4')
5、完整代码
import requests
import json
import re
from pprint import pprint
def get_url(url):
"""
获取网页元素文本
:param url:
:return: 返回url页面元素
"""
head = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62',
'referer': 'https://www.**.com/'
}
res = requests.get(url=url, headers=head)
return res
def get_json_url(url):
"""
获取视频标题 视频画面 音频
:param url: 视频页面地址
:return: 返回视频标题 视频画面 音频
"""
res = get_url(url).text
title = re.findall('<title data-vue-meta="true">(.*?)</title>', res)[0] # 获取视频标题
play_info = json.loads(re.findall('<script>window.__playinfo__=(.*?)<script>', res)[0][0:-9])
# pprint(play_info)
video = play_info['data']['dash']['video'][0]['backupUrl'][0]
audio = play_info['data']['dash']['audio'][0]['backupUrl'][0]
tit_au_vi = [title, video, audio]
return tit_au_vi
def save(url):
"""
:param url:
:return:
"""
json_data = get_json_url(url=url)
title = json_data[0]
video_content = get_url(url=json_data[1]).content
audio_content = get_url(url=json_data[2]).content
with open(f'{title}.mp4', mode='wb') as file:
file.write(video_content)
with open(f'{title}.mp3', mode='wb') as file:
file.write(audio_content)
merge(title)
def start(bv):
"""
:param url:
:return:
"""
print('开始下载')
url = f'https://www.**.com/video/{bv}'
save(url)
print('下载完成')
def merge(title):
audio = ffmpeg.input(f'{title}.mp3')
video = ffmpeg.input(f'{title}.mp4')
print("合并视音频")
out = ffmpeg.output(video, audio, f'E:/material/{title}.mp4')
out.run()
os.remove(f'{title}.mp3')
os.remove(f'{title}.mp4')
if __name__ == '__main__':
start('BV1H341157Tz')