diff --git a/.dockerignore b/.dockerignore index aa7dbce..903a561 100644 --- a/.dockerignore +++ b/.dockerignore @@ -14,6 +14,7 @@ verify-cache/ verify-media/ verify-downloads/ .codex-* +.codex/ .tmp/ .tmp_* .tmp-deploy-* diff --git a/.gitignore b/.gitignore index 3f7bb76..64a5923 100644 --- a/.gitignore +++ b/.gitignore @@ -66,6 +66,7 @@ config.yaml .tmp-live-backups/ .tmp-* .codex-* +.codex/ downloads/ media/ *.pid diff --git a/internal/handler/routes_authenticated_core.go b/internal/handler/routes_authenticated_core.go index c50af69..ea1ca35 100644 --- a/internal/handler/routes_authenticated_core.go +++ b/internal/handler/routes_authenticated_core.go @@ -54,6 +54,7 @@ func registerAuthedMediaRoutes(authed *gin.RouterGroup, svc *service.Container) authed.DELETE("/media/:id", middleware.AdminRequired(), deleteMediaHandler(svc)) authed.GET("/media/:id/subtitles", listSubtitlesHandler(svc)) authed.GET("/subtitles/:id", serveSubtitleHandler(svc)) + authed.GET("/subtitles/:id/ass", serveASSSubtitleHandler(svc)) authed.POST("/media/:id/nfo", middleware.AdminRequired(), exportNFOHandler(svc)) authed.POST("/libraries/:id/nfo", middleware.AdminRequired(), exportLibraryNFOHandler(svc)) } diff --git a/internal/handler/subtitles.go b/internal/handler/subtitles.go index 9c0b84d..8a97e74 100644 --- a/internal/handler/subtitles.go +++ b/internal/handler/subtitles.go @@ -34,6 +34,22 @@ func listSubtitlesHandler(svc *service.Container) gin.HandlerFunc { } } +func serveASSSubtitleHandler(svc *service.Container) gin.HandlerFunc { + return func(c *gin.Context) { + path := c.Query("path") + if path == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "missing path"}) + return + } + c.Header("Content-Type", "text/plain; charset=utf-8") + c.Header("Cache-Control", "no-cache, no-store, must-revalidate") + if err := svc.Subtitle.ServeASS(c.Request.Context(), c.Param("id"), path, c.Writer); err != nil { + c.JSON(http.StatusNotFound, gin.H{"error": err.Error()}) + return + } + } +} + func serveSubtitleHandler(svc *service.Container) gin.HandlerFunc { return func(c *gin.Context) { path := c.Query("path") diff --git a/internal/service/subtitle.go b/internal/service/subtitle.go index 8c3981b..a7b2f25 100644 --- a/internal/service/subtitle.go +++ b/internal/service/subtitle.go @@ -234,7 +234,7 @@ func discoverExternalSubtitleTracks(m *model.Media) []SubtitleTrack { Path: filepath.Join(c, e.Name()), Codec: codec, Source: "external", - Delivery: "webvtt", + Delivery: subtitleDeliveryForCodec(codec), }) } } @@ -297,6 +297,18 @@ func (s *SubtitleService) discoverEmbedded(ctx context.Context, media *model.Med return subtitleTracksFromProbe(probe), nil } +// subtitleDeliveryForCodec selects the browser rendering path for a text +// subtitle codec. ASS/SSA keep their original bytes and are rendered by +// libass in the web player; other text formats are converted to WebVTT. +func subtitleDeliveryForCodec(codec string) string { + switch strings.ToLower(strings.TrimSpace(codec)) { + case "ass", "ssa": + return "ass" + default: + return "webvtt" + } +} + func subtitleTracksFromProbe(probe embeddedSubtitleProbe) []SubtitleTrack { tracks := make([]SubtitleTrack, 0, len(probe.Streams)) for _, stream := range probe.Streams { @@ -314,7 +326,7 @@ func subtitleTracksFromProbe(probe embeddedSubtitleProbe) []SubtitleTrack { } else if stream.Disposition.Default != 0 { label += "(默认)" } - delivery := "webvtt" + delivery := subtitleDeliveryForCodec(codec) if imageSubtitleCodecs[codec] { delivery = "burn" } @@ -464,6 +476,9 @@ func (s *SubtitleService) ServeRaw(ctx context.Context, mediaID, sub string, w i if err != nil || m == nil { return errors.New("media not found") } + if strings.HasPrefix(sub, "embedded:") { + return errors.New("embedded subtitle is not available in raw mode") + } abs, err := filepath.Abs(sub) if err != nil { return err @@ -472,6 +487,9 @@ func (s *SubtitleService) ServeRaw(ctx context.Context, mediaID, sub string, w i if !pathWithin(abs, mediaDir) { return fmt.Errorf("path escape") } + if _, ok := extToCodec[strings.ToLower(filepath.Ext(abs))]; !ok { + return errors.New("unsupported subtitle format") + } f, err := os.Open(abs) // #nosec G304 -- abs is constrained to the media file directory with pathWithin. if err != nil { return err @@ -480,3 +498,49 @@ func (s *SubtitleService) ServeRaw(ctx context.Context, mediaID, sub string, w i _, err = io.Copy(w, f) return err } + +// ServeASS returns an ASS/SSA stream suitable for libass-wasm. External ASS +// files are sent unchanged; embedded ASS/SSA tracks are remuxed to ASS by +// ffmpeg. This keeps fonts, positioning and typesetting data available to the +// browser renderer instead of flattening the track through assToVTT first. +func (s *SubtitleService) ServeASS(ctx context.Context, mediaID, sub string, w io.Writer) error { + m, err := s.repo.Media.FindByID(ctx, mediaID) + if err != nil || m == nil { + return errors.New("media not found") + } + if strings.HasPrefix(sub, "embedded:") { + index, err := strconv.Atoi(strings.TrimPrefix(sub, "embedded:")) + if err != nil || index < 0 { + return errors.New("invalid embedded subtitle") + } + return s.serveEmbeddedASS(ctx, m, index, w) + } + switch strings.ToLower(filepath.Ext(sub)) { + case ".ass", ".ssa": + return s.ServeRaw(ctx, mediaID, sub, w) + default: + return errors.New("subtitle is not ASS/SSA") + } +} + +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 { + return err + } + bin, err := resolveLocalExecutable(s.cfg.App.FFmpegPath, "ffmpeg") + if err != nil { + return err + } + args := []string{"-hide_banner", "-loglevel", "error"} + args = append(args, ffmpegHTTPInputArgs(input)...) + args = append(args, "-i", input.Source, "-map", "0:"+strconv.Itoa(streamIndex), "-c:s", "ass", "-f", "ass", "-") + cmd := exec.CommandContext(ctx, bin, args...) // #nosec G204 -- executable is resolved locally and arguments do not use a shell. + cmd.Stdout = w + var stderr strings.Builder + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("extract embedded ASS subtitle: %w: %s", err, strings.TrimSpace(stderr.String())) + } + return nil +} diff --git a/internal/service/subtitle_test.go b/internal/service/subtitle_test.go index 70a12a7..102d498 100644 --- a/internal/service/subtitle_test.go +++ b/internal/service/subtitle_test.go @@ -100,7 +100,7 @@ func TestEmbeddedSubtitleProbeClassifiesTextAndBitmapTracks(t *testing.T) { if len(tracks) != 2 { t.Fatalf("len(tracks) = %d, want 2", len(tracks)) } - if tracks[0].Delivery != "webvtt" || tracks[0].Path != "embedded:2" { + if tracks[0].Delivery != "ass" || tracks[0].Path != "embedded:2" { t.Fatalf("text track = %#v", tracks[0]) } if tracks[1].Delivery != "burn" || tracks[1].StreamIndex != 4 { @@ -108,6 +108,18 @@ func TestEmbeddedSubtitleProbeClassifiesTextAndBitmapTracks(t *testing.T) { } } +func TestSubtitleDeliveryClassifiesASSForLibass(t *testing.T) { + if got := subtitleDeliveryForCodec("ass"); got != "ass" { + t.Fatalf("ass delivery = %q, want ass", got) + } + if got := subtitleDeliveryForCodec("ssa"); got != "ass" { + t.Fatalf("ssa delivery = %q, want ass", got) + } + if got := subtitleDeliveryForCodec("subrip"); got != "webvtt" { + t.Fatalf("subrip delivery = %q, want webvtt", got) + } +} + func TestNormaliseTimecode(t *testing.T) { cases := map[string]string{ "0:00:01": "00:00:01", @@ -176,4 +188,17 @@ func TestSubtitleServeRawWritesSourceBytes(t *testing.T) { if got := buf.String(); got != raw { t.Fatalf("ServeRaw returned %q, want raw %q", got, raw) } + + buf.Reset() + if err := svc.ServeASS(t.Context(), media.ID, subPath, &buf); err != nil { + t.Fatal(err) + } + if got := buf.String(); got != raw { + t.Fatalf("ServeASS returned %q, want raw ASS %q", got, raw) + } + + buf.Reset() + if err := svc.ServeRaw(t.Context(), media.ID, videoPath, &buf); err == nil { + t.Fatal("ServeRaw accepted a non-subtitle file") + } } diff --git a/web/package-lock.json b/web/package-lock.json index 97fa839..901bf7d 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -13,6 +13,7 @@ "danmu": "^0.20.0", "framer-motion": "^12.38.0", "hls.js": "^1.5.11", + "jassub": "^2.5.16", "lucide-react": "^0.379.0", "opencc-js": "^1.4.2", "qrcode": "^1.5.4", @@ -1753,6 +1754,12 @@ "vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0" } }, + "node_modules/abslink": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/abslink/-/abslink-1.2.2.tgz", + "integrity": "sha512-HCOItvzORPjMzddCmEpbH8+W1VM3yjH+CR7DLtxlxFKX/jz5twJbm7KLfblO7eKLnXINLah8Hk2ZAJqxXeSkOg==", + "license": "Apache-2.0" + }, "node_modules/acorn": { "version": "8.16.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.16.0.tgz", @@ -3108,6 +3115,18 @@ "dev": true, "license": "ISC" }, + "node_modules/jassub": { + "version": "2.5.16", + "resolved": "https://registry.npmjs.org/jassub/-/jassub-2.5.16.tgz", + "integrity": "sha512-n4vjXfraf3o8k3/5TK6DhCYVcbQ/VqhVOgwqLd3l0xfMtsN4fpE/hPq2hPaGagBTOtJS9yHwRoRp3mmchQ9XVg==", + "license": "LGPL-2.1-or-later AND (FTL OR GPL-2.0-or-later) AND MIT AND MIT-Modern-Variant AND ISC AND NTP AND Zlib AND BSL-1.0", + "dependencies": { + "abslink": "^1.2.2", + "lfa-ponyfill": "^1.1.0", + "rvfc-polyfill": "^1.0.8", + "throughput": "^1.0.2" + } + }, "node_modules/jiti": { "version": "1.21.7", "resolved": "https://registry.npmjs.org/jiti/-/jiti-1.21.7.tgz", @@ -3195,6 +3214,13 @@ "node": ">= 0.8.0" } }, + "node_modules/lfa-ponyfill": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/lfa-ponyfill/-/lfa-ponyfill-1.1.1.tgz", + "integrity": "sha512-Had4tsGEHtIj58MRtf6SlubNDltxinxMSdxIn1GdwrWbye8TD+pZ+D6qKETnJ9+vQOfG9q/Ds4WUPskhyAMp5Q==", + "hasInstallScript": true, + "license": "MIT" + }, "node_modules/lilconfig": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-3.1.3.tgz", @@ -4032,6 +4058,12 @@ "queue-microtask": "^1.2.2" } }, + "node_modules/rvfc-polyfill": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/rvfc-polyfill/-/rvfc-polyfill-1.0.8.tgz", + "integrity": "sha512-uA+0wwTkZ4OT8v45pfDfH+7Yq8mY6MvNngiF5Sq6VBgjJsvsfgt7Q18cyZqZjfAhW9rhkgXPX0cW0R9uw7yElA==", + "license": "GPL-3.0" + }, "node_modules/scheduler": { "version": "0.23.2", "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.23.2.tgz", @@ -4219,6 +4251,12 @@ "node": ">=0.8" } }, + "node_modules/throughput": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/throughput/-/throughput-1.0.2.tgz", + "integrity": "sha512-jvK1ZXuhsggjb3qYQjMiU/AVYYiTeqT5thWvYR2yuy2LGM84P5MSSyAinwHahGsdBYKR9m9HncVR/3f3nFKkxg==", + "license": "MIT" + }, "node_modules/tinyglobby": { "version": "0.2.16", "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.16.tgz", diff --git a/web/package.json b/web/package.json index ffd6799..5901862 100644 --- a/web/package.json +++ b/web/package.json @@ -15,6 +15,7 @@ "danmu": "^0.20.0", "framer-motion": "^12.38.0", "hls.js": "^1.5.11", + "jassub": "^2.5.16", "lucide-react": "^0.379.0", "opencc-js": "^1.4.2", "qrcode": "^1.5.4", diff --git a/web/src/api/subtitles.ts b/web/src/api/subtitles.ts index ac696ab..9b6fa5d 100644 --- a/web/src/api/subtitles.ts +++ b/web/src/api/subtitles.ts @@ -8,7 +8,7 @@ export interface SubtitleTrack { url: string codec: string source: 'external' | 'embedded' - delivery: 'webvtt' | 'burn' + delivery: 'webvtt' | 'ass' | 'burn' stream_index?: number } @@ -26,4 +26,11 @@ export const subtitlesAPI = { path, )}&token=${encodeURIComponent(token)}` }, + + assUrl: (mediaId: string, path: string) => { + const token = useAuthStore.getState().token ?? '' + return `/api/subtitles/${encodeURIComponent(mediaId)}/ass?path=${encodeURIComponent( + path, + )}&token=${encodeURIComponent(token)}` + }, } diff --git a/web/src/assets/fonts/LICENSE-NOTO-SANS-SC.txt b/web/src/assets/fonts/LICENSE-NOTO-SANS-SC.txt new file mode 100644 index 0000000..d952d62 --- /dev/null +++ b/web/src/assets/fonts/LICENSE-NOTO-SANS-SC.txt @@ -0,0 +1,92 @@ +This Font Software is licensed under the SIL Open Font License, +Version 1.1. + +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font +creation efforts of academic and linguistic communities, and to +provide a free and open framework in which fonts may be shared and +improved in partnership with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply to +any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software +components as distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, +deleting, or substituting -- in part or in whole -- any of the +components of the Original Version, by changing formats or by porting +the Font Software to a new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, +modify, redistribute, and sell modified and unmodified copies of the +Font Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, in +Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the +corresponding Copyright Holder. This restriction only applies to the +primary font name as presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created using +the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/web/src/assets/fonts/noto-sans-sc-regular.woff2 b/web/src/assets/fonts/noto-sans-sc-regular.woff2 new file mode 100644 index 0000000..f2091ea Binary files /dev/null and b/web/src/assets/fonts/noto-sans-sc-regular.woff2 differ diff --git a/web/src/components/AssSubtitleStage.tsx b/web/src/components/AssSubtitleStage.tsx new file mode 100644 index 0000000..5e03b62 --- /dev/null +++ b/web/src/components/AssSubtitleStage.tsx @@ -0,0 +1,107 @@ +import { useEffect, useRef } from 'react' +import JASSUB from 'jassub' +import jassubWorkerUrl from 'jassub/dist/worker/worker.js?worker&url' +import jassubWasmUrl from 'jassub/dist/wasm/jassub-worker.wasm?url' +import jassubModernWasmUrl from 'jassub/dist/wasm/jassub-worker-modern.wasm?url' +import notoSansSCUrl from '../assets/fonts/noto-sans-sc-regular.woff2?url' + +import { + loadSubtitleChineseConverter, + type SubtitleChineseMode, +} from '../utils/subtitleChinese' +import { convertASSContent } from '../utils/subtitleASS' + +type AssSubtitleStageProps = { + videoRef: React.RefObject + source: string + timeOffset?: number + chineseMode?: SubtitleChineseMode + onReady?: () => void + onError?: (error: unknown) => void +} + +/** + * libass-wasm subtitle layer for external and embedded ASS/SSA tracks. JASSUB + * owns its canvas and attaches it next to the video, so this component itself + * renders no visible DOM nodes. + */ +export function AssSubtitleStage({ + videoRef, + source, + timeOffset = 0, + chineseMode = 'original', + onReady, + onError, +}: AssSubtitleStageProps) { + const instanceRef = useRef(null) + const timeOffsetRef = useRef(timeOffset) + const callbacksRef = useRef({ onReady, onError }) + + useEffect(() => { + callbacksRef.current = { onReady, onError } + }, [onReady, onError]) + + useEffect(() => { + const video = videoRef.current + if (!video || !source) return + + let cancelled = false + let instance: JASSUB | null = null + const controller = new AbortController() + + const load = async () => { + try { + const [response, converter] = await Promise.all([ + fetch(source, { signal: controller.signal }), + loadSubtitleChineseConverter(chineseMode).catch(() => (text: string) => text), + ]) + if (!response.ok) throw new Error(`subtitle request failed: ${response.status}`) + const body = await response.text() + if (!body.trim()) throw new Error('empty subtitle payload') + if (cancelled) return + + instance = new JASSUB({ + video, + subContent: convertASSContent(body, converter), + timeOffset: timeOffsetRef.current, + workerUrl: jassubWorkerUrl, + wasmUrl: jassubWasmUrl, + modernWasmUrl: jassubModernWasmUrl, + fonts: [notoSansSCUrl], + defaultFont: 'Noto Sans SC', + queryFonts: 'local', + prescaleFactor: 1, + }) + instanceRef.current = instance + await instance.ready + if (cancelled) { + void instance.destroy().catch(() => undefined) + return + } + callbacksRef.current.onReady?.() + } catch (error: unknown) { + if (cancelled) return + if (error instanceof DOMException && error.name === 'AbortError') return + callbacksRef.current.onError?.(error) + } + } + + void load() + + return () => { + cancelled = true + controller.abort() + instanceRef.current = null + if (instance) void instance.destroy().catch(() => undefined) + } + // timeOffset is applied live below. Recreating the full libass worker on a + // seek offset change would be unnecessarily expensive. + }, [source, chineseMode, videoRef]) + + useEffect(() => { + timeOffsetRef.current = timeOffset + if (instanceRef.current) instanceRef.current.timeOffset = timeOffset + }, [timeOffset]) + + return null +} diff --git a/web/src/components/PlayerControls.tsx b/web/src/components/PlayerControls.tsx index d1a6261..ca63ac2 100644 --- a/web/src/components/PlayerControls.tsx +++ b/web/src/components/PlayerControls.tsx @@ -16,6 +16,12 @@ import { } from 'lucide-react' import type { SubtitleTrack } from '../api/subtitles' import type { SubtitleChineseMode } from '../utils/subtitleChinese' +import { + SUBTITLE_POSITION_OPTIONS, + SUBTITLE_STYLE_OPTIONS, + type SubtitlePosition, + type SubtitleStylePreset, +} from '../utils/subtitleDisplay' // PlayerControls — custom bottom control bar replacing the native