弹幕偏移小工具
This commit is contained in:
parent
3ce4249082
commit
92a0115e3f
54
danmaku_xml_helper.py
Normal file
54
danmaku_xml_helper.py
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import datetime
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
|
||||||
|
class NoDanmakuException(Exception):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
class DanmakuFormatErrorException(Exception):
|
||||||
|
...
|
||||||
|
|
||||||
|
|
||||||
|
def check_file_exist(file):
|
||||||
|
if not os.path.isfile(file):
|
||||||
|
raise FileNotFoundError("文件不存在:%s" % file)
|
||||||
|
|
||||||
|
|
||||||
|
def get_file_start(file: os.PathLike[str]) -> float:
|
||||||
|
with open(file, "r", encoding="utf-8") as f:
|
||||||
|
soup = BeautifulSoup("".join(f.readlines()), "lxml")
|
||||||
|
danmaku_item = soup.find("d")
|
||||||
|
if danmaku_item is None:
|
||||||
|
# 没有弹幕?
|
||||||
|
raise NoDanmakuException()
|
||||||
|
danmaku_info = danmaku_item["p"]
|
||||||
|
split_info = danmaku_info.split(",")
|
||||||
|
if len(split_info) < 5:
|
||||||
|
raise DanmakuFormatErrorException()
|
||||||
|
bias_sec = float(split_info[0])
|
||||||
|
bias_ts_ms = int(split_info[4])
|
||||||
|
return bias_ts_ms / 1000 - bias_sec
|
||||||
|
|
||||||
|
|
||||||
|
def diff_danmaku_files(base_file: os.PathLike[str], file: os.PathLike[str]) -> float:
|
||||||
|
return get_file_start(file) - get_file_start(base_file)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("base", help="以此为标准")
|
||||||
|
parser.add_argument("file", nargs="+", help="需要对齐的文件")
|
||||||
|
args = parser.parse_args()
|
||||||
|
check_file_exist(args.base)
|
||||||
|
base_start_ts = get_file_start(args.base)
|
||||||
|
base_start = datetime.datetime.fromtimestamp(base_start_ts)
|
||||||
|
print("[+] 基准文件[{:s}],开始时间:{:.3f},时间:{}".format(args.base, base_start_ts, base_start))
|
||||||
|
for _file in args.file:
|
||||||
|
check_file_exist(_file)
|
||||||
|
file_start_ts = get_file_start(_file)
|
||||||
|
diff_sec = file_start_ts - base_start_ts
|
||||||
|
print("[+] 待调整文件[{:s}],开始时间:{:.3f},偏差:{:.3f}".format(_file, file_start_ts, diff_sec))
|
7
requirements.txt
Normal file
7
requirements.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
beautifulsoup4==4.10.0
|
||||||
|
bs4==0.0.1
|
||||||
|
lxml==4.8.0
|
||||||
|
pip==21.3.1
|
||||||
|
setuptools==60.2.0
|
||||||
|
soupsieve==2.3.1
|
||||||
|
wheel==0.37.1
|
Loading…
x
Reference in New Issue
Block a user