mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-09-08 09:06:40 +08:00
feat: Add subtitle priority fetching and enhance mindmap export
## Subtitle Priority (Backend) - Add download_subtitles() method to base downloader - Implement Bilibili subtitle fetching with cookies support - Implement YouTube subtitle fetching - Support SRT and JSON3 format parsing - Prioritize platform subtitles over Whisper transcription ## Mindmap Export Enhancements (Frontend) - Add SVG vector export with proper viewBox handling - Add XMind format export with Chinese character encoding fix - Fix PNG/SVG export to capture full content by calling fit() before export - Add JSZip dependency for XMind export Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
10311c1438
commit
85b24dee40
@@ -1,14 +1,19 @@
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
from abc import ABC
|
||||
from typing import Union, Optional
|
||||
from typing import Union, Optional, List
|
||||
|
||||
import yt_dlp
|
||||
|
||||
from app.downloaders.base import Downloader, DownloadQuality
|
||||
from app.models.notes_model import AudioDownloadResult
|
||||
from app.models.transcriber_model import TranscriptResult, TranscriptSegment
|
||||
from app.utils.path_helper import get_data_dir
|
||||
from app.utils.url_parser import extract_video_id
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class YoutubeDownloader(Downloader, ABC):
|
||||
def __init__(self):
|
||||
@@ -92,3 +97,119 @@ class YoutubeDownloader(Downloader, ABC):
|
||||
raise FileNotFoundError(f"视频文件未找到: {video_path}")
|
||||
|
||||
return video_path
|
||||
|
||||
def download_subtitles(self, video_url: str, output_dir: str = None,
|
||||
langs: List[str] = None) -> Optional[TranscriptResult]:
|
||||
"""
|
||||
尝试获取YouTube视频字幕(优先人工字幕,其次自动生成)
|
||||
|
||||
:param video_url: 视频链接
|
||||
:param output_dir: 输出路径
|
||||
:param langs: 优先语言列表
|
||||
:return: TranscriptResult 或 None
|
||||
"""
|
||||
if output_dir is None:
|
||||
output_dir = get_data_dir()
|
||||
if not output_dir:
|
||||
output_dir = self.cache_data
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
|
||||
if langs is None:
|
||||
langs = ['zh-Hans', 'zh', 'zh-CN', 'zh-TW', 'en', 'en-US']
|
||||
|
||||
video_id = extract_video_id(video_url, "youtube")
|
||||
|
||||
ydl_opts = {
|
||||
'writesubtitles': True,
|
||||
'writeautomaticsub': True,
|
||||
'subtitleslangs': langs,
|
||||
'subtitlesformat': 'json3',
|
||||
'skip_download': True,
|
||||
'outtmpl': os.path.join(output_dir, f'{video_id}.%(ext)s'),
|
||||
'quiet': True,
|
||||
}
|
||||
|
||||
try:
|
||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||
info = ydl.extract_info(video_url, download=True)
|
||||
|
||||
# 查找下载的字幕文件
|
||||
subtitles = info.get('requested_subtitles') or {}
|
||||
if not subtitles:
|
||||
logger.info(f"YouTube视频 {video_id} 没有可用字幕")
|
||||
return None
|
||||
|
||||
# 按优先级查找字幕文件
|
||||
subtitle_file = None
|
||||
detected_lang = None
|
||||
for lang in langs:
|
||||
if lang in subtitles:
|
||||
subtitle_file = os.path.join(output_dir, f"{video_id}.{lang}.json3")
|
||||
detected_lang = lang
|
||||
break
|
||||
|
||||
# 如果按优先级没找到,取第一个可用的
|
||||
if not subtitle_file:
|
||||
for lang, sub_info in subtitles.items():
|
||||
subtitle_file = os.path.join(output_dir, f"{video_id}.{lang}.json3")
|
||||
detected_lang = lang
|
||||
break
|
||||
|
||||
if not subtitle_file or not os.path.exists(subtitle_file):
|
||||
logger.info(f"字幕文件不存在: {subtitle_file}")
|
||||
return None
|
||||
|
||||
# 解析字幕文件
|
||||
return self._parse_json3_subtitle(subtitle_file, detected_lang)
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"获取YouTube字幕失败: {e}")
|
||||
return None
|
||||
|
||||
def _parse_json3_subtitle(self, subtitle_file: str, language: str) -> Optional[TranscriptResult]:
|
||||
"""
|
||||
解析 json3 格式字幕文件
|
||||
|
||||
:param subtitle_file: 字幕文件路径
|
||||
:param language: 语言代码
|
||||
:return: TranscriptResult
|
||||
"""
|
||||
try:
|
||||
with open(subtitle_file, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
segments = []
|
||||
events = data.get('events', [])
|
||||
|
||||
for event in events:
|
||||
# json3 格式中时间单位是毫秒
|
||||
start_ms = event.get('tStartMs', 0)
|
||||
duration_ms = event.get('dDurationMs', 0)
|
||||
|
||||
# 提取文本
|
||||
segs = event.get('segs', [])
|
||||
text = ''.join(seg.get('utf8', '') for seg in segs).strip()
|
||||
|
||||
if text: # 只添加非空文本
|
||||
segments.append(TranscriptSegment(
|
||||
start=start_ms / 1000.0,
|
||||
end=(start_ms + duration_ms) / 1000.0,
|
||||
text=text
|
||||
))
|
||||
|
||||
if not segments:
|
||||
return None
|
||||
|
||||
full_text = ' '.join(seg.text for seg in segments)
|
||||
|
||||
logger.info(f"成功解析YouTube字幕,共 {len(segments)} 段")
|
||||
return TranscriptResult(
|
||||
language=language,
|
||||
full_text=full_text,
|
||||
segments=segments,
|
||||
raw={'source': 'youtube_subtitle', 'file': subtitle_file}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"解析字幕文件失败: {e}")
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user