File size: 10,251 Bytes
0aee47a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
"""
bilibili_api.ass
有关 ASS 文件的操作
"""
import os
from tempfile import gettempdir
from typing import Union, Optional
from .video import Video
from .bangumi import Episode
from .cheese import CheeseVideo
from .utils.srt2ass import srt2ass
from .utils.json2srt import json2srt
from .utils.credential import Credential
from .utils.danmaku2ass import Danmaku2ASS
from .utils.network import get_session
from .exceptions.ArgsException import ArgsException
def export_ass_from_xml(
file_local,
output_local,
stage_size,
font_name,
font_size,
alpha,
fly_time,
static_time,
) -> None:
"""
以一个 XML 文件创建 ASS
一定看清楚 Arguments!
Args:
file_local (str) : 文件输入
output_local (str) : 文件输出
stage_size (tuple(int)): 视频大小
font_name (str) : 字体
font_size (float) : 字体大小
alpha (float) : 透明度(0-1)
fly_time (float) : 滚动弹幕持续时间
static_time (float) : 静态弹幕持续时间
"""
Danmaku2ASS(
input_files=file_local,
input_format="Bilibili",
output_file=output_local,
stage_width=stage_size[0],
stage_height=stage_size[1],
reserve_blank=0,
font_face=font_name,
font_size=font_size,
text_opacity=alpha,
duration_marquee=fly_time,
duration_still=static_time,
)
def export_ass_from_srt(file_local, output_local) -> None:
"""
转换 srt 至 ass
Args:
file_local (str): 文件位置
output_local (str): 输出位置
"""
srt2ass(file_local, output_local, "movie")
def export_ass_from_json(file_local, output_local) -> None:
"""
转换 json 至 ass
Args:
file_local (str): 文件位置
output_local (str): 输出位置
"""
json2srt(file_local, output_local.replace(".ass", ".srt"))
srt2ass(output_local.replace(".ass", ".srt"), output_local, "movie")
os.remove(output_local.replace(".ass", ".srt"))
async def make_ass_file_subtitle(
obj: Union[Video, Episode],
page_index: Optional[int] = 0,
cid: Optional[int] = None,
out: Optional[str] = "test.ass",
lan_name: Optional[str] = "中文(自动生成)",
lan_code: Optional[str] = "ai-zh",
credential: Credential = Credential(),
) -> None:
"""
生成视频字幕文件
Args:
obj (Union[Video,Episode]): 对象
page_index (int, optional) : 分 P 索引
cid (int, optional) : cid
out (str, optional) : 输出位置. Defaults to "test.ass".
lan_name (str, optional) : 字幕名,如”中文(自动生成)“,是简介的 subtitle 项的'list'项中的弹幕的'lan_doc'属性。Defaults to "中文(自动生成)".
lan_code (str, optional) : 字幕语言代码,如 ”中文(自动翻译)” 和 ”中文(自动生成)“ 为 "ai-zh"
credential (Credential) : Credential 类. 必须在此处或传入的视频 obj 中传入凭据,两者均存在则优先此处
"""
# 目测必须得有 Credential 才能获取字幕
if credential.has_sessdata():
obj.credential = credential
elif not obj.credential.has_sessdata():
raise credential.raise_for_no_sessdata()
if isinstance(obj, Episode):
info = await obj.get_player_info(cid=await obj.get_cid(), epid=obj.get_epid())
else:
if cid == None:
if page_index == None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await obj.get_cid(page_index=page_index)
info = await obj.get_player_info(cid=cid)
json_files = info["subtitle"]["subtitles"]
for subtitle in json_files:
if subtitle["lan_doc"] == lan_name or subtitle["lan"] == lan_code:
url = subtitle["subtitle_url"]
if isinstance(obj, Episode) or "https:" not in url:
url = "https:" + url
req = await get_session().request("GET", url)
file_dir = gettempdir() + "/" + "subtitle.json"
with open(file_dir, "wb") as f:
f.write(req.content)
export_ass_from_json(file_dir, out)
return
raise ValueError("没有找到指定字幕")
async def make_ass_file_danmakus_protobuf(
obj: Union[Video, Episode, CheeseVideo],
page: int = 0,
out="test.ass",
cid: Union[int, None] = None,
credential: Union[Credential, None] = None,
date=None,
font_name="Simsun",
font_size=25.0,
alpha=1,
fly_time=7,
static_time=5,
) -> None:
"""
生成视频弹幕文件
来源:protobuf
Args:
obj (Union[Video,Episode,CheeseVideo]) : 对象
page (int, optional) : 分 P 号. Defaults to 0.
out (str, optional) : 输出文件. Defaults to "test.ass"
cid (int | None, optional) : cid. Defaults to None.
credential (Credential | None, optional) : 凭据. Defaults to None.
date (datetime.date, optional) : 获取时间. Defaults to None.
font_name (str, optional) : 字体. Defaults to "Simsun".
font_size (float, optional) : 字体大小. Defaults to 25.0.
alpha (float, optional) : 透明度(0-1). Defaults to 1.
fly_time (float, optional) : 滚动弹幕持续时间. Defaults to 7.
static_time (float, optional) : 静态弹幕持续时间. Defaults to 5.
"""
credential = credential if credential else Credential()
if date:
credential.raise_for_no_sessdata()
if isinstance(obj, Video):
v = obj
if isinstance(obj, Episode):
cid = 0
else:
if cid is None:
if page is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
# type: ignore
cid = await v._Video__get_cid_by_index(page)
try:
info = await v.get_info()
except:
info = {"dimension": {"width": 1440, "height": 1080}}
width = info["dimension"]["width"]
height = info["dimension"]["height"]
if width == 0:
width = 1440
if height == 0:
height = 1080
stage_size = (width, height)
if isinstance(obj, Episode):
danmakus = await v.get_danmakus()
else:
danmakus = await v.get_danmakus(cid=cid, date=date) # type: ignore
elif isinstance(obj, CheeseVideo):
stage_size = (1440, 1080)
danmakus = await obj.get_danmakus()
else:
raise ValueError("请传入 Video/Episode/CheeseVideo 类!")
with open(gettempdir() + "/danmaku_temp.xml", "w+", encoding="utf-8") as file:
file.write("<i>")
for d in danmakus:
file.write(d.to_xml())
file.write("</i>")
export_ass_from_xml(
gettempdir() + "/danmaku_temp.xml",
out,
stage_size,
font_name,
font_size,
alpha,
fly_time,
static_time,
)
async def make_ass_file_danmakus_xml(
obj: Union[Video, Episode, CheeseVideo],
page: int = 0,
out="test.ass",
cid: Union[int, None] = None,
font_name="Simsun",
font_size=25.0,
alpha=1,
fly_time=7,
static_time=5,
) -> None:
"""
生成视频弹幕文件
来源:xml
Args:
obj (Union[Video,Episode,Cheese]): 对象
page (int, optional) : 分 P 号. Defaults to 0.
out (str, optional) : 输出文件. Defaults to "test.ass".
cid (int | None, optional) : cid. Defaults to None.
font_name (str, optional) : 字体. Defaults to "Simsun".
font_size (float, optional) : 字体大小. Defaults to 25.0.
alpha (float, optional) : 透明度(0-1). Defaults to 1.
fly_time (float, optional) : 滚动弹幕持续时间. Defaults to 7.
static_time (float, optional) : 静态弹幕持续时间. Defaults to 5.
"""
if isinstance(obj, Video):
v = obj
if isinstance(obj, Episode):
cid = 0
else:
if cid is None:
if page is None:
raise ArgsException("page_index 和 cid 至少提供一个。")
cid = await v._Video__get_cid_by_index(page) # type: ignore
try:
info = await v.get_info()
except:
info = {"dimension": {"width": 1440, "height": 1080}}
width = info["dimension"]["width"]
height = info["dimension"]["height"]
if width == 0:
width = 1440
if height == 0:
height = 1080
stage_size = (width, height)
if isinstance(obj, Episode):
xml_content = await v.get_danmaku_xml()
else:
xml_content = await v.get_danmaku_xml(cid=cid) # type: ignore
elif isinstance(obj, CheeseVideo):
stage_size = (1440, 1080)
xml_content = await obj.get_danmaku_xml()
else:
raise ValueError("请传入 Video/Episode/CheeseVideo 类!")
with open(gettempdir() + "/danmaku_temp.xml", "w+", encoding="utf-8") as file:
file.write(xml_content)
export_ass_from_xml(
gettempdir() + "/danmaku_temp.xml",
out,
stage_size,
font_name,
font_size,
alpha,
fly_time,
static_time,
)
|