feat: ASS 字幕格式支持#558
Open
rrrrrede1 wants to merge 8 commits into
Open
Conversation
- Add core/utils/ass_utils.py for ASS file generation (PlayRes absolute/relative modes, style config driven) - config.yaml: add subtitle.format and subtitle.ass_style (source/translation fonts, colors, borders, alignment) - _6_gen_sub: generate ASS files alongside SRT when format=ass - _7_sub_into_vid: remove hardcoded styles, read from config; ASS mode burns two .ass files, SRT mode uses config-driven force_style with platform font fallback - _12_dub_to_vid: same pattern, generate dub.ass on-the-fly - sidebar_setting: add Subtitle Format selector and ASS Style Settings expander with all style controls - imports_and_utils: include .ass files in subtitle download zip
_build_style_line had severely misaligned field output vs ASS V4+ Format (23 fields). Fields from Bold onward were shifted, causing Shadow depth to be swallowed and later fields (Alignment, MarginV) in wrong positions. Added missing fields: SecondaryColour, BackColour for source, Bold, Italic, Shadow depth, MarginL, MarginR. Updated config, UI, defaults, and translations accordingly.
…support - Add parse_ass_style_line() to parse any ASS Style line into config dict - Add import UI in sidebar: text_area + radio (source/translation/both) - Change outline_width and shadow to float for fractional values (e.g. 3.1) - Add translation keys for import feature (en + zh-CN)
- Remove ASS_AUDIO_OUTPUT_CONFIGS and its generation in _6_gen_sub (dubbing pipeline only consumes SRT, audio ASS files were never used)
…tions BorderStyle and Alignment selectboxes now dynamically include the current config value if it's not in the default preset list, so imported values (e.g. BorderStyle=4 from paste) are never silently overwritten by fallback.
…nodes - Add subtitle.srt_style config node for SRT force_style parameters (FontName, FontSize, PrimaryColour, OutlineColour, OutlineWidth, ShadowColour/BackColour, BorderStyle, Alignment, MarginV/L/R) - Keep subtitle.ass_style for full V4+ Style attributes independently - _build_srt_force_style now reads from subtitle.srt_style instead of subtitle.ass_style - _build_dub_force_style now reads from subtitle.srt_style.translation - Add SRT Style Settings UI panel in sidebar (simplified editor with only force_style-supported fields) - Add translations for SRT style UI strings - Regenerate clean config.yaml from config_bak.yaml template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
在原有 SRT 字幕流程基础上,新增 ASS (Advanced SubStation Alpha) 字幕格式支持。ASS 格式提供远超 SRT force_style 的样式控制能力,包括独立字体、颜色、描边、阴影、对齐等完整 ASS V4+ Style 属性,以及一键粘贴导入外部样式行。同时将 SRT 和 ASS 的样式配置分离为独立的配置节点,各自拥有专属的 UI 编辑器。
提交列表(按时间顺序):
1726467feat: add ASS subtitle format support with configurable styles5563102fix: add missing zh-CN translations for ASS subtitle UI strings6993565fix: correct ASS V4+ Style field order and add missing style fieldsb84cea2feat: add ASS style import from pasted line and float outline/shadow supportac38dd9refactor: remove unused audio ASS output and import59d6cabfix: prevent selectbox data loss on imported values outside preset options78bc07frefactor: separate SRT and ASS style configs into independent config nodes1259e49feat: add PlayRes conditional comment in config and separate SRT/ASS style configs新增功能
1. 字幕格式选择
config.yaml新增subtitle.format字段,取值srt或ass(默认srt)。Streamlit 侧边栏 Subtitles Settings 区域新增下拉框,切换后立即生效并 rerun。
2. SRT/ASS 样式配置分离
SRT 和 ASS 的样式配置分为两个独立节点:
subtitle.srt_style:SRT force_style 参数,仅包含 ffmpeg force_style 支持的字段子集subtitle.ass_style:ASS V4+ Style 完整属性,包含所有 23 字段两者拥有不同的默认值(如 SRT source fontsize=15 vs ASS source fontsize=17)。
3. ASS 文件生成(
_6_gen_sub)当
subtitle.format为ass时,步骤 6 在生成 SRT 的同时额外生成:output/src.assoutput/trans.assoutput/src_trans.ass\N分隔)output/trans_src.assSRT 文件始终生成,不受格式选择影响。配音用 SRT(
audio/src_subs_for_audio.srt和audio/trans_subs_for_audio.srt)也始终生成。4. 烧录模式切换(
_7_sub_into_vid)srtsubtitles=xxx.srt:force_style='...',force_style 参数从subtitle.srt_style读取asssubtitles=xxx.ass,样式由 .ass 文件内的 Style 行定义ASS 模式下不再走 force_style,所有样式由 ASS 文件中的 Style 行控制。
5. 配音烧录(
_12_dub_to_vid)subtitle.srt_style.translation构建 force_style,烧录dub.srt_generate_dub_ass()实时生成dub.ass,烧录到配音视频中_generate_dub_ass从_8_1_AUDIO_TASK的new_sub_times和lines数据构建 DataFrame,再调用generate_ass生成,与_6生成的显示用 ASS 文件完全独立。6. 字幕下载 ZIP
下载 ZIP 时同时包含
.srt和.ass文件(若存在)。7. 样式配置(
config.yaml)缩放模式
absoluterelative8. Streamlit 样式编辑器
侧边栏根据格式选择条件显示不同的样式面板:
SRT 模式 → 显示 SRT Style Settings 折叠面板:
ASS 模式 → 显示 ASS Style Settings 折叠面板:
9. 一键粘贴导入
在 ASS Style Settings 顶部新增 "Import ASS Style" 折叠面板:
解析函数
parse_ass_style_line()支持:Style:前缀或不带-1值自动转为13.1)和 Shadow(如1.2)54.0形式10. 完整 ASS V4+ Style 字段对齐
原始实现中
_build_style_line的字段顺序与 ASS Format 严重错位(从 Bold 开始全部偏移)。现已修正为 23 字段标准输出:新增了之前缺失的字段:SecondaryColour、BackColour(source)、Bold、Italic、Shadow(深度值,非颜色)、MarginL、MarginR。
已知局限
不支持从 UI 编辑的字段
以下 ASS Style 字段在 UI 和 config 中不可编辑,使用硬编码默认值:
这些字段可以通过一键导入粘贴来设置(解析后存入 config),但不会在 UI 中单独暴露,因为对字幕场景几乎无用。一键导入后 ScaleX/ScaleY/Spacing/Angle 不会被保留(不在解析字段中),固定使用默认值。
Selectbox 动态选项
BorderStyle 和 Alignment 的 selectbox 选项是动态的:预设选项为基础(BorderStyle: 1/3/4,Alignment: 1-9),但当 config 中的值不在预设列表中时,该值会被自动追加到选项中,确保导入的值不会因 UI 下拉框不包含而丢失。
shadow_color字段为冗余配置ASS V4+ 格式中没有
ShadowColour字段(ASS 只有数值类型的Shadow深度)。ass_style.source.shadow_color目前存在于 config 中但不被任何代码路径使用——ASS 模式下不写入 Style 行的 ShadowColour,SRT 模式下从srt_style读取 ShadowColour。此字段为遗留冗余,未来可考虑移除。字体可用性
ASS 文件中引用的字体名必须在运行 ffmpeg 的系统上已安装。Linux 默认回退到
NotoSansCJK-Regular,macOS 回退到Arial Unicode MS。如果字体不可用,字幕会显示方框。双语字幕的 Style 分配
src_trans.ass中双语行使用Source的 Style(第一个 column 的 Style),两部分文本用\N连接。这意味着源语言和译文在同一 Style 下渲染,无法在双语行中分别控制源/译的字体和颜色——这是 ASS 格式本身的限制(一个 Dialogue 行只能指定一个 Style)。SRT force_style 的局限
SRT 模式下通过 ffmpeg
force_style参数注入样式,但 force_style 只支持有限的 ASS 属性子集(FontName、FontSize、PrimaryColour、OutlineColour、OutlineWidth、ShadowColour/BackColour、BorderStyle、Alignment、MarginV/L/R)。这些参数可通过srt_style配置和 SRT Style 编辑器修改,并非固定写死;但受限于 ffmpeg force_style 本身支持的属性范围,不如 ASS 模式灵活。模式切换后的遗留文件
若先以 ASS 格式运行一遍流水线(
_6_gen_sub生成src.ass、trans.ass、src_trans.ass、trans_src.ass,_12可能生成dub.ass),再将subtitle.format切回 SRT 重新运行,这些.ass文件既不会被删除也不会被覆盖——因为 SRT 模式下的_6和_12不触碰任何.ass文件。这些遗留文件不影响 SRT 流程的运行,但如果需要纯净输出,需手动清理output/下的.ass文件。配音流程仅使用 SRT
步骤 8-11 的配音流程内部使用 SRT 文件做音频对齐,不受
subtitle.format影响。_6_gen_sub不会生成audio/*_for_audio.ass文件(配音不需要 ASS)。ASS 文件仅在烧录(_7和_12)时使用。为什么配音依赖 SRT 不会导致音画不同步
整个流水线中,决定时间轴的数据源只有一个:
_2_asr产出的词级时间戳(cleaned_chunks.xlsx)。无论 SRT 还是 ASS,时间戳都由同一个align_timestamp()函数生成,来自同一组(start, end)数据。配音流程的时间轴链路:
关键点:
_8_1解析 SRTaudio/trans_subs_for_audio.srt_8_2匹配文本trans.srt+src.srt_11输出dub.srtnew_sub_times(配音速度调整后的实际时间)_12生成dub.assnew_sub_times实时转换_6生成的 ASS 无关_8_1解析的是audio/trans_subs_for_audio.srt,这是_6_gen_sub始终生成的文件_8_2匹配的trans.srt和src.srt同样始终存在_11输出的dub.srt时间戳来自new_sub_times(经过配音速度调整后的实际时间),与任何字幕格式无关_12的dub.ass是在烧录时从_8_1_AUDIO_TASK的new_sub_times实时转换的(_generate_dub_ass),时间戳直接从配音数据取值,和_6生成的显示用 ASS 文件完全独立因此,配音音频与字幕的时间轴始终一致,不会因字幕格式选择而产生音画不同步。
一键导入不覆盖的字段
一键导入只写入解析出来的字段。以下字段不会被导入写入(因为不属于 ASS Style 行),保留 config 中的原值:
shadow_color(冗余字段,ASS 不使用,SRT 从srt_style读取)scale_mode、play_res_x、play_res_y(ASS 全局属性,不属于 Style 行)修改文件清单
config.yamlsubtitle.format、subtitle.srt_style和subtitle.ass_style完整配置core/utils/ass_utils.pygenerate_ass、parse_ass_style_line、_build_style_line、_get_default_style、时间转换函数core/_6_gen_sub.pycore/_7_sub_into_vid.pysrt_style配置读取,新增_platform_fontname和_build_srt_force_stylecore/_12_dub_to_vid.pydub.ass(_generate_dub_ass),SRT force_style 从srt_style读取,配音烧录双模式core/st_utils/sidebar_setting.pycore/st_utils/imports_and_utils.py.srt和.ass文件core/utils/models.pytranslations/en.jsontranslations/zh-CN.json测试清单
在 Window 10 的 WSL Fedora Linux 43 中启动 Streamlit 进行测试: