您当前的位置: 首页 >  Python

轻松学Python

暂无认证

  • 0浏览

    0关注

    317博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

用Python写一个自动下载视频、弹幕、评论的软件(2022最新)

轻松学Python 发布时间:2022-08-23 22:33:11 ,浏览量:0

文章目录
  • 序言
  • 效果展示
  • 下载视频
  • 下载弹幕
  • 下载评论
  • 软件生成
  • 打包

序言

哈喽兄弟们,今天来实现一个Python采集视频、弹幕、评论与一体的小软件。

平常咱们都是直接代码运行,不过今天我们做成软件,这样的话,咱们不仅能自己用,还能分享给小伙伴,女朋友一起使用。

内容有点多,拿好小本本,做好笔记,发车了~

效果展示

我们先来看看效果

整体界面

我随便找个视频下载一下

弹幕和评论我都顺便下载了

在这里插入图片描述 有一说一,确实方便,就是下载视频太大的话,会卡一下。

不过我这里视频没有做去水印,所以下载下来还是有水印的。

接下来看看代码

下载视频

数据请求模块 ,第三方模块,需要在cmd里进行 pip install requests 安装

import requests  

正则表达式,内置模块 ,不需要安装

import re  

json模块 ,内置模块, 不需要安装

import json    

格式输出模块,内置模块 ,不需要安装

from pprint import  pprint   

导入进程

import subprocess

文件操作模块

import os

发送请求

url = f'https://****.com/video/{bv_id}'

headers = {

    'referer': 'https://****.com/video/',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}

获取数据, 获取服务器返回响应数据 —> 文本数据 print(response.text)

response = requests.get(url=url, headers=headers)

解析数据,提取我们想要数据内容。

正则表达式 —> 对于字符串数据类型进行提取/解析 re模块findall() ----> 告诉程序从什么地方去找什么数据 re.findall() '“title”:“(.?)“,“pubdate”', response.text 从 response.text 里面 去找 “title”:”(.?)”,“pubdate” 其中括号里内容就是我们要的。

title = re.findall('"title":"(.*?)","pubdate"', response.text)[0].replace(' ', '')

html_data = re.findall('window.__playinfo__=(.*?)', response.text)[0]

json_data = json.loads(html_data)

audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
video_url = json_data['data']['dash']['video'][0]['baseUrl']
audio_content = requests.get(url=audio_url, headers=headers).content
video_content = requests.get(url=video_url, headers=headers).content
if not os.path.exists('video\\'):
    os.mkdir('video\\')
with open('video\\' + title + '.mp3', mode='wb') as audio:
    audio.write(audio_content)
with open('video\\' + title + '.mp4', mode='wb') as video:
    video.write(video_content)

获取音频内容以及视频画面内容

cmd = f"ffmpeg -i video\\{title}.mp4 -i video\\{title}.mp3 -c:v copy -c:a aac -strict experimental video\\{title}output.mp4"
subprocess.run(cmd, shell=True)
os.remove(f'video\\{title}.mp4')
os.remove(f'video\\{title}.mp3')
return title
下载弹幕

部分代码展示

def get_response(html_url):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
    }
    response = requests.get(url=html_url, headers=headers)
    response.encoding = response.apparent_encoding
    return response


def get_Dm_url(bv_id):
    link = f'https://www.*****/video/{bv_id}/'
    html_data = get_response(link).text
    Dm_url = re.findall('弹幕', html_data)[0]
    title = re.findall('            
关注
打赏
1663832040
查看更多评论
0.0740s