import json import os import re import re import re from dataclasses import asdict NOTE_OUTPUT_DIR = "note_results" def replace_content_markers(markdown: str, video_id: str, platform: str = 'bilibili') -> str: """ 替换 *Content-04:16*、Content-04:16 或 Content-[04:16] 为超链接,跳转到对应平台视频的时间位置 """ # 匹配三种形式:*Content-04:16*、Content-04:16、Content-[04:16] pattern = r"(?:\*?)Content-(?:\[(\d{2}):(\d{2})\]|(\d{2}):(\d{2}))" def replacer(match): mm = match.group(1) or match.group(3) ss = match.group(2) or match.group(4) total_seconds = int(mm) * 60 + int(ss) if platform == 'bilibili': url = f"https://www.bilibili.com/video/{video_id}?t={total_seconds}" elif platform == 'youtube': url = f"https://www.youtube.com/watch?v={video_id}&t={total_seconds}s" elif platform == 'douyin': url = f"https://www.douyin.com/video/{video_id}" return f"[原片 @ {mm}:{ss}]({url})" else: return f"({mm}:{ss})" return f"[原片 @ {mm}:{ss}]({url})" return re.sub(pattern, replacer, markdown) def save_note_to_file(task_id: str, note): os.makedirs(NOTE_OUTPUT_DIR, exist_ok=True) with open(os.path.join(NOTE_OUTPUT_DIR, f"{task_id}.json"), "w", encoding="utf-8") as f: json.dump(asdict(note), f, ensure_ascii=False, indent=2)