diff --git a/webui/components/script_settings.py b/webui/components/script_settings.py index 4dadb3b..a97e53a 100644 --- a/webui/components/script_settings.py +++ b/webui/components/script_settings.py @@ -55,6 +55,11 @@ def render_script_file(tr, params): MODE_SHORT = "short" MODE_SUMMARY = "summary" + # 处理保存脚本后的模式切换(必须在 widget 实例化之前) + if st.session_state.get('_switch_to_file_mode'): + st.session_state['script_mode_selection'] = tr("Select/Upload Script") + del st.session_state['_switch_to_file_mode'] + # 模式选项映射 mode_options = { tr("Select/Upload Script"): MODE_FILE, @@ -148,6 +153,10 @@ def render_script_file(tr, params): selected_index = i break + # 如果找到了保存的脚本,同步更新 selectbox 的 key 状态 + if saved_script_path and selected_index > 0: + st.session_state['script_file_selection'] = selected_index + selected_script_index = st.selectbox( tr("Script Files"), index=selected_index, @@ -157,8 +166,14 @@ def render_script_file(tr, params): ) script_path = script_list[selected_script_index][1] - st.session_state['video_clip_json_path'] = script_path - params.video_clip_json_path = script_path + # 只有当用户实际选择了脚本时才更新路径,避免覆盖已保存的路径 + if script_path: + st.session_state['video_clip_json_path'] = script_path + params.video_clip_json_path = script_path + elif saved_script_path: + # 如果用户选择了 "None" 但之前有保存的脚本,保持原有路径 + st.session_state['video_clip_json_path'] = saved_script_path + params.video_clip_json_path = saved_script_path # 处理脚本上传 if script_path == "upload_script": @@ -482,6 +497,9 @@ def save_script_with_validation(tr, video_clip_json_details): json.dump(data, file, ensure_ascii=False, indent=4) st.session_state['video_clip_json'] = data st.session_state['video_clip_json_path'] = save_path + + # 标记需要切换到文件选择模式(在下次渲染前处理) + st.session_state['_switch_to_file_mode'] = True # 更新配置 config.app["video_clip_json_path"] = save_path diff --git a/webui/tools/generate_script_short.py b/webui/tools/generate_script_short.py index feef634..78fd1ee 100644 --- a/webui/tools/generate_script_short.py +++ b/webui/tools/generate_script_short.py @@ -45,11 +45,18 @@ def generate_script_short(tr, params, custom_clips=5): update_progress(20, "开始准备生成脚本") - srt_path = params.video_origin_path.replace(".mp4", ".srt").replace("videos", "srt").replace("video", "subtitle") - if not os.path.exists(srt_path): - logger.error(f"{srt_path} 文件不存在请检查或重新转录") - st.error(f"{srt_path} 文件不存在请检查或重新转录") - st.stop() + # 优先使用用户上传的字幕文件 + uploaded_subtitle = st.session_state.get('subtitle_path') + if uploaded_subtitle and os.path.exists(uploaded_subtitle): + srt_path = uploaded_subtitle + logger.info(f"使用用户上传的字幕文件: {srt_path}") + else: + # 回退到根据视频路径自动推断 + srt_path = params.video_origin_path.replace(".mp4", ".srt").replace("videos", "srt").replace("video", "subtitle") + if not os.path.exists(srt_path): + logger.error(f"{srt_path} 文件不存在请检查或重新转录") + st.error(f"{srt_path} 文件不存在,请上传字幕文件或重新转录") + st.stop() api_params = { "vision_provider": vision_llm_provider,