Skip to content

Instantly share code, notes, and snippets.

@tookdes
Last active February 17, 2020 16:23
Show Gist options
  • Save tookdes/bd6e178a35270b8ee50e62b86f63941f to your computer and use it in GitHub Desktop.
Save tookdes/bd6e178a35270b8ee50e62b86f63941f to your computer and use it in GitHub Desktop.
结合 annie 和 niconvert 自动下载 B 站视频和弹幕的脚本
# 需要下载
# https://github.com/iawia002/annie
# https://github.com/muzuiget/niconvert
# 将 annie 可执行文件、Niconvert 的主 pyw 文件放在本脚本同目录或者加入环境变量
# 或者自行修改相对路径
# -*- coding: utf-8 -*-
import subprocess
import random
import re
import requests
import os
import unicodedata
def is_num(av):
try:
float(av)
return True
except ValueError:
pass
try:
unicodedata.numeric(av)
return True
except (TypeError, ValueError):
pass
return False
def get_epi(url):
headers = {
"User-Agent": random.choice("Agents")
}
u = requests.get(url=url, headers=headers)
html = u.text
epi = len(re.findall(r'"page"', html))
return epi
def get_vid_xml(url,av_p):
exe = "annie -c cookies.txt -C "+ url
subprocess.call(exe)
def xml2ass():
file_list = os.listdir(os.getcwd())
print(file_list)
for i in file_list:
if os.path.splitext(i)[1] == '.xml':
exe = 'python niconvert.pyw "'+ i +\
'" -o "'+ os.path.splitext(i)[0] +'".ass +r 1280x720'
print(exe)
subprocess.call(exe)
os.remove(i)
def main():
av_input = input("输入网址或者AV号:").split("?", 1)[0]
if is_num(av_input) == True:
av_num = av_input
av_url = "https://www.bilibili.com/video/av"+ av_num
elif av_input[:2] == "av":
av_num = av_input
av_url = "https://www.bilibili.com/video/av"+ av_num
elif av_input[:33] == "https://www.bilibili.com/video/av":
av_url = av_input
av_num = av_input[33:]
elif av_input[:15] == "https://b23.tv/":
av_num = av_input[15:]
av_url = "https://www.bilibili.com/video/av"+ av_num
else:
print("无法识别,请检查输入的信息!")
exit()
av = "av"+ av_num
p = get_epi(av_url)
for i in range(1,p + 1):
av_p = av +"_P"+ str(i)
av_p_url = av_url + "?p=" + str(i)
get_vid_xml(av_p_url,av_p)
xml2ass()
main()
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment