mirror of
https://github.com/d0zingcat/MoviePilot-Plugins.git
synced 2026-05-13 23:16:47 +00:00
Merge pull request #749 from TimoYoung/main
This commit is contained in:
@@ -25,14 +25,15 @@
|
||||
"AutoSubv2": {
|
||||
"name": "AI字幕自动生成(v2)",
|
||||
"description": "使用whisper自动生成视频文件字幕,使用大模型翻译字幕成中文。",
|
||||
"version": "1.1",
|
||||
"version": "1.2",
|
||||
"icon": "autosubtitles.jpeg",
|
||||
"author": "TimoYoung",
|
||||
"level": 1,
|
||||
"v2": true,
|
||||
"history": {
|
||||
"v1.0": "first stable version",
|
||||
"v1.1": "优化字幕翻译逻辑,优化日志输出"
|
||||
"v1.1": "优化字幕翻译逻辑,优化日志输出",
|
||||
"v1.2": "fix openai_proxy打开时,翻译失败的问题,优化日志输出"
|
||||
}
|
||||
},
|
||||
"CustomSites": {
|
||||
|
||||
@@ -28,6 +28,10 @@ from app.schemas.types import NotificationType
|
||||
# todo
|
||||
# 监听入库事件,自动调用翻译
|
||||
|
||||
class UserInterruptException(Exception):
|
||||
"""用户中断当前任务的异常"""
|
||||
pass
|
||||
|
||||
class AutoSubv2(_PluginBase):
|
||||
# 插件名称
|
||||
plugin_name = "AI字幕自动生成(v2)"
|
||||
@@ -38,7 +42,7 @@ class AutoSubv2(_PluginBase):
|
||||
# 主题色
|
||||
plugin_color = "#2C4F7E"
|
||||
# 插件版本
|
||||
plugin_version = "1.1"
|
||||
plugin_version = "1.2"
|
||||
# 插件作者
|
||||
plugin_author = "TimoYoung"
|
||||
# 作者主页
|
||||
@@ -199,6 +203,7 @@ class AutoSubv2(_PluginBase):
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"处理异常: {e}")
|
||||
logger.error(traceback.format_exc())
|
||||
finally:
|
||||
logger.info(f"处理完成: "
|
||||
f"成功{self.success_count} / 跳过{self.skip_count} / 失败{self.fail_count} / 共{self.process_count}")
|
||||
@@ -208,7 +213,7 @@ class AutoSubv2(_PluginBase):
|
||||
if self.asr_engine == 'whisper.cpp':
|
||||
if not self.whisper_main or not self.whisper_model:
|
||||
logger.warn(f"配置信息不完整,不进行处理")
|
||||
return
|
||||
return False
|
||||
if not os.path.exists(self.whisper_main):
|
||||
logger.warn(f"whisper.cpp主程序不存在,不进行处理")
|
||||
return False
|
||||
@@ -222,7 +227,7 @@ class AutoSubv2(_PluginBase):
|
||||
elif self.asr_engine == 'faster-whisper':
|
||||
if not self.faster_whisper_model_path or not self.faster_whisper_model:
|
||||
logger.warn(f"配置信息不完整,不进行处理")
|
||||
return
|
||||
return False
|
||||
if not os.path.exists(self.faster_whisper_model_path):
|
||||
logger.info(f"创建faster-whisper模型目录:{self.faster_whisper_model_path}")
|
||||
os.mkdir(self.faster_whisper_model_path)
|
||||
@@ -287,6 +292,9 @@ class AutoSubv2(_PluginBase):
|
||||
if self.send_notify:
|
||||
self.post_message(mtype=NotificationType.Plugin, title="【自动字幕生成】", text=message)
|
||||
self.success_count += 1
|
||||
except UserInterruptException:
|
||||
logger.info(f"用户中断当前任务:{video_file}")
|
||||
self.fail_count += 1
|
||||
except Exception as e:
|
||||
logger.error(f"自动字幕生成 处理异常:{e}")
|
||||
end_time = time.time()
|
||||
@@ -364,7 +372,7 @@ class AutoSubv2(_PluginBase):
|
||||
for segment in segments:
|
||||
if self._event.is_set():
|
||||
logger.info(f"whisper音轨转录服务停止")
|
||||
raise Exception(f"用户中断当前任务")
|
||||
raise UserInterruptException(f"用户中断当前任务")
|
||||
for word in segment.words:
|
||||
idx += 1
|
||||
subs.append(srt.Subtitle(index=idx,
|
||||
@@ -376,7 +384,7 @@ class AutoSubv2(_PluginBase):
|
||||
for i, segment in enumerate(segments):
|
||||
if self._event.is_set():
|
||||
logger.info(f"whisper音轨转录服务停止")
|
||||
raise Exception(f"用户中断当前任务")
|
||||
raise UserInterruptException(f"用户中断当前任务")
|
||||
subs.append(srt.Subtitle(index=i,
|
||||
start=timedelta(seconds=segment.start),
|
||||
end=timedelta(seconds=segment.end),
|
||||
@@ -749,9 +757,8 @@ class AutoSubv2(_PluginBase):
|
||||
context = self.__get_context(all_subs, indices, is_batch=True) if self.context_window > 0 else None
|
||||
batch_text = '\n'.join([item.content for item in batch])
|
||||
|
||||
openai = OpenAi(self._openai_key, self._openai_url, self._openai_proxy, self._openai_model)
|
||||
try:
|
||||
ret, result = openai.translate_to_zh(batch_text, context)
|
||||
ret, result = self.openai.translate_to_zh(batch_text, context)
|
||||
if not ret:
|
||||
raise Exception(result)
|
||||
|
||||
@@ -770,11 +777,10 @@ class AutoSubv2(_PluginBase):
|
||||
|
||||
def __process_single(self, all_subs: List[srt.Subtitle], item: srt.Subtitle) -> srt.Subtitle:
|
||||
"""单条处理逻辑"""
|
||||
openai = OpenAi(self._openai_key, self._openai_url, self._openai_proxy, self._openai_model)
|
||||
for _ in range(self.max_retries):
|
||||
idx = all_subs.index(item)
|
||||
context = self.__get_context(all_subs, [idx], is_batch=False) if self.context_window > 0 else None
|
||||
success, trans = openai.translate_to_zh(item.content, context)
|
||||
success, trans = self.openai.translate_to_zh(item.content, context)
|
||||
|
||||
if success:
|
||||
item.content = f"{trans}\n{item.content}"
|
||||
@@ -801,7 +807,7 @@ class AutoSubv2(_PluginBase):
|
||||
for item in valid_subs:
|
||||
if self._event.is_set():
|
||||
logger.info(f"字幕{source_subtitle}翻译停止")
|
||||
raise Exception(f"用户中断当前任务")
|
||||
raise UserInterruptException(f"用户中断当前任务")
|
||||
current_batch.append(item)
|
||||
|
||||
if len(current_batch) >= self.batch_size:
|
||||
|
||||
Reference in New Issue
Block a user