当前位置: 首页 > news >正文

python下载b站视频

python下载b站视频源码参考

import time
import requests
import json
import re
from lxml import etree
from moviepy.editor import *def merge_video_audio(video_path, audio_path):print('原始视频音频合并中,请耐心等待~')# 获取下载好的音频和视频文件vd = VideoFileClip(video_path)ad = AudioFileClip(audio_path)vd2 = vd.set_audio(ad)  # 将提取到的音频和视频文件进行合成output = video_path.replace('_video','')vd2.write_videofile(output)  # 输出新的视频文件# 移除原始的视频和音频os.remove(video_path)os.remove(audio_path)def download_video(jump_url_list,keyword):# 遍历我们刚才获取到的所有跳转链接for jump_url in jump_url_list: # 先取一个视频进行测试print('正在下载:',jump_url)# 发送请求headers = {'Accept': '*/*','Accept-Language': 'en-US,en;q=0.5','User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'}response = requests.get(jump_url, headers=headers)# 视频详情jsonmatch = re.search( '__playinfo__=(.*?)</script><script>',response.text)playinfo =  json.loads(match.group(1))# 视频内容jsonmatch = re.search( r'__INITIAL_STATE__=(.*?);\(function\(\)',response.text)initial_state = json.loads(match.group(1))# 视频分多种格式,直接取分辨率最高的视频 1080pvideo_url = playinfo['data']['dash']['video'][0]['baseUrl']# 取出音频地址audio_url = playinfo['data']['dash']['audio'][0]['baseUrl']title = initial_state['videoData']['title']print('视频名字:',title)# print('视频地址:', video_url)# print('音频地址:', audio_url)# 根据关键词创建文件夹if not os.path.exists(keyword):os.mkdir(keyword)# 下载视频headers.update({"Referer": jump_url})video_content = requests.get(video_url, headers=headers)received_video = 0video_path = f'./{keyword}/{title}_video.mp4'with open(video_path, 'ab') as output:while int(video_content.headers['content-length']) > received_video:headers['Range'] = 'bytes=' + str(received_video) + '-'response = requests.get(video_url, headers=headers)output.write(response.content)received_video += len(response.content)# 下载音频audio_content = requests.get(audio_url, headers=headers)received_audio = 0audio_path = f'./{keyword}/{title}_audio.mp4'with open(audio_path, 'ab') as output:while int(audio_content.headers['content-length']) > received_audio:# 视频分片下载headers['Range'] = 'bytes=' + str(received_audio) + '-'response = requests.get(audio_url, headers=headers)output.write(response.content)received_audio += len(response.content)# 合并视频和音频merge_video_audio(video_path, audio_path)print('********************这是一条隔离线***************************')time.sleep(1)  if __name__ == '__main__':jump_url_list = ["https://www.bilibili.com/video/BV1Gb42177YX/"]download_video(jump_url_list, "ddd")


http://www.mrgr.cn/news/2419.html

相关文章:

  • 大数据量实现滚动分页-vue3+element-plus实现方式
  • 后端Java秋招面试中的自我介绍需要说什么?
  • nginx核心配置示例
  • Go语言基础--switch
  • 第3章处理机调度与死锁
  • 树莓派开发笔记09-树莓派的UDP通信实验
  • 【人工智能】Transformers之Pipeline(十一):零样本图片分类(zero-shot-image-classification)
  • 内网拓扑可视化及管控技术
  • TypeScript学习笔记1---认识ts与js的异同、ts的所有数据类型详解
  • .Net插件开发开源框架
  • WPF学习(8) --Windows API函数的使用
  • 探索 HarmonyOS 的层叠布局:灵活的 Stack 容器
  • 原神4.8版本抽到角色和重点培养数据表
  • 高性能企业WEB服务器
  • 水利机械5G智能制造工厂物联数字孪生平台,推进制造业数字化转型
  • OpenCV4特征匹配
  • 快速上手 iOS Protocol Buffer
  • Xshell 连接服务器
  • 人工智能将原本需要数月的镜头设计工作缩短为一天
  • JVM(Java虚拟机) - 深入了解Java中的GC命令:如何优化垃圾回收