Compare commits

...

3 Commits

Author SHA1 Message Date
truewhile f4abdf78e2 优化在线字幕 2026-09-17 23:53:22 +08:00
truewhile ae8db26c1e bug处理 2026-09-17 23:20:58 +08:00
truewhile 8017e994f4 优化 2026-09-17 22:57:41 +08:00
7 changed files with 392 additions and 50 deletions
+38 -9
View File
@@ -410,14 +410,10 @@ func (s *SubtitleService) Serve(ctx context.Context, mediaID, sub string, w io.W
}
return s.serveEmbedded(ctx, m, index, w)
}
abs, err := filepath.Abs(sub)
abs, err := readExternalSubtitlePath(m, sub)
if err != nil {
return err
}
mediaDir, _ := filepath.Abs(filepath.Dir(m.Path))
if !pathWithin(abs, mediaDir) {
return fmt.Errorf("path escape")
}
f, err := os.Open(abs) // #nosec G304 -- abs is constrained to the media file directory with pathWithin.
if err != nil {
@@ -428,14 +424,17 @@ func (s *SubtitleService) Serve(ctx context.Context, mediaID, sub string, w io.W
if err != nil {
return err
}
// 非 UTF-8 的外挂字幕(UTF-16、GBK/Big5 等)必须先归一化:浏览器只能按
// UTF-8 解析 <track> 内容,否则整篇都会变成替换字符。
text := decodeSubtitleText(body)
switch strings.ToLower(filepath.Ext(abs)) {
case ".vtt":
_, err = w.Write(body)
_, err = io.WriteString(w, text)
case ".srt":
_, err = w.Write([]byte(srtToVTT(string(body))))
_, err = io.WriteString(w, srtToVTT(text))
case ".ass", ".ssa":
_, err = w.Write([]byte(assToVTT(string(body))))
_, err = io.WriteString(w, assToVTT(text))
default:
return errors.New("unsupported subtitle format")
}
@@ -517,12 +516,42 @@ func (s *SubtitleService) ServeASS(ctx context.Context, mediaID, sub string, w i
}
switch strings.ToLower(filepath.Ext(sub)) {
case ".ass", ".ssa":
return s.ServeRaw(ctx, mediaID, sub, w)
// 外挂 ASS 同样要归一化成 UTF-8:libass 只认 UTF-8,UTF-16/GBK 的
// 字幕交给它会解析不到任何事件,表现为「字幕选中了却不显示」。
abs, err := readExternalSubtitlePath(m, sub)
if err != nil {
return err
}
f, err := os.Open(abs) // #nosec G304 -- abs is constrained to the media file directory with pathWithin.
if err != nil {
return err
}
defer f.Close()
body, err := io.ReadAll(f)
if err != nil {
return err
}
_, err = io.WriteString(w, decodeSubtitleText(body))
return err
default:
return errors.New("subtitle is not ASS/SSA")
}
}
// readExternalSubtitlePath 校验外挂字幕路径(必须落在媒体文件所在目录内)并返回
// 绝对路径。Serve 与 ServeASS 共用,避免两处各自实现出现安全口径不一致。
func readExternalSubtitlePath(m *model.Media, sub string) (string, error) {
abs, err := filepath.Abs(sub)
if err != nil {
return "", err
}
mediaDir, _ := filepath.Abs(filepath.Dir(m.Path))
if !pathWithin(abs, mediaDir) {
return "", fmt.Errorf("path escape")
}
return abs, nil
}
func (s *SubtitleService) serveEmbeddedASS(ctx context.Context, media *model.Media, streamIndex int, w io.Writer) error {
input, err := s.resolveInput(ctx, media)
if err != nil {
+167
View File
@@ -0,0 +1,167 @@
package service
import (
"bytes"
"encoding/binary"
"math"
"regexp"
"strings"
"unicode"
"unicode/utf16"
"unicode/utf8"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/japanese"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/encoding/traditionalchinese"
)
// 外挂字幕文件的编码归一化。
//
// 字幕文件由字幕组/工具生成,编码并不统一:除 UTF-8 外,中文圈常见 GBK/GB18030,
// 港台常见 Big5,部分 Windows 工具(Aegisub 早期版本、Subtitle Edit 的某些导出)
// 会写成带 BOM 的 UTF-16LE。浏览器 <track> 与 libass(JASSUB)都只按 UTF-8 解析,
// 后端如果原样下发字节,非 UTF-8 的字幕会整篇变成替换字符——表现就是「字幕轨已经
// 选中,但画面上一个字都不显示」。所以外挂字幕在服务端就要统一解码成 UTF-8。
//
// 注意:只用于下发给浏览器/播放器的文本路径。Emby 兼容层的 /subtitles 原始字节
// 接口(ServeRaw)必须保持字节一致,不能经过这里。
// 各编码的 BOM 前缀。
var (
utf8BOM = []byte{0xEF, 0xBB, 0xBF}
utf16BEBOM = []byte{0xFE, 0xFF}
utf16LEBOM = []byte{0xFF, 0xFE}
)
// subtitleLegacyEncodings 是无 BOM 时的候选编码,按优先级排列。
// GB18030 是 GBK/GB2312 的超集,中文外挂字幕绝大多数是它,因此排在最前——
// 多个候选都能解出合法字幕时,靠前的优先。
var subtitleLegacyEncodings = []struct {
name string
enc encoding.Encoding
}{
{"gb18030", simplifiedchinese.GB18030},
{"big5", traditionalchinese.Big5},
{"shift_jis", japanese.ShiftJIS},
{"euc-kr", korean.EUCKR},
}
// subtitleTimecodePattern 匹配 SRT(00:00:01,000 --> ...)与 WebVTT
// (00:00:01.000 --> ...)的时间行。
var subtitleTimecodePattern = regexp.MustCompile(`\d{1,2}:\d{2}:\d{2}[,.]\d{1,3}\s*-->`)
// decodeSubtitleText 把字幕文件字节归一化成 UTF-8 文本。识别不出编码时原样返回,
// 保证不会比「直接透传」更糟。
func decodeSubtitleText(raw []byte) string {
switch {
case bytes.HasPrefix(raw, utf8BOM):
return string(raw[len(utf8BOM):])
case bytes.HasPrefix(raw, utf16LEBOM):
return decodeUTF16(raw[len(utf16LEBOM):], false)
case bytes.HasPrefix(raw, utf16BEBOM):
return decodeUTF16(raw[len(utf16BEBOM):], true)
}
// 无 BOM 且是合法 UTF-8:这是绝大多数情况,直接返回。
if utf8.Valid(raw) {
return string(raw)
}
best := ""
bestScore := math.MinInt
consider := func(text string) {
if !utf8.ValidString(text) || !looksLikeSubtitle(text) {
return
}
score := subtitlePlausibility(text)
if score > bestScore {
best, bestScore = text, score
}
}
// 无 BOM 的 UTF-16 少见(多来自手工编辑),但一旦是它,所有单字节编码都会
// 解出带大量 NUL 的垃圾,因此先用「格式特征」筛掉这些候选。
consider(decodeUTF16(raw, false))
consider(decodeUTF16(raw, true))
for _, candidate := range subtitleLegacyEncodings {
text, err := candidate.enc.NewDecoder().Bytes(raw)
if err != nil {
continue
}
consider(string(text))
}
if best != "" {
return best
}
return string(raw)
}
// decodeUTF16 按给定字节序把 UTF-16 字节解码为字符串(含代理对)。
func decodeUTF16(raw []byte, bigEndian bool) string {
units := make([]uint16, 0, len(raw)/2)
for i := 0; i+1 < len(raw); i += 2 {
if bigEndian {
units = append(units, binary.BigEndian.Uint16(raw[i:i+2]))
} else {
units = append(units, binary.LittleEndian.Uint16(raw[i:i+2]))
}
}
return string(utf16.Decode(units))
}
// looksLikeSubtitle 判断解码结果是否真的像一份字幕。解码器「没报错」并不代表
// 解对了——很多编码能把任意字节解成合法文本,只有格式特征才能确认。
func looksLikeSubtitle(text string) bool {
head := text
if len(head) > 4096 {
head = head[:4096]
}
if strings.Contains(head, "[Script Info]") || strings.Contains(head, "Dialogue:") {
return true // ASS/SSA
}
if strings.HasPrefix(strings.TrimSpace(head), "WEBVTT") {
return true
}
return subtitleTimecodePattern.MatchString(head) // SRT / WebVTT 时间行
}
// subtitlePlausibility 给解码结果打分,返回 -100..100 的可信度:正常文本字符加分,
// 替换字符(解码失败)、控制字符、未分配码位扣分。
//
// 刻意用比例而不是字符数量:单字节回退会把一个双字节字符拆成两个字符,字符多的
// 候选看起来「更正常」,实际是解错了——GBK 中文字幕被当成 Shift_JIS 解,就会得到
// 一串半角片假名,数量比正确结果还多。半角片假名单独扣分正是为了压住这种情况。
func subtitlePlausibility(text string) int {
total := 0
weight := 0
for _, r := range text {
total++
switch {
case r == utf8.RuneError:
weight -= 8
case r == '\n' || r == '\r' || r == '\t':
// 正常换行不计分也不扣分。
case r < 0x20 || r == 0x7F:
weight -= 8
case unicode.Is(unicode.Co, r) || unicode.Is(unicode.Cn, r):
weight -= 4
case r >= 0xFF61 && r <= 0xFF9F:
// 半角片假名:GBK 汉字被按 Shift_JIS 解出来的典型产物。
weight -= 2
case unicode.In(r, unicode.Han, unicode.Hiragana, unicode.Katakana, unicode.Hangul),
unicode.Is(unicode.Latin, r), unicode.IsDigit(r), unicode.IsPunct(r):
weight++
case unicode.IsSpace(r):
// 空格(含全角空格)不参与打分。
default:
weight--
}
}
if total == 0 {
return 0
}
return weight * 100 / total
}
+169
View File
@@ -0,0 +1,169 @@
package service
import (
"bytes"
"encoding/binary"
"os"
"path/filepath"
"strings"
"testing"
"unicode/utf16"
"github.com/glebarez/sqlite"
"go.uber.org/zap"
"golang.org/x/text/encoding/simplifiedchinese"
"gorm.io/gorm"
"github.com/truewhile/MeBox/internal/config"
"github.com/truewhile/MeBox/internal/model"
"github.com/truewhile/MeBox/internal/repository"
)
const assFixture = "[Script Info]\n" +
"Title:Railgun 01 BD\n" +
"ScriptType:v4.00+\n" +
"\n" +
"[Events]\n" +
"Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n" +
"Dialogue: 0,0:00:01.00,0:00:02.00,Default,,0,0,0,,放て!心に刻んだ夢を\n"
const srtFixture = "1\n" +
"00:00:01,000 --> 00:00:02,000\n" +
"只有我的超电磁炮\n" +
"\n"
func encodeUTF16(s string, bigEndian bool, withBOM bool) []byte {
units := utf16.Encode([]rune(s))
out := make([]byte, 0, len(units)*2+2)
if withBOM {
if bigEndian {
out = append(out, 0xFE, 0xFF)
} else {
out = append(out, 0xFF, 0xFE)
}
}
buf := make([]byte, 2)
for _, unit := range units {
if bigEndian {
binary.BigEndian.PutUint16(buf, unit)
} else {
binary.LittleEndian.PutUint16(buf, unit)
}
out = append(out, buf...)
}
return out
}
func encodeGBK(t *testing.T, s string) []byte {
t.Helper()
encoded, err := simplifiedchinese.GBK.NewEncoder().Bytes([]byte(s))
if err != nil {
t.Fatalf("encode GBK: %v", err)
}
return encoded
}
// 这是本次线上问题的核心场景:字幕组给的 ASS 是带 BOM 的 UTF-16LE,
// 之前原样下发,浏览器按 UTF-8 解开后整篇是替换字符,一个字都渲染不出来。
func TestDecodeSubtitleTextNormalisesLegacyEncodings(t *testing.T) {
cases := []struct {
name string
raw []byte
}{
{"utf8", []byte(assFixture)},
{"utf8-bom", append([]byte{0xEF, 0xBB, 0xBF}, []byte(assFixture)...)},
{"utf16le-bom", encodeUTF16(assFixture, false, true)},
{"utf16be-bom", encodeUTF16(assFixture, true, true)},
{"utf16le-no-bom", encodeUTF16(assFixture, false, false)},
{"gb18030", encodeGBK(t, assFixture)},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := decodeSubtitleText(tc.raw)
if !strings.Contains(got, "[Script Info]") {
t.Fatalf("decoded text lost ASS header: %q", got)
}
if !strings.Contains(got, "Dialogue:") {
t.Fatalf("decoded text lost Dialogue line: %q", got)
}
if !strings.Contains(got, "放て!心に刻んだ夢を") {
t.Fatalf("decoded text lost non-ASCII content: %q", got)
}
if strings.ContainsRune(got, 0) || strings.ContainsRune(got, '\uFFFD') {
t.Fatalf("decoded text still contains NUL or replacement runes: %q", got)
}
})
}
}
func TestDecodeSubtitleTextKeepsUnknownBytes(t *testing.T) {
raw := []byte("not a subtitle at all: \x81\x82\x83\x84")
if got := decodeSubtitleText(raw); got != string(raw) {
t.Fatalf("decodeSubtitleText mangled non-subtitle bytes: %q", got)
}
}
// 接口层回归:外挂 UTF-16 字幕经 /subtitles/:id/ass(libass 路径)与
// /subtitles/:id(WebVTT 降级路径)下发时都必须是可解析的 UTF-8。
func TestSubtitleHandlersNormaliseUTF16Sidecar(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file:subtitle-encoding?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
if err := db.AutoMigrate(&model.Library{}, &model.Media{}); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
videoPath := filepath.Join(dir, "Railgun S01E01.mkv")
assPath := filepath.Join(dir, "Railgun S01E01.ass")
srtPath := filepath.Join(dir, "Railgun S01E01.zh.srt")
if err := os.WriteFile(videoPath, []byte("video"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(assPath, encodeUTF16(assFixture, false, true), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(srtPath, encodeGBK(t, srtFixture), 0o644); err != nil {
t.Fatal(err)
}
media := model.Media{Title: "Railgun", Path: videoPath}
if err := db.Create(&media).Error; err != nil {
t.Fatal(err)
}
svc := NewSubtitleService(&config.Config{}, zap.NewNop(), repository.New(db))
var buf bytes.Buffer
if err := svc.ServeASS(t.Context(), media.ID, assPath, &buf); err != nil {
t.Fatalf("ServeASS: %v", err)
}
ass := buf.String()
if !bytes.HasPrefix(buf.Bytes(), []byte("[Script Info]")) {
t.Fatalf("ServeASS output is not UTF-8 ASS: %q", ass)
}
if !strings.Contains(ass, "放て!心に刻んだ夢を") {
t.Fatalf("ServeASS output lost dialogue text: %q", ass)
}
buf.Reset()
if err := svc.Serve(t.Context(), media.ID, assPath, &buf); err != nil {
t.Fatalf("Serve(ass): %v", err)
}
vtt := buf.String()
if !strings.HasPrefix(vtt, "WEBVTT") || !strings.Contains(vtt, "-->") {
t.Fatalf("ASS->WebVTT fallback produced no cues: %q", vtt)
}
if !strings.Contains(vtt, "放て!心に刻んだ夢を") {
t.Fatalf("ASS->WebVTT fallback lost dialogue text: %q", vtt)
}
buf.Reset()
if err := svc.Serve(t.Context(), media.ID, srtPath, &buf); err != nil {
t.Fatalf("Serve(srt): %v", err)
}
vtt = buf.String()
if !strings.Contains(vtt, "只有我的超电磁炮") {
t.Fatalf("GBK SRT was not transcoded: %q", vtt)
}
}
+15 -34
View File
@@ -10,7 +10,6 @@ import {
Loader2,
Lock,
Maximize,
MessageSquareText,
Minimize,
Pause,
PictureInPicture,
@@ -155,10 +154,12 @@ type PlayerControlsProps = {
onSubtitleStyleChange: (style: SubtitleStylePreset) => void
/** 弹幕设置面板是否打开。 */
danmakuOpen: boolean
/** 弹幕当前是否渲染在画面上(操作栏上的「弹」开关)。 */
/** 弹幕当前是否渲染在画面上:操作栏的「弹」按钮用它显示开关状态。 */
danmakuEnabled: boolean
onToggleDanmakuEnabled?: (next: boolean) => void
/** 打开弹幕设置面板(弹幕服务的搜索与参数都在那里)。 */
/**
* 打开弹幕设置面板。弹幕的加载开关、来源搜索、渲染参数都收在这个面板里,
* 所以「弹」按钮直接指向它——不再经过「设置」面板中转。
*/
onOpenDanmaku?: () => void
hasPrevEpisode?: boolean
hasNextEpisode?: boolean
@@ -217,7 +218,6 @@ export function PlayerControls({
onSubtitleStyleChange,
danmakuOpen,
danmakuEnabled,
onToggleDanmakuEnabled,
onOpenDanmaku,
hasPrevEpisode = false,
hasNextEpisode = false,
@@ -965,7 +965,7 @@ export function PlayerControls({
/>
</div>
)}
<div className="sm:hidden">{PLAYER_MENU_DIVIDER}</div>
<div className={`sm:hidden ${PLAYER_MENU_DIVIDER}`} />
</>
)
@@ -1021,27 +1021,6 @@ export function PlayerControls({
</button>
</div>
)}
{onOpenDanmaku && (
<div>
<p className={PLAYER_MENU_LABEL}>弹幕</p>
<button
type="button"
onClick={() => {
setSettingsMenuOpen(false)
onOpenDanmaku()
}}
className={PLAYER_MENU_ITEM}
title="打开弹幕设置(搜索弹幕库、调整渲染参数)"
>
<MessageSquareText size={14} className="shrink-0 text-white/50" />
<span className="min-w-0 flex-1 truncate">弹幕设置</span>
{danmakuOpen ? (
<span className="shrink-0 text-[10px] text-rose-300">已打开</span>
) : null}
</button>
</div>
)}
</div>
)
@@ -1237,13 +1216,15 @@ export function PlayerControls({
</button>
)}
{/* 弹幕开关:只负责开/关,「弹幕设置」在设置面板里 */}
{/* 弹幕:点开就是弹幕设置面板。弹幕的加载开关、来源搜索、渲染参数都在
那个面板里,所以这里不再做「先切设置再找弹幕」的中转;按钮上的方块
颜色仍然反映当前弹幕是否显示。 */}
<button
type="button"
onClick={() => onToggleDanmakuEnabled?.(!danmakuEnabled)}
disabled={!onToggleDanmakuEnabled}
className={PLAYER_ICON_BUTTON}
title={danmakuEnabled ? '关闭弹幕显示' : '开启弹幕显示'}
onClick={onOpenDanmaku}
disabled={!onOpenDanmaku}
className={`${PLAYER_ICON_BUTTON} ${danmakuOpen ? 'bg-white/15' : ''}`}
title={danmakuOpen ? '弹幕设置(已打开)' : '弹幕设置'}
>
<span
className={`flex h-[18px] w-[18px] items-center justify-center rounded-[4px] text-[10px] font-bold leading-none transition ${
@@ -1521,7 +1502,7 @@ function SubtitleItems({
{canAdjustSelectedSubtitle && (
<>
{PLAYER_MENU_DIVIDER}
<div className={PLAYER_MENU_DIVIDER} />
<p className={PLAYER_MENU_LABEL}>字幕位置</p>
<div className="flex flex-wrap gap-1 px-2.5 pb-1">
{SUBTITLE_POSITION_OPTIONS.map(([value, label]) => (
@@ -1567,7 +1548,7 @@ function SubtitleItems({
{canConvertSelectedSubtitle && (
<>
{PLAYER_MENU_DIVIDER}
<div className={PLAYER_MENU_DIVIDER} />
<p className={PLAYER_MENU_LABEL}>外挂字幕简繁转换</p>
{(
[
+3 -2
View File
@@ -215,9 +215,10 @@ export function PlayerDanmakuPanel({
</div>
<div className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-4 py-3.5">
{/* 是否加载弹幕 */}
{/* 弹幕总开关。从操作栏的「弹」按钮直接进来就到了这里,所以这一项就是
用户找的「开关弹幕」;文案跟旧的按钮提示保持一致,避免换个说法让人找不到。 */}
<label className="mb-3 flex cursor-pointer items-center justify-between rounded-lg bg-white/5 px-2.5 py-2 text-sm transition hover:bg-white/10">
<span className="text-white/85">加载弹幕</span>
<span className="text-white/85">显示弹幕</span>
<input
type="checkbox"
checked={enabled}
-1
View File
@@ -1696,7 +1696,6 @@ export function PlayerPage() {
danmakuSearchTrigger={danmakuSearchTrigger}
danmakuOpen={danmakuOpen}
onOpenDanmaku={toggleDanmakuOpen}
onToggleDanmakuEnabled={danmakuChangeEnabled}
onDanmakuLoaded={danmakuLoaded}
onDanmakuCandidates={danmakuGotCandidates}
onDanmakuAlternatives={danmakuGotAlternatives}
-4
View File
@@ -187,8 +187,6 @@ type PlayerVideoStageProps = {
danmakuOpen: boolean
/** 打开弹幕设置面板(搜索弹幕库、调整渲染参数)。 */
onOpenDanmaku: () => void
/** 操作栏上的「弹」开关:只控制画面上的弹幕是否渲染。 */
onToggleDanmakuEnabled: (next: boolean) => void
onDanmakuLoaded: (info: DanmakuLoadedInfo | null) => void
onDanmakuCandidates: (candidates: DanmakuAnime[]) => void
onDanmakuAlternatives: (alternatives: DanmakuAnime[]) => void
@@ -264,7 +262,6 @@ export function PlayerVideoStage({
danmakuSearchTrigger = 0,
danmakuOpen,
onOpenDanmaku,
onToggleDanmakuEnabled,
onDanmakuLoaded,
onDanmakuCandidates,
onDanmakuAlternatives,
@@ -876,7 +873,6 @@ export function PlayerVideoStage({
onSubtitleStyleChange={onSubtitleStyleChange}
danmakuOpen={danmakuOpen}
danmakuEnabled={danmakuEnabled}
onToggleDanmakuEnabled={onToggleDanmakuEnabled}
onOpenDanmaku={onOpenDanmaku}
hasPrevEpisode={hasPrevEpisode}
hasNextEpisode={hasNextEpisode}