From 92a0115e3fd98a2ed88532deb5301d0889918458 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Tue, 29 Mar 2022 09:05:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=B9=E5=B9=95=E5=81=8F=E7=A7=BB=E5=B0=8F?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- danmaku_xml_helper.py | 54 +++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 7 ++++++ 2 files changed, 61 insertions(+) create mode 100644 danmaku_xml_helper.py create mode 100644 requirements.txt diff --git a/danmaku_xml_helper.py b/danmaku_xml_helper.py new file mode 100644 index 0000000..e74ef5e --- /dev/null +++ b/danmaku_xml_helper.py @@ -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)) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6721c87 --- /dev/null +++ b/requirements.txt @@ -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