linyq 6cd1ff8b68 refactor(tools): 移除调试日志和未使用的参数- 在 base.py 中移除了调试日志,以减少日志噪音
- 在 generate_script_short.py 中移除了未使用的参数,简化了 API 调用
2025-05-10 23:57:15 +08:00

46 lines
1.2 KiB
Python

# 公共方法
import json
import requests # 新增
from typing import List, Dict
def load_srt(file_path: str) -> List[Dict]:
"""加载并解析SRT文件
Args:
file_path: SRT文件路径
Returns:
字幕内容列表
"""
with open(file_path, 'r', encoding='utf-8-sig') as f:
content = f.read().strip()
# 按空行分割字幕块
subtitle_blocks = content.split('\n\n')
subtitles = []
for block in subtitle_blocks:
lines = block.split('\n')
if len(lines) >= 3: # 确保块包含足够的行
try:
number = int(lines[0].strip())
timestamp = lines[1]
text = ' '.join(lines[2:])
# 解析时间戳
start_time, end_time = timestamp.split(' --> ')
subtitles.append({
'number': number,
'timestamp': timestamp,
'text': text,
'start_time': start_time,
'end_time': end_time
})
except ValueError as e:
print(f"Warning: 跳过无效的字幕块: {e}")
continue
return subtitles