From fcd61d7a0942a3f06fac79375986661aa00caddc Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Mon, 18 Apr 2022 10:07:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E5=8F=8D=E5=90=91=E5=81=8F=E7=A7=BB=E8=BF=87=E5=A4=A7=E7=9A=84?= =?UTF-8?q?=E5=BC=B9=E5=B9=95=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/api/bilirecorder_blueprint.py | 21 ++++++++++++++++++++- worker/danmaku.py | 9 ++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/controller/api/bilirecorder_blueprint.py b/controller/api/bilirecorder_blueprint.py index a26a4b8..5f22779 100644 --- a/controller/api/bilirecorder_blueprint.py +++ b/controller/api/bilirecorder_blueprint.py @@ -11,6 +11,9 @@ from model.DanmakuClip import DanmakuClip from model.VideoClip import VideoClip from model.Workflow import Workflow from worker.danmaku import do_workflow +from workflow.danmaku import get_file_start +from workflow.video import get_video_real_duration +from exception.danmaku import DanmakuException blueprint = Blueprint("api_bilirecorder", __name__, url_prefix="/api/bilirecorder") @@ -86,6 +89,7 @@ def collect_danmaku_files(workflow: Optional[Workflow]): full_path = clip.full_path pre_file_name = os.path.splitext(full_path)[0] # 理论上也只有一个结果 + start_time_ts = None for danmaku_file in glob("{}*xml".format(pre_file_name)): relpath = os.path.relpath(danmaku_file, BILILIVE_RECORDER_DIRECTORY) danmaku = DanmakuClip.query.filter( @@ -93,6 +97,10 @@ def collect_danmaku_files(workflow: Optional[Workflow]): DanmakuClip.base_path == BILILIVE_RECORDER_DIRECTORY ).first() if danmaku is None: + try: + start_time_ts = get_file_start(danmaku_file) + except DanmakuException: + start_time_ts = None danmaku = DanmakuClip() danmaku.file = relpath danmaku.base_path = BILILIVE_RECORDER_DIRECTORY @@ -100,6 +108,10 @@ def collect_danmaku_files(workflow: Optional[Workflow]): danmaku.workflow = workflow db.session.add(danmaku) workflow.danmaku_clips.append(danmaku) + if start_time_ts is None: + if clip.duration is None or clip.duration == 0: + clip.duration = get_video_real_duration(clip.full_path) + start_time_ts = datetime.now().timestamp() - clip.duration for danmaku_file in glob(os.path.join(XIGUALIVE_RECORDER_DIRECTORY, "*.xml")): relpath = os.path.relpath(danmaku_file, XIGUALIVE_RECORDER_DIRECTORY) danmaku = DanmakuClip.query.filter( @@ -107,10 +119,17 @@ def collect_danmaku_files(workflow: Optional[Workflow]): DanmakuClip.base_path == XIGUALIVE_RECORDER_DIRECTORY ).first() if danmaku is None: + try: + start_time_xg = get_file_start(danmaku_file) + except DanmakuException: + continue + bias = start_time_xg - start_time_ts + if bias < -600: + print("弹幕文件", danmaku_file, "反向偏移超过10分钟") danmaku = DanmakuClip() danmaku.file = relpath danmaku.base_path = XIGUALIVE_RECORDER_DIRECTORY - danmaku.offset = 0 + danmaku.offset = bias danmaku.workflow = workflow db.session.add(danmaku) workflow.danmaku_clips.append(danmaku) diff --git a/worker/danmaku.py b/worker/danmaku.py index c7e47cf..9cf8a07 100644 --- a/worker/danmaku.py +++ b/worker/danmaku.py @@ -14,11 +14,14 @@ def do_workflow(video_file, danmaku_base_file, *danmaku_files): base_start = datetime.fromtimestamp(start_ts) new_file_name = base_start.strftime("%Y%m%d_%H%M.flv") result.append(danmaku_to_subtitle(danmaku_base_file, 0)) - for file in danmaku_files: + for danmaku_file in danmaku_files: try: - result.append(danmaku_to_subtitle(file, diff_danmaku_files(danmaku_base_file, file))) + bias = diff_danmaku_files(danmaku_base_file, danmaku_file) + if bias < -600: + print("弹幕文件", danmaku_file, "反向偏移超过10分钟") + result.append(danmaku_to_subtitle(danmaku_file, bias)) except DanmakuException: - print("弹幕文件", file, "异常") + print("弹幕文件", danmaku_file, "异常") continue print(result) encode_video_with_subtitles(video_file, result, new_file_name)