mirror of
https://github.com/linyqh/NarratoAI.git
synced 2025-12-11 10:32:49 +00:00
fix(字幕): 修复字幕开关功能无效的问题
添加subtitle_enabled参数控制字幕处理逻辑,当禁用时跳过字幕处理 修复字幕文件不存在时的日志级别为warning
This commit is contained in:
parent
1792311ef4
commit
06148a6b8c
@ -59,6 +59,7 @@ def merge_materials(
|
||||
- stroke_width: 描边宽度,默认1
|
||||
- threads: 处理线程数,默认2
|
||||
- fps: 输出帧率,默认30
|
||||
- subtitle_enabled: 是否启用字幕,默认True
|
||||
|
||||
返回:
|
||||
输出视频的路径
|
||||
@ -83,13 +84,17 @@ def merge_materials(
|
||||
stroke_width = options.get('stroke_width', 1)
|
||||
threads = options.get('threads', 2)
|
||||
fps = options.get('fps', 30)
|
||||
subtitle_enabled = options.get('subtitle_enabled', True)
|
||||
|
||||
# 音量配置日志 - 便于调试音量问题
|
||||
# 配置日志 - 便于调试问题
|
||||
logger.info(f"音量配置详情:")
|
||||
logger.info(f" - 配音音量: {voice_volume}")
|
||||
logger.info(f" - 背景音乐音量: {bgm_volume}")
|
||||
logger.info(f" - 原声音量: {original_audio_volume}")
|
||||
logger.info(f" - 是否保留原声: {keep_original_audio}")
|
||||
logger.info(f"字幕配置详情:")
|
||||
logger.info(f" - 是否启用字幕: {subtitle_enabled}")
|
||||
logger.info(f" - 字幕文件路径: {subtitle_path}")
|
||||
|
||||
# 音量参数验证
|
||||
def validate_volume(volume, name):
|
||||
@ -266,27 +271,34 @@ def merge_materials(
|
||||
color=subtitle_color,
|
||||
)
|
||||
|
||||
# 处理字幕
|
||||
if subtitle_path and os.path.exists(subtitle_path):
|
||||
# 处理字幕 - 修复字幕开关bug
|
||||
if subtitle_enabled and subtitle_path and os.path.exists(subtitle_path):
|
||||
logger.info("字幕已启用,开始处理字幕文件")
|
||||
try:
|
||||
# 加载字幕文件
|
||||
sub = SubtitlesClip(
|
||||
subtitles=subtitle_path,
|
||||
encoding="utf-8",
|
||||
subtitles=subtitle_path,
|
||||
encoding="utf-8",
|
||||
make_textclip=make_textclip
|
||||
)
|
||||
|
||||
|
||||
# 创建每个字幕片段
|
||||
text_clips = []
|
||||
for item in sub.subtitles:
|
||||
clip = create_text_clip(subtitle_item=item)
|
||||
text_clips.append(clip)
|
||||
|
||||
|
||||
# 合成视频和字幕
|
||||
video_clip = CompositeVideoClip([video_clip, *text_clips])
|
||||
logger.info(f"已添加{len(text_clips)}个字幕片段")
|
||||
except Exception as e:
|
||||
logger.error(f"处理字幕失败: \n{traceback.format_exc()}")
|
||||
elif not subtitle_enabled:
|
||||
logger.info("字幕已禁用,跳过字幕处理")
|
||||
elif not subtitle_path:
|
||||
logger.info("未提供字幕文件路径,跳过字幕处理")
|
||||
elif not os.path.exists(subtitle_path):
|
||||
logger.warning(f"字幕文件不存在: {subtitle_path},跳过字幕处理")
|
||||
|
||||
# 导出最终视频
|
||||
try:
|
||||
@ -392,6 +404,7 @@ if __name__ == '__main__':
|
||||
'bgm_volume': 0.1, # 背景音乐音量
|
||||
'original_audio_volume': 1.0, # 视频原声音量,0表示不保留
|
||||
'keep_original_audio': True, # 是否保留原声
|
||||
'subtitle_enabled': True, # 是否启用字幕 - 修复字幕开关bug
|
||||
'subtitle_font': 'MicrosoftYaHeiNormal.ttc', # 这里使用相对字体路径,会自动在 font_dir() 目录下查找
|
||||
'subtitle_font_size': 40,
|
||||
'subtitle_color': '#FFFFFF',
|
||||
|
||||
@ -315,6 +315,7 @@ def start_subclip(task_id: str, params: VideoClipParams, subclip_path_videos: di
|
||||
'bgm_volume': params.bgm_volume, # 背景音乐音量
|
||||
'original_audio_volume': params.original_volume, # 视频原声音量,0表示不保留
|
||||
'keep_original_audio': True, # 是否保留原声
|
||||
'subtitle_enabled': params.subtitle_enabled, # 是否启用字幕 - 修复字幕开关bug
|
||||
'subtitle_font': params.font_name, # 这里使用相对字体路径,会自动在 font_dir() 目录下查找
|
||||
'subtitle_font_size': params.font_size,
|
||||
'subtitle_color': params.text_fore_color,
|
||||
|
||||
@ -205,7 +205,8 @@ def generate_video_v3(
|
||||
bgm_path: Optional[str] = None,
|
||||
narration_path: Optional[str] = None,
|
||||
output_path: str = "output.mp4",
|
||||
font_path: Optional[str] = None
|
||||
font_path: Optional[str] = None,
|
||||
subtitle_enabled: bool = True
|
||||
) -> None:
|
||||
"""
|
||||
合并视频素材,包括视频、字幕、BGM和解说音频
|
||||
@ -220,6 +221,7 @@ def generate_video_v3(
|
||||
- original: 原声音量(0-1),默认1.0
|
||||
- bgm: BGM音量(0-1),默认0.3
|
||||
- narration: 解说音量(0-1),默认1.0
|
||||
subtitle_enabled: 是否启用字幕,默认True
|
||||
subtitle_style: 字幕样式配置字典,可包含以下键:
|
||||
- font: 字体名称
|
||||
- fontsize: 字体大小
|
||||
@ -239,8 +241,8 @@ def generate_video_v3(
|
||||
video = VideoFileClip(video_path)
|
||||
subtitle_clips = []
|
||||
|
||||
# 处理字幕(如果提供)
|
||||
if subtitle_path:
|
||||
# 处理字幕(如果启用且提供)- 修复字幕开关bug
|
||||
if subtitle_enabled and subtitle_path:
|
||||
if os.path.exists(subtitle_path):
|
||||
# 检查字体文件
|
||||
if font_path and not os.path.exists(font_path):
|
||||
@ -308,7 +310,11 @@ def generate_video_v3(
|
||||
except Exception as e:
|
||||
logger.info(f"警告:处理字幕文件时出错: {str(e)}")
|
||||
else:
|
||||
logger.info(f"提示:字幕文件不存在: {subtitle_path}")
|
||||
logger.warning(f"字幕文件不存在: {subtitle_path}")
|
||||
elif not subtitle_enabled:
|
||||
logger.info("字幕已禁用,跳过字幕处理")
|
||||
elif not subtitle_path:
|
||||
logger.info("未提供字幕文件路径,跳过字幕处理")
|
||||
|
||||
# 合并音频
|
||||
audio_clips = []
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user