优化用户中断任务时的日志打印机制

This commit is contained in:
TimoYoung
2025-04-22 15:00:20 +08:00
parent 5353deed64
commit 5cf8f9fda9

View File

@@ -28,6 +28,10 @@ from app.schemas.types import NotificationType
# todo
# 监听入库事件,自动调用翻译
class UserInterruptException(Exception):
"""用户中断当前任务的异常"""
pass
class AutoSubv2(_PluginBase):
# 插件名称
plugin_name = "AI字幕自动生成(v2)"
@@ -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),
@@ -799,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: