mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 03:56:38 +08:00
支持 ASS 字幕原样渲染与字幕显示设置
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user