Compare commits

..

12 Commits

Author SHA1 Message Date
truewhile bad07f7add 优化支持0.5集 2026-09-27 13:52:17 +08:00
truewhile cb539afd2a 优化 2026-09-27 13:04:28 +08:00
truewhile 594166f696 bug处理 2026-09-25 20:30:49 +08:00
truewhile e0b07492a5 优化网页播放跳转 2026-09-25 13:24:10 +08:00
truewhile 3be6978e70 优化 2026-09-25 13:10:23 +08:00
truewhile 065652d758 优化 2026-09-24 22:27:09 +08:00
truewhile e273b5e87d 优化 2026-09-24 11:43:17 +08:00
truewhile c9c612df1b bug处理 2026-09-23 23:07:01 +08:00
truewhile e2cd32af53 处理yamby继续播放bug 2026-09-23 22:31:09 +08:00
truewhile 28485ed429 优化 2026-09-23 17:01:08 +08:00
truewhile 34ccf14cda 优化视频头尾跳过功能 2026-09-23 16:07:28 +08:00
truewhile 44fde6c5a0 优化 2026-09-23 13:53:50 +08:00
86 changed files with 4652 additions and 528 deletions
@@ -0,0 +1,36 @@
# Segment Coverage Implementation Plan
> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.
**Goal:** IntroDB prewarm + same-season intro propagation + shorter miss TTL + playback-only duration probe (option C).
**Architecture:** Extend `MediaSegmentService` for merge/propagate/prewarm; add scheduler job `segment_prewarm`; repository queries for candidates and season siblings.
**Tech Stack:** Go, GORM/SQLite, existing SchedulerService + IntroDB client.
---
### Task 1: Negative cache TTL + ledgerFresh tests
**Files:** `internal/service/media_segment.go`, `internal/service/media_segment_test.go`
- Change `segmentMissingTTL` to 24h; update tests for fresh/stale miss.
### Task 2: Playback merge + propagation
**Files:** `media_segment.go`, `media_segment_repository.go`, `media_repository.go`, tests
- Constant `PropagatedSource = "propagated"`.
- `ListForPlayback` merges sources with priority manual > theintrodb > propagated.
- After successful IntroDB refresh with intro, propagate to same-season siblings (shared `tm_db_id` or `series_id`).
### Task 3: Prewarm API + scheduler job
**Files:** `media_segment.go`, `media_segment_repository.go`, `scheduler.go`, `scheduler_segment_jobs.go`, `service_builder.go`, tests
- `Prewarm(ctx, limit)` rate-limited refreshes.
- Job `segment_prewarm` every 6h.
### Task 4: Verify
- `go test` for affected packages.
@@ -0,0 +1,52 @@
# Segment Coverage Improvements (1–4, option C)
## Goal
Raise intro/outro skip availability without Chromaprint or full-library STRM probing.
## Scope (approved)
1. **IntroDB prewarm** — background job for all queryable media (TMDb + season/episode).
2. **Duration** — playback-path only (no full STRM scan); keep `ListForPlayback` → `EnsureAsync`.
3. **Same-season intro propagation** — copy intro to sibling episodes after a hit.
4. **Negative cache** — miss TTL 24h; 403/429/timeout must not write miss ledger.
## Non-goals
Chromaprint, manual segment UI, library-name filters, batch STRM duration scan.
## Behavior
### Prewarm (`segment_prewarm`)
- Scheduler job, interval 6h, initial delay 6h (avoid restart spike).
- **Opt-in via `segment.prewarm_enabled` (default off)** — system settings toggle「后台预热 IntroDB 片头片段」.
- Candidates: `tm_db_id > 0`, season/episode or movie, ledger missing or stale.
- Order: recently played first, then others.
- Rate: ~1 req/s, stop on context cancel; respect IntroDB 429 retry already in client.
- Reuse `MediaSegmentService` refresh + propagation.
- Manual `RunNow` can bypass the toggle (same pattern as organize).
### Propagation
- After TheIntroDB refresh finds at least one `intro` span, write the same intro window to same-season siblings that lack a `theintrodb` intro.
- Source tag: `propagated`.
- Do not propagate credits/preview/recap.
- Playback merge priority per kind: `manual` > `theintrodb` > `propagated`.
### Negative cache
- `segmentMissingTTL`: 24h (was 7d).
- `segmentFoundTTL`: 30d (unchanged).
- Provider errors (non-404) continue to skip ledger writes.
### Duration (C)
- No new batch probe job.
- Existing `defer ensureMediaProbe` on playback remains the only STRM duration path.
## Success criteria
- Prewarm increases `media_segment_fetches` over time without blocking play.
- One IntroDB intro hit can surface skip on sibling episodes via `propagated`.
- Misses are retried within ~24h (sooner if recently played and prewarm runs).
+26 -16
View File
@@ -112,8 +112,7 @@ func TestGetDanmakuConfigIncludesPerUserMergePreference(t *testing.T) {
t.Fatalf("status = %d, want 200", w.Code)
}
var cfg struct {
MergeSources bool `json:"merge_sources"`
PlaybackRate float64 `json:"playback_rate"`
MergeSources bool `json:"merge_sources"`
}
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
t.Fatalf("decode: %v", err)
@@ -121,9 +120,6 @@ func TestGetDanmakuConfigIncludesPerUserMergePreference(t *testing.T) {
if cfg.MergeSources {
t.Fatal("default merge preference should be false")
}
if cfg.PlaybackRate != 1 {
t.Fatalf("default playback rate = %v, want 1", cfg.PlaybackRate)
}
if err := svc.Danmaku.SetMergeSources(t.Context(), "user-1", true); err != nil {
t.Fatalf("set: %v", err)
@@ -141,7 +137,7 @@ func TestGetDanmakuConfigIncludesPerUserMergePreference(t *testing.T) {
func TestUpdateDanmakuSettingsPersistsAllPlayerPreferences(t *testing.T) {
svc := newDanmakuSettingsService(t)
body := "{\"enabled\":false,\"opacity\":0.6,\"font_size\":32,\"area\":0.7,\"merge_sources\":true,\"volume\":0.35,\"playback_rate\":1.5,\"source\":\"https://dm.example/base/\",\"app_id\":\"my-app-id\",\"app_key\":\"my-app-secret\"}"
body := "{\"enabled\":false,\"opacity\":0.6,\"font_size\":32,\"area\":0.7,\"merge_sources\":true,\"volume\":0.35,\"source\":\"https://dm.example/base/\",\"app_id\":\"my-app-id\",\"app_key\":\"my-app-secret\"}"
c, w := newDanmakuSettingsContext(t, svc, http.MethodPut, "/danmaku/settings", body, "user-1")
updateDanmakuSettingsHandler(svc)(c)
@@ -158,7 +154,7 @@ func TestUpdateDanmakuSettingsPersistsAllPlayerPreferences(t *testing.T) {
if cfg.Enabled || cfg.Opacity != "0.6" || cfg.FontSize != "32" || cfg.Area != "0.7" {
t.Fatalf("unexpected render config: %+v", cfg)
}
if !cfg.MergeSources || cfg.Volume != 0.35 || cfg.PlaybackRate != 1.5 {
if !cfg.MergeSources || cfg.Volume != 0.35 {
t.Fatalf("unexpected user preferences: %+v", cfg)
}
if cfg.Source != "https://dm.example/base" || cfg.AppID != "my-app-id" || !cfg.AppKeyConfigured {
@@ -169,7 +165,7 @@ func TestUpdateDanmakuSettingsPersistsAllPlayerPreferences(t *testing.T) {
if err != nil || user == nil {
t.Fatalf("read persisted user: %v", err)
}
if user.DanmakuAppKey != "my-app-secret" || user.PlayerVolume != 0.35 || user.PlayerPlaybackRate != 1.5 || user.DanmakuSource != "https://dm.example/base" {
if user.DanmakuAppKey != "my-app-secret" || user.PlayerVolume != 0.35 || user.DanmakuSource != "https://dm.example/base" {
t.Fatalf("preferences not persisted: %+v", user)
}
}
@@ -207,22 +203,36 @@ func TestUpdateDanmakuSettingsPersistsVr360GuideSeen(t *testing.T) {
}
}
func TestUpdateDanmakuSettingsRejectsInvalidPlaybackRate(t *testing.T) {
// 播放倍速只作用于当前视频:接口既不下发也不保存它。老客户端仍可能带上
// playback_rate,服务端应当忽略而不是报错。
func TestDanmakuSettingsIgnorePlaybackRate(t *testing.T) {
svc := newDanmakuSettingsService(t)
c, w := newDanmakuSettingsContext(t, svc, http.MethodPut, "/danmaku/settings",
`{"playback_rate":4}`, "user-1")
updateDanmakuSettingsHandler(svc)(c)
c, w := newDanmakuSettingsContext(t, svc, http.MethodGet, "/danmaku/config", "", "user-1")
getDanmakuConfigHandler(svc)(c)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want 200 (body=%s)", w.Code, w.Body.String())
}
if bytes.Contains(w.Body.Bytes(), []byte("playback_rate")) {
t.Fatalf("config must not expose playback rate: %s", w.Body.String())
}
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400 (body=%s)", w.Code, w.Body.String())
c2, w2 := newDanmakuSettingsContext(t, svc, http.MethodPut, "/danmaku/settings",
`{"volume":0.5,"playback_rate":1.5}`, "user-1")
updateDanmakuSettingsHandler(svc)(c2)
if w2.Code != http.StatusOK {
t.Fatalf("status = %d, want 200 (body=%s)", w2.Code, w2.Body.String())
}
if bytes.Contains(w2.Body.Bytes(), []byte("playback_rate")) {
t.Fatalf("settings response must not expose playback rate: %s", w2.Body.String())
}
user, err := svc.Repo.User.FindByID(t.Context(), "user-1")
if err != nil || user == nil {
t.Fatalf("read user: %v", err)
}
if user.PlayerPlaybackRate != 1 {
t.Fatalf("invalid playback rate was persisted: %v", user.PlayerPlaybackRate)
if user.PlayerVolume != 0.5 {
t.Fatalf("volume not persisted: %v", user.PlayerVolume)
}
}
+36 -1
View File
@@ -24,7 +24,42 @@ func embyNextUpHandler(svc *service.Container) gin.HandlerFunc {
return
}
limit, _ := strconv.Atoi(embyFirstNonEmptyString(firstQueryValue(c, "Limit", "limit"), ""))
out, err := svc.Emby.NextUp(c.Request.Context(), userID, limit)
// YamBy / Emby 进剧集详情会带 SeriesId 请求「本剧下一集」。
// 忽略该参数会把全站 NextUp 第一条塞进详情页「继续播放」。
// 注意:不要把普通 ParentId(媒体库)当成 SeriesId,否则首页 NextUp 会被滤空。
seriesID := firstQueryValue(c, "SeriesId", "seriesId", "seriesid")
if seriesID == "" {
if parentID := firstQueryValue(c, "ParentId", "parentId", "parentid"); parentID != "" {
if strings.HasPrefix(parentID, "msgo-series-") || service.IsEmbyRemoteID(parentID) {
seriesID = parentID
}
}
}
out, err := svc.Emby.NextUp(c.Request.Context(), userID, seriesID, limit)
if err != nil {
c.JSON(http.StatusOK, embyEmptyItemsPayload())
return
}
embyAttachRequestTokenToMediaSources(c, out)
c.JSON(http.StatusOK, out)
}
}
// embyShowNextUpHandler 处理 /Shows/{id}/NextUp:把路径上的剧集 ID 当作 SeriesId。
func embyShowNextUpHandler(svc *service.Container) gin.HandlerFunc {
return func(c *gin.Context) {
userID := embyScopedUserID(c)
if userID == "" {
c.JSON(http.StatusOK, embyEmptyItemsPayload())
return
}
seriesID := strings.TrimSpace(c.Param("id"))
if seriesID == "" || strings.EqualFold(seriesID, "NextUp") {
c.JSON(http.StatusOK, embyEmptyItemsPayload())
return
}
limit, _ := strconv.Atoi(embyFirstNonEmptyString(firstQueryValue(c, "Limit", "limit"), ""))
out, err := svc.Emby.NextUp(c.Request.Context(), userID, seriesID, limit)
if err != nil {
c.JSON(http.StatusOK, embyEmptyItemsPayload())
return
+130 -2
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
@@ -104,8 +105,8 @@ func TestEmbyNextUpReturnsNextEpisode(t *testing.T) {
}
if watched {
h := &model.PlaybackHistory{
UserID: userID, MediaID: m.ID, PositionMs: 1000, DurationMs: 2000,
WatchedAt: watchedAt, Completed: false,
UserID: userID, MediaID: m.ID, PositionMs: 2000, DurationMs: 2000,
WatchedAt: watchedAt, Completed: true, // 第 1 集已看完
}
if err := svc.Repo.DB.Create(h).Error; err != nil {
t.Fatal(err)
@@ -126,6 +127,49 @@ func TestEmbyNextUpReturnsNextEpisode(t *testing.T) {
}
}
// 回归:用户在剧集详情页点播放、只看了几秒就退出(历史行 completed=false)后,
// Yamby 再次进入详情页带的 NextUp 仍要指向那一集本身,否则「继续播放」会跳到下一集。
func TestEmbyNextUpKeepsPartiallyWatchedEpisode(t *testing.T) {
router, svc, userID := newEmbyDiscoveryEnv(t)
libID := seedEmbyLibrary(t, svc, "tv")
episodeIDs := map[int]string{}
for ep := 1; ep <= 3; ep++ {
m := &model.Media{
LibraryID: libID, SeriesID: "series-1", Title: "剧一",
SeasonNum: 1, EpisodeNum: ep,
Path: "/media/tv/S1E" + strconv.Itoa(ep) + ".mkv",
}
if err := svc.Repo.DB.Create(m).Error; err != nil {
t.Fatal(err)
}
episodeIDs[ep] = m.ID
}
// 第 2 集播放了 3 秒后退出:有进度、未标记看完。
if err := svc.Repo.DB.Create(&model.PlaybackHistory{
UserID: userID, MediaID: episodeIDs[2], PositionMs: 3582, DurationMs: 1440064,
WatchedAt: time.Now(), Completed: false,
}).Error; err != nil {
t.Fatal(err)
}
w := embyGet(t, router, "/emby/Shows/NextUp?SeriesId=series-1&Limit=1", signedTestToken(t, "test-secret"))
if w.Code != http.StatusOK {
t.Fatalf("status = %d body=%s", w.Code, w.Body.String())
}
items := decodeItemsEnvelope(t, w.Body.Bytes())
if len(items) != 1 {
t.Fatalf("items = %d, want 1 (body=%s)", len(items), w.Body.String())
}
if id, _ := items[0]["Id"].(string); id != episodeIDs[2] {
t.Fatalf("Id = %q, want the partially watched episode %q (body=%s)", id, episodeIDs[2], w.Body.String())
}
if index, ok := items[0]["IndexNumber"].(float64); !ok || int(index) != 2 {
t.Fatalf("IndexNumber = %v, want 2 (body=%s)", items[0]["IndexNumber"], w.Body.String())
}
}
// 没有历史时必须返回合法空信封,不能 404/500。
func TestEmbyNextUpEmptyWithoutHistory(t *testing.T) {
router, _, _ := newEmbyDiscoveryEnv(t)
@@ -242,3 +286,87 @@ func TestEmbyNextUpRejectsForeignUserID(t *testing.T) {
t.Fatalf("items = %d, want 0", len(items))
}
}
// YamBy 等客户端进入剧集详情会带 SeriesId 调 NextUp;必须只返回该剧的下一集,
// 不能回落成全站「继续观看」第一条,否则详情页播放会串到别的片子。
func TestEmbyNextUpFiltersBySeriesID(t *testing.T) {
router, svc, userID := newEmbyDiscoveryEnv(t)
libID := seedEmbyLibrary(t, svc, "tv")
recent := time.Now().Add(-time.Minute)
older := time.Now().Add(-2 * time.Hour)
seedSeries := func(seriesID, title string, watchedAt time.Time) (watchedID, nextID string) {
t.Helper()
for ep := 1; ep <= 3; ep++ {
m := &model.Media{
LibraryID: libID, SeriesID: seriesID, Title: title,
SeasonNum: 1, EpisodeNum: ep,
Path: "/media/tv/" + seriesID + "/S1E" + strconv.Itoa(ep) + ".mkv",
}
if err := svc.Repo.DB.Create(m).Error; err != nil {
t.Fatal(err)
}
switch ep {
case 1:
watchedID = m.ID
h := &model.PlaybackHistory{
UserID: userID, MediaID: m.ID, PositionMs: 2000, DurationMs: 2000,
WatchedAt: watchedAt, Completed: true, // 第 1 集已看完,下一集是 S1E2
}
if err := svc.Repo.DB.Create(h).Error; err != nil {
t.Fatal(err)
}
case 2:
nextID = m.ID
}
}
return watchedID, nextID
}
_, _ = seedSeries("series-hot", "热门剧", recent)
_, wantNext := seedSeries("series-cold", "目标剧", older)
token := signedTestToken(t, "test-secret")
global := embyGet(t, router, "/emby/Shows/NextUp?Limit=10", token)
if global.Code != http.StatusOK {
t.Fatalf("global status = %d body=%s", global.Code, global.Body.String())
}
if items := decodeItemsEnvelope(t, global.Body.Bytes()); len(items) < 2 {
t.Fatalf("global items = %d, want >= 2 (body=%s)", len(items), global.Body.String())
}
scoped := embyGet(t, router, "/emby/Shows/NextUp?SeriesId=series-cold&Limit=10", token)
if scoped.Code != http.StatusOK {
t.Fatalf("scoped status = %d body=%s", scoped.Code, scoped.Body.String())
}
items := decodeItemsEnvelope(t, scoped.Body.Bytes())
if len(items) != 1 {
t.Fatalf("scoped items = %d, want 1 (body=%s)", len(items), scoped.Body.String())
}
if id, _ := items[0]["Id"].(string); id != wantNext {
t.Fatalf("scoped Id = %q, want %q (body=%s)", id, wantNext, scoped.Body.String())
}
if seriesID, _ := items[0]["SeriesId"].(string); seriesID != "series-cold" {
t.Fatalf("scoped SeriesId = %q, want series-cold", seriesID)
}
empty := embyGet(t, router, "/emby/Shows/NextUp?SeriesId=series-never-watched", token)
if empty.Code != http.StatusOK {
t.Fatalf("empty status = %d body=%s", empty.Code, empty.Body.String())
}
if items := decodeItemsEnvelope(t, empty.Body.Bytes()); len(items) != 0 {
t.Fatalf("never-watched items = %d, want 0 (body=%s)", len(items), empty.Body.String())
}
pathScoped := embyGet(t, router, "/emby/Shows/series-cold/NextUp?Limit=10", token)
if pathScoped.Code != http.StatusOK {
t.Fatalf("path scoped status = %d body=%s", pathScoped.Code, pathScoped.Body.String())
}
pathItems := decodeItemsEnvelope(t, pathScoped.Body.Bytes())
if len(pathItems) != 1 {
t.Fatalf("path scoped items = %d, want 1 (body=%s)", len(pathItems), pathScoped.Body.String())
}
if id, _ := pathItems[0]["Id"].(string); id != wantNext {
t.Fatalf("path scoped Id = %q, want %q", id, wantNext)
}
}
+17 -2
View File
@@ -132,7 +132,10 @@ func embyResumeItemsHandler(svc *service.Container) gin.HandlerFunc {
return func(c *gin.Context) {
uid := embyEffectiveUserID(c)
limit, _ := strconv.Atoi(embyFirstNonEmptyString(firstQueryValue(c, "Limit", "limit"), "20"))
out, err := svc.Emby.ResumeItems(c.Request.Context(), uid, limit)
startIndex, _ := strconv.Atoi(embyFirstNonEmptyString(firstQueryValue(c, "StartIndex", "startIndex", "startindex"), "0"))
// ParentId / SeriesId 收窄到当前库或当前剧,避免详情页继续播放串到全站历史。
parentID := firstQueryValue(c, "ParentId", "parentId", "parentid", "SeriesId", "seriesId", "seriesid")
out, err := svc.Emby.ResumeItems(c.Request.Context(), uid, parentID, limit, startIndex)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
@@ -228,12 +231,24 @@ func embyShowEpisodesHandler(svc *service.Container) gin.HandlerFunc {
// 不一致时把结果过滤成空集。
seasonIndex = nil
}
// Emby 客户端按 StartIndex/Limit 分页拉取分集,并在已收条目数小于
// TotalRecordCount 时继续请求下一页。忽略这两个参数会让客户端永远
// 停在第一页、反复重发同一请求(现象是分集列表一直加载不出来)。
limit, _ := strconv.Atoi(embyFirstNonEmptyString(firstQueryValue(c, "Limit", "limit"), "500"))
if limit <= 0 {
limit = 500
}
startIndex, _ := strconv.Atoi(embyFirstNonEmptyString(firstQueryValue(c, "StartIndex", "startIndex", "startindex"), "0"))
if startIndex < 0 {
startIndex = 0
}
params := service.ItemsParams{
UserID: embyEffectiveUserID(c),
ParentID: parentID,
IncludeItemTypes: []string{"Episode"},
Recursive: true,
Limit: 500,
Limit: limit,
StartIndex: startIndex,
SeasonIndex: seasonIndex,
}
out, err := svc.Emby.Items(c.Request.Context(), params)
+3
View File
@@ -198,6 +198,9 @@ func registerEmbyAuthenticatedItemRoutes(auth *gin.RouterGroup, svc *service.Con
auth.GET("/Users/:userId/Shows/:id/Episodes", embyShowEpisodesHandler(svc))
auth.GET("/Shows/NextUp", embyNextUpHandler(svc))
auth.GET("/Users/:userId/Shows/NextUp", embyNextUpHandler(svc))
// 部分客户端用路径形式 /Shows/{seriesId}/NextUp,而不是 query SeriesId。
auth.GET("/Shows/:id/NextUp", embyShowNextUpHandler(svc))
auth.GET("/Users/:userId/Shows/:id/NextUp", embyShowNextUpHandler(svc))
auth.GET("/MediaSegments/:id", embyMediaSegmentsHandler(svc))
auth.GET("/Items/:id/MediaSegments", embyMediaSegmentsHandler(svc))
auth.GET("/Users/:userId/Items/:id/MediaSegments", embyMediaSegmentsHandler(svc))
@@ -42,6 +42,8 @@ func registerLowercaseEmbyItemRoutes(auth *gin.RouterGroup, svc *service.Contain
auth.GET("/users/:userId/shows/:id/episodes", embyShowEpisodesHandler(svc))
auth.GET("/shows/nextup", embyNextUpHandler(svc))
auth.GET("/users/:userId/shows/nextup", embyNextUpHandler(svc))
auth.GET("/shows/:id/nextup", embyShowNextUpHandler(svc))
auth.GET("/users/:userId/shows/:id/nextup", embyShowNextUpHandler(svc))
auth.GET("/mediasegments/:id", embyMediaSegmentsHandler(svc))
auth.GET("/items/:id/mediasegments", embyMediaSegmentsHandler(svc))
auth.GET("/users/:userId/items/:id/mediasegments", embyMediaSegmentsHandler(svc))
@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gin-gonic/gin"
@@ -21,6 +22,31 @@ import (
// two seasons, so /Shows/{id}/Episodes can be exercised with the query forms
// real clients send.
func embySeasonEpisodesRouter(t *testing.T) (*gin.Engine, string, string) {
t.Helper()
return embySeriesRouter(t, []model.Media{
{
Base: model.Base{ID: "s1e1"},
Title: "Test Show",
Path: "D:\\media\\tv\\Test Show\\Season 01\\Test Show - S01E01.mkv",
SeasonNum: 1,
EpisodeNum: 1,
Container: "mkv",
},
{
Base: model.Base{ID: "s2e1"},
Title: "Test Show",
Path: "D:\\media\\tv\\Test Show\\Season 02\\Test Show - S02E01.mkv",
SeasonNum: 2,
EpisodeNum: 1,
Container: "mkv",
},
})
}
// embySeriesRouter builds an Emby-compatible router with a single tv library and
// one series made of the given episode rows. Callers omit LibraryID: it is filled
// in here once the library exists.
func embySeriesRouter(t *testing.T, rows []model.Media) (*gin.Engine, string, string) {
t.Helper()
gin.SetMode(gin.TestMode)
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
@@ -48,27 +74,9 @@ func embySeasonEpisodesRouter(t *testing.T) (*gin.Engine, string, string) {
if err := repos.Library.Create(t.Context(), &lib); err != nil {
t.Fatalf("create library: %v", err)
}
for _, m := range []model.Media{
{
Base: model.Base{ID: "s1e1"},
LibraryID: lib.ID,
Title: "Test Show",
Path: "D:\\media\\tv\\Test Show\\Season 01\\Test Show - S01E01.mkv",
SeasonNum: 1,
EpisodeNum: 1,
Container: "mkv",
},
{
Base: model.Base{ID: "s2e1"},
LibraryID: lib.ID,
Title: "Test Show",
Path: "D:\\media\\tv\\Test Show\\Season 02\\Test Show - S02E01.mkv",
SeasonNum: 2,
EpisodeNum: 1,
Container: "mkv",
},
} {
if err := db.Create(&m).Error; err != nil {
for i := range rows {
rows[i].LibraryID = lib.ID
if err := db.Create(&rows[i]).Error; err != nil {
t.Fatalf("create media: %v", err)
}
}
@@ -104,7 +112,34 @@ func embySeasonEpisodesRouter(t *testing.T) (*gin.Engine, string, string) {
return router, secret, seriesID
}
func fetchEpisodeIDs(t *testing.T, router *gin.Engine, secret, path string) ([]string, int) {
func fetchSeasonID(t *testing.T, router *gin.Engine, secret, seriesID string, index int) string {
t.Helper()
path := "/Shows/" + seriesID + "/Seasons"
req := httptest.NewRequest(http.MethodGet, path, nil)
req.Header.Set("X-Emby-Token", signedTestToken(t, secret))
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("seasons status=%d body=%s", rec.Code, rec.Body.String())
}
var seasons struct {
Items []map[string]any `json:"Items"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &seasons); err != nil {
t.Fatalf("decode seasons: %v", err)
}
for _, s := range seasons.Items {
if got, ok := s["IndexNumber"].(float64); ok && int(got) == index {
if id, _ := s["Id"].(string); id != "" {
return id
}
}
}
t.Fatalf("season %d not found in %#v", index, seasons.Items)
return ""
}
func fetchEpisodeItems(t *testing.T, router *gin.Engine, secret, path string) ([]map[string]any, int) {
t.Helper()
req := httptest.NewRequest(http.MethodGet, path, nil)
req.Header.Set("X-Emby-Token", signedTestToken(t, secret))
@@ -120,12 +155,18 @@ func fetchEpisodeIDs(t *testing.T, router *gin.Engine, secret, path string) ([]s
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
t.Fatalf("decode %s: %v", path, err)
}
ids := make([]string, 0, len(payload.Items))
for _, item := range payload.Items {
return payload.Items, int(payload.TotalRecordCount)
}
func fetchEpisodeIDs(t *testing.T, router *gin.Engine, secret, path string) ([]string, int) {
t.Helper()
items, total := fetchEpisodeItems(t, router, secret, path)
ids := make([]string, 0, len(items))
for _, item := range items {
id, _ := item["Id"].(string)
ids = append(ids, id)
}
return ids, int(payload.TotalRecordCount)
return ids, total
}
// TestEmbyShowEpisodesHonoursSeasonQueryParam is the client-facing regression
@@ -174,29 +215,7 @@ func TestEmbyShowEpisodesHonoursSeasonQueryParam(t *testing.T) {
func TestEmbyShowEpisodesSeasonIdStillWins(t *testing.T) {
router, secret, seriesID := embySeasonEpisodesRouter(t)
seasonsPath := "/Shows/" + seriesID + "/Seasons"
req := httptest.NewRequest(http.MethodGet, seasonsPath, nil)
req.Header.Set("X-Emby-Token", signedTestToken(t, secret))
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("seasons status=%d body=%s", rec.Code, rec.Body.String())
}
var seasons struct {
Items []map[string]any `json:"Items"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &seasons); err != nil {
t.Fatalf("decode seasons: %v", err)
}
var season1ID string
for _, s := range seasons.Items {
if index, ok := s["IndexNumber"].(float64); ok && int(index) == 1 {
season1ID, _ = s["Id"].(string)
}
}
if season1ID == "" {
t.Fatalf("season 1 not found in %#v", seasons.Items)
}
season1ID := fetchSeasonID(t, router, secret, seriesID, 1)
ids, _ := fetchEpisodeIDs(t, router, secret, "/Shows/"+seriesID+"/Episodes?SeasonId="+season1ID)
if len(ids) != 1 || ids[0] != "s1e1" {
@@ -209,3 +228,145 @@ func TestEmbyShowEpisodesSeasonIdStillWins(t *testing.T) {
t.Fatalf("SeasonId+Season episodes = %#v, want [s1e1]", ids)
}
}
// foldedDuplicateSeriesRows models a season where two files resolve to the same
// episode number — 3月的狮子 S01E11 together with S01E11.5, which the episode
// parser reads as S01E11 and stores as a second row for season 1 episode 2 here.
func foldedDuplicateSeriesRows() []model.Media {
return []model.Media{
{
Base: model.Base{ID: "e1"},
Title: "三月的狮子",
Path: "D:\\media\\tv\\三月的狮子\\三月的狮子 - S01E01.mkv",
SeasonNum: 1,
EpisodeNum: 1,
Container: "mkv",
},
{
Base: model.Base{ID: "e2"},
Title: "三月的狮子",
Path: "D:\\media\\tv\\三月的狮子\\三月的狮子 - S01E02.mkv",
SeasonNum: 1,
EpisodeNum: 2,
Container: "mkv",
},
{
Base: model.Base{ID: "e2half"},
Title: "三月的狮子",
Path: "D:\\media\\tv\\三月的狮子\\三月的狮子 - S01E02.5.mkv",
SeasonNum: 1,
EpisodeNum: 2,
Container: "mkv",
},
}
}
// TestEmbyShowEpisodesFoldedDuplicateKeepsTotalsConsistent is the regression test
// for the "第一季分集加载不出来" lock-up: two rows sharing one season/episode are
// folded into a single Emby item with two MediaSources. TotalRecordCount used to
// be computed before that fold, so clients that page until they have
// TotalRecordCount items kept re-requesting the same page and never rendered the
// season.
func TestEmbyShowEpisodesFoldedDuplicateKeepsTotalsConsistent(t *testing.T) {
router, secret, seriesID := embySeriesRouter(t, foldedDuplicateSeriesRows())
seasonID := fetchSeasonID(t, router, secret, seriesID, 1)
items, total := fetchEpisodeItems(t, router, secret, "/Shows/"+seriesID+"/Episodes?SeasonId="+seasonID)
if total != len(items) {
t.Fatalf("TotalRecordCount = %d but %d items returned, want them equal", total, len(items))
}
if total != 2 {
t.Fatalf("TotalRecordCount = %d, want 2 distinct episodes", total)
}
// The folded episode must still carry both files so clients can switch version.
versioned := 0
for _, item := range items {
if sources, ok := item["MediaSources"].([]any); ok && len(sources) > 1 {
versioned++
}
}
if versioned != 1 {
t.Fatalf("items with multiple MediaSources = %d, want 1 (%#v)", versioned, items)
}
}
// TestEmbyShowEpisodesPagingAdvances pins that the client's StartIndex/Limit reach
// the episode list. Ignoring them made every page identical, so a client paging
// until it has TotalRecordCount items could never finish loading the season.
func TestEmbyShowEpisodesPagingAdvances(t *testing.T) {
router, secret, seriesID := embySeriesRouter(t, foldedDuplicateSeriesRows())
base := "/Shows/" + seriesID + "/Episodes?Season=1"
first, total := fetchEpisodeIDs(t, router, secret, base+"&StartIndex=0&Limit=1")
if total != 2 || len(first) != 1 {
t.Fatalf("page 1 = %#v (total=%d), want one item of two", first, total)
}
second, _ := fetchEpisodeIDs(t, router, secret, base+"&StartIndex=1&Limit=1")
if len(second) != 1 || second[0] == first[0] {
t.Fatalf("page 2 = %#v, want the remaining episode (page 1 = %#v)", second, first)
}
third, _ := fetchEpisodeIDs(t, router, secret, base+"&StartIndex=2&Limit=1")
if len(third) != 0 {
t.Fatalf("page 3 = %#v, want an empty page so clients stop paging", third)
}
}
// 半集(S01E11.5)在 Emby 侧必须和第 11 集并列返回:一旦被折成一个条目,
// TotalRecordCount 就会比 Items 多一条,按总数翻页的客户端会一直重发同一页
// (这就是「第一季分集加载不出来」的根因)。Emby 的 IndexNumber 只能是整数,
// 所以集号仍占 11,小数通过条目名体现。
func TestEmbyShowEpisodesKeepsHalfEpisodeVisible(t *testing.T) {
router, secret, seriesID := embySeriesRouter(t, []model.Media{
{
Base: model.Base{ID: "e11"},
Title: "三月的狮子",
Path: "D:\\media\\tv\\三月的狮子\\三月的狮子 - S01E11.mkv",
SeasonNum: 1,
EpisodeNum: 11,
Container: "mkv",
},
{
Base: model.Base{ID: "e11half"},
Title: "三月的狮子",
Path: "D:\\media\\tv\\三月的狮子\\三月的狮子 - S01E11.5.mkv",
SeasonNum: 1,
EpisodeNum: 11,
EpisodeFraction: 0.5,
Container: "mkv",
},
{
Base: model.Base{ID: "e12"},
Title: "三月的狮子",
Path: "D:\\media\\tv\\三月的狮子\\三月的狮子 - S01E12.mkv",
SeasonNum: 1,
EpisodeNum: 12,
Container: "mkv",
},
})
items, total := fetchEpisodeItems(t, router, secret, "/Shows/"+seriesID+"/Episodes?Season=1")
if total != 3 || len(items) != 3 {
t.Fatalf("TotalRecordCount = %d, items = %d, want 3/3", total, len(items))
}
wantOrder := []string{"e11", "e11half", "e12"}
for i, want := range wantOrder {
id, _ := items[i]["Id"].(string)
if id != want {
t.Fatalf("items[%d] = %q, want %q (order %#v)", i, id, want, items)
}
}
for _, item := range items {
sources, _ := item["MediaSources"].([]any)
if len(sources) != 1 {
t.Fatalf("item %v folded %d sources, want 1", item["Id"], len(sources))
}
}
halfName, _ := items[1]["Name"].(string)
if !strings.HasPrefix(halfName, "第 11.5 集") {
t.Fatalf("half episode name = %q, want prefix 第 11.5 集", halfName)
}
if idx, _ := items[1]["IndexNumber"].(float64); int(idx) != 11 {
t.Fatalf("half episode IndexNumber = %v, want 11 (Emby only supports integers)", items[1]["IndexNumber"])
}
}
+33 -32
View File
@@ -26,38 +26,39 @@ type LibraryRoot struct {
// Media 是单个可播放项。剧集链接到 SeriesID;电影 SeriesID == ""。
type Media struct {
Base
LibraryID string `gorm:"index;size:36;index:idx_media_library_release,priority:1" json:"library_id"`
LibraryRootID string `gorm:"index;size:36" json:"library_root_id,omitempty"`
SeriesID string `gorm:"index;size:128" json:"series_id,omitempty"`
Title string `gorm:"size:255;not null" json:"title"`
OriginalName string `gorm:"size:255" json:"original_name,omitempty"`
EpisodeTitle string `gorm:"size:255" json:"episode_title,omitempty"`
Path string `gorm:"uniqueIndex;size:1024;not null" json:"path"`
RelativePath string `gorm:"size:1024" json:"relative_path,omitempty"`
SizeBytes int64 `json:"size_bytes"`
DurationSec int `json:"duration_sec"`
Width int `json:"width"`
Height int `json:"height"`
VideoCodec string `gorm:"size:32" json:"video_codec,omitempty"`
AudioCodec string `gorm:"size:32" json:"audio_codec,omitempty"`
Container string `gorm:"size:128" json:"container,omitempty"`
PosterURL string `gorm:"size:1024" json:"poster_url,omitempty"`
BackdropURL string `gorm:"size:1024" json:"backdrop_url,omitempty"`
Overview string `gorm:"type:text" json:"overview,omitempty"`
Rating float32 `json:"rating"`
Year int `json:"year"`
ReleaseDate string `gorm:"size:10;index:idx_media_library_release,priority:2" json:"release_date,omitempty"`
SeasonNum int `json:"season_num"`
EpisodeNum int `json:"episode_num"`
ScrapeStatus string `gorm:"size:16;default:pending" json:"scrape_status"`
TMDbID int `json:"tmdb_id"`
BangumiID int `json:"bangumi_id"`
DoubanID string `gorm:"column:douban_id;size:32" json:"douban_id,omitempty"`
TheTVDBID string `gorm:"column:thetvdb_id;size:64" json:"thetvdb_id,omitempty"`
Languages string `gorm:"size:64" json:"languages,omitempty"` // 逗号分隔的 ISO 639-1 代码,如 "zh,en"
Countries string `gorm:"size:128" json:"countries,omitempty"` // 逗号分隔的 ISO 3166-1,如 "CN,US"
Genres string `gorm:"type:text" json:"genres,omitempty"` // 逗号分隔的类型名,如 "Action,Animation"
NSFW bool `gorm:"default:false" json:"nsfw"`
LibraryID string `gorm:"index;size:36;index:idx_media_library_release,priority:1" json:"library_id"`
LibraryRootID string `gorm:"index;size:36" json:"library_root_id,omitempty"`
SeriesID string `gorm:"index;size:128" json:"series_id,omitempty"`
Title string `gorm:"size:255;not null" json:"title"`
OriginalName string `gorm:"size:255" json:"original_name,omitempty"`
EpisodeTitle string `gorm:"size:255" json:"episode_title,omitempty"`
Path string `gorm:"uniqueIndex;size:1024;not null" json:"path"`
RelativePath string `gorm:"size:1024" json:"relative_path,omitempty"`
SizeBytes int64 `json:"size_bytes"`
DurationSec int `json:"duration_sec"`
Width int `json:"width"`
Height int `json:"height"`
VideoCodec string `gorm:"size:32" json:"video_codec,omitempty"`
AudioCodec string `gorm:"size:32" json:"audio_codec,omitempty"`
Container string `gorm:"size:128" json:"container,omitempty"`
PosterURL string `gorm:"size:1024" json:"poster_url,omitempty"`
BackdropURL string `gorm:"size:1024" json:"backdrop_url,omitempty"`
Overview string `gorm:"type:text" json:"overview,omitempty"`
Rating float32 `json:"rating"`
Year int `json:"year"`
ReleaseDate string `gorm:"size:10;index:idx_media_library_release,priority:2" json:"release_date,omitempty"`
SeasonNum int `json:"season_num"`
EpisodeNum int `json:"episode_num"`
EpisodeFraction float64 `gorm:"default:0" json:"episode_fraction,omitempty"` // 集号小数部分(S01E11.5 → 0.5);Emby 的 IndexNumber 只能是整数,小数单独存放
ScrapeStatus string `gorm:"size:16;default:pending" json:"scrape_status"`
TMDbID int `json:"tmdb_id"`
BangumiID int `json:"bangumi_id"`
DoubanID string `gorm:"column:douban_id;size:32" json:"douban_id,omitempty"`
TheTVDBID string `gorm:"column:thetvdb_id;size:64" json:"thetvdb_id,omitempty"`
Languages string `gorm:"size:64" json:"languages,omitempty"` // 逗号分隔的 ISO 639-1 代码,如 "zh,en"
Countries string `gorm:"size:128" json:"countries,omitempty"` // 逗号分隔的 ISO 3166-1,如 "CN,US"
Genres string `gorm:"type:text" json:"genres,omitempty"` // 逗号分隔的类型名,如 "Action,Animation"
NSFW bool `gorm:"default:false" json:"nsfw"`
// STRMURL is the indirection target for .strm files: when present the
// stream handler redirects to it instead of opening the local file.
+42
View File
@@ -0,0 +1,42 @@
package model
import "time"
// MediaProbe 是一次 ffprobe 全量探测(容器 / 轨道 / 内嵌章节)的结果缓存。
//
// 它存在的理由:探测一次要 3~4 秒——远端直链更慢,因为要跨洋跑三次 HTTP
// 事务。播放链路绝不能等它,所以第一次播放只起后台任务,结果落库后由后续请求
// 与「跳过片头」的章节数据直接读库。
//
// Payload 刻意只保存裁剪后的字段:ffprobe 原始输出里的 format.filename 是解析
// 后的播放直链(带网盘签名与 pickcode),原样落库等于把可直接下载的链接留在
// 数据库里,所以只保留与技术信息有关的字段。
type MediaProbe struct {
Base
MediaID string `gorm:"uniqueIndex;size:128;not null" json:"media_id"`
// Signature 是「探的是哪个文件」的指纹(哈希):本地文件取路径 + 大小 +
// 修改时间,STRM / 云盘取固化的播放目标。文件换了就说明缓存不再对应当前
// 内容,需要重探。
Signature string `gorm:"size:64" json:"signature,omitempty"`
// Source 记录输入形态:local | strm。
Source string `gorm:"size:16" json:"source,omitempty"`
Container string `gorm:"size:64" json:"container,omitempty"`
DurationSec int `json:"duration_sec"`
BitRate int64 `json:"bit_rate,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
VideoCodec string `gorm:"size:64" json:"video_codec,omitempty"`
AudioCodec string `gorm:"size:64" json:"audio_codec,omitempty"`
VideoStreams int `json:"video_streams"`
AudioStreams int `json:"audio_streams"`
SubtitleStreams int `json:"subtitle_streams"`
ChapterCount int `json:"chapter_count"`
// Payload 是供详情页展示的裁剪后 JSON(容器 + 每路轨道 + 章节)。
Payload string `gorm:"type:text" json:"payload,omitempty"`
// ProbedAt 是最近一次探测的时刻。LastError 非空表示这次探测失败;失败只更新
// 这两个字段,不会覆盖此前成功的 Payload 与已经落库的章节片段。
ProbedAt time.Time `json:"probed_at"`
LastError string `gorm:"size:512" json:"last_error,omitempty"`
}
+1
View File
@@ -40,6 +40,7 @@ func AllModels() []interface{} {
&PlaybackHistory{},
&MediaSegment{},
&MediaSegmentFetch{},
&MediaProbe{},
&Favorite{},
&Playlist{},
&PlaylistItem{},
+2 -2
View File
@@ -37,8 +37,8 @@ type User struct {
// original / simplified / traditional。
SubtitleChineseMode string `gorm:"size:16;not null;default:original" json:"subtitle_chinese_mode"`
// 网页播放器偏好按用户存储,切换媒体对象后继续沿用。
PlayerVolume float64 `gorm:"not null;default:1" json:"player_volume"`
PlayerPlaybackRate float64 `gorm:"not null;default:1" json:"player_playback_rate"`
// 播放倍速不在此列:它只作用于当前视频,不进数据库。
PlayerVolume float64 `gorm:"not null;default:1" json:"player_volume"`
// PlayerVr360GuideSeen 记录用户是否已经看过 VR 全景播放的首次操作说明,
// 按用户保存:看过一次之后不再弹出。
PlayerVr360GuideSeen bool `gorm:"not null;default:false" json:"player_vr360_guide_seen"`
@@ -0,0 +1,61 @@
package repository
import (
"context"
"errors"
"time"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/truewhile/MeBox/internal/model"
)
// MediaProbeRepository 持久化 ffprobe 全量探测的结果缓存。
type MediaProbeRepository struct{ db *gorm.DB }
// Get 返回某媒体的探测缓存,未探测过时返回 (nil, nil)。
func (r *MediaProbeRepository) Get(ctx context.Context, mediaID string) (*model.MediaProbe, error) {
var row model.MediaProbe
err := r.db.WithContext(ctx).
Where("media_id = ?", mediaID).
First(&row).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return &row, nil
}
// Upsert 写入一次成功的探测结果(含裁剪后的媒体信息)。
func (r *MediaProbeRepository) Upsert(ctx context.Context, row *model.MediaProbe) error {
onConflict := clause.OnConflict{
Columns: []clause.Column{{Name: "media_id"}},
DoUpdates: clause.AssignmentColumns([]string{
"signature", "source",
"container", "duration_sec", "bit_rate",
"width", "height", "video_codec", "audio_codec",
"video_streams", "audio_streams", "subtitle_streams", "chapter_count",
"payload", "probed_at", "last_error",
"deleted_at",
}),
}
return r.db.WithContext(ctx).Clauses(onConflict).Create(row).Error
}
// MarkFailure 记录一次失败的探测。它只更新「时间 + 错误信息」,刻意不碰
// payload 与其它字段:一次失败的重探不该把上一次成功拿到的媒体信息抹掉。
func (r *MediaProbeRepository) MarkFailure(ctx context.Context, mediaID, message string, probedAt time.Time) error {
row := model.MediaProbe{MediaID: mediaID, ProbedAt: probedAt, LastError: message}
onConflict := clause.OnConflict{
Columns: []clause.Column{{Name: "media_id"}},
DoUpdates: clause.Assignments(map[string]any{
"probed_at": probedAt,
"last_error": message,
"deleted_at": nil,
}),
}
return r.db.WithContext(ctx).Clauses(onConflict).Create(&row).Error
}
@@ -0,0 +1,105 @@
package repository
import (
"testing"
"time"
"github.com/truewhile/MeBox/internal/model"
)
func TestMediaProbeUpsertReplacesAndGetReturnsLatest(t *testing.T) {
repos := newSegmentTestRepos(t)
ctx := t.Context()
first := &model.MediaProbe{
MediaID: "m-1", Signature: "sig-1", Source: "local",
Container: "matroska,webm", DurationSec: 1451, ChapterCount: 2,
Payload: `{"container":"matroska,webm"}`, ProbedAt: time.Now(),
}
if err := repos.MediaProbe.Upsert(ctx, first); err != nil {
t.Fatalf("upsert #1: %v", err)
}
second := &model.MediaProbe{
MediaID: "m-1", Signature: "sig-2", Source: "strm",
Container: "mp4", DurationSec: 900, ChapterCount: 0,
Payload: `{"container":"mp4"}`, ProbedAt: time.Now(),
}
if err := repos.MediaProbe.Upsert(ctx, second); err != nil {
t.Fatalf("upsert #2: %v", err)
}
got, err := repos.MediaProbe.Get(ctx, "m-1")
if err != nil {
t.Fatal(err)
}
if got == nil {
t.Fatal("probe row missing")
}
if got.Container != "mp4" || got.DurationSec != 900 || got.Signature != "sig-2" || got.Payload != `{"container":"mp4"}` {
t.Fatalf("probe = %#v, want the second upsert to win", got)
}
// 重复探测不能累积重复行:media_id 是唯一索引。
var count int64
if err := repos.DB.Model(&model.MediaProbe{}).Where("media_id = ?", "m-1").Count(&count).Error; err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatalf("rows = %d, want 1", count)
}
}
func TestMediaProbeGetReturnsNilWhenMissing(t *testing.T) {
repos := newSegmentTestRepos(t)
got, err := repos.MediaProbe.Get(t.Context(), "nope")
if err != nil {
t.Fatalf("Get: %v", err)
}
if got != nil {
t.Fatalf("probe = %#v, want nil", got)
}
}
func TestMediaProbeMarkFailureKeepsPreviousPayload(t *testing.T) {
repos := newSegmentTestRepos(t)
ctx := t.Context()
if err := repos.MediaProbe.Upsert(ctx, &model.MediaProbe{
MediaID: "m-1", Container: "matroska,webm", DurationSec: 1451,
ChapterCount: 2, Payload: `{"container":"matroska,webm"}`, ProbedAt: time.Now(),
}); err != nil {
t.Fatal(err)
}
if err := repos.MediaProbe.MarkFailure(ctx, "m-1", "ffprobe full: exit status 1", time.Now()); err != nil {
t.Fatal(err)
}
got, err := repos.MediaProbe.Get(ctx, "m-1")
if err != nil {
t.Fatal(err)
}
if got == nil || got.LastError != "ffprobe full: exit status 1" {
t.Fatalf("probe = %#v, want the recorded failure", got)
}
// 一次失败的重探不该把上一次成功拿到的媒体信息抹掉。
if got.Container != "matroska,webm" || got.DurationSec != 1451 || got.Payload == "" || got.ChapterCount != 2 {
t.Fatalf("a failed re-probe wiped the previous summary: %#v", got)
}
}
func TestMediaProbeMarkFailureInsertsRowWhenAbsent(t *testing.T) {
repos := newSegmentTestRepos(t)
ctx := t.Context()
// 没有历史成功记录时,失败也必须落一行:否则冷却期没有时间戳,
// 每次播放都会重跑一次注定失败的探测。
if err := repos.MediaProbe.MarkFailure(ctx, "m-2", "boom", time.Now()); err != nil {
t.Fatal(err)
}
got, err := repos.MediaProbe.Get(ctx, "m-2")
if err != nil {
t.Fatal(err)
}
if got == nil || got.LastError != "boom" || got.ProbedAt.IsZero() {
t.Fatalf("probe = %#v, want a failure row with a timestamp", got)
}
}
+58
View File
@@ -199,6 +199,64 @@ func (r *MediaRepository) FindByID(ctx context.Context, id string) (*model.Media
return &m, nil
}
// ExistsSiblingWithTMDbID reports whether another row of the same show carries
// the same tm_db_id as m.
//
// 它的用途是把「剧集级 id」和「单集自己的 id」区分开:一部剧的多集共用一个
// 剧集级 id,而单集各自的 id 不会重复。调用方据此决定能否把 Media.TMDbID
// 当作 Series.TMDbID 的替代品(见 MediaSegmentService.queryIDs)。
//
// 同一部剧的判定优先用 series_id;没有 series_id 的行(部分刮削路径不写它)
// 退回到「同一个库 + 同一个标题」。查询失败按「不共用」处理:宁可不查,
// 也不能拿一个可能是单集的 id 去查错片。
func (r *MediaRepository) ExistsSiblingWithTMDbID(ctx context.Context, m *model.Media) bool {
if r == nil || m == nil || m.TMDbID <= 0 || m.ID == "" {
return false
}
query := r.db.WithContext(ctx).Model(&model.Media{}).
Where("tm_db_id = ? AND id <> ?", m.TMDbID, m.ID)
if seriesID := strings.TrimSpace(m.SeriesID); seriesID != "" {
query = query.Where("series_id = ?", seriesID)
} else {
libraryID := strings.TrimSpace(m.LibraryID)
title := strings.TrimSpace(m.Title)
if libraryID == "" || title == "" {
return false
}
query = query.Where("library_id = ? AND title = ?", libraryID, title)
}
var count int64
if err := query.Limit(1).Count(&count).Error; err != nil {
return false
}
return count > 0
}
// ListSeasonSiblings returns other episodes in the same season as m.
// Prefers series_id; falls back to shared library+title+tm_db_id (anime scrape path).
func (r *MediaRepository) ListSeasonSiblings(ctx context.Context, m *model.Media) ([]model.Media, error) {
if r == nil || m == nil || m.SeasonNum <= 0 || m.ID == "" {
return nil, nil
}
query := r.db.WithContext(ctx).Model(&model.Media{}).
Where("season_num = ? AND id <> ?", m.SeasonNum, m.ID)
if seriesID := strings.TrimSpace(m.SeriesID); seriesID != "" {
query = query.Where("series_id = ?", seriesID)
} else {
libraryID := strings.TrimSpace(m.LibraryID)
title := strings.TrimSpace(m.Title)
if libraryID == "" || title == "" || m.TMDbID <= 0 {
return nil, nil
}
query = query.Where("library_id = ? AND title = ? AND tm_db_id = ?", libraryID, title, m.TMDbID)
}
var rows []model.Media
if err := query.Find(&rows).Error; err != nil {
return nil, err
}
return rows, nil
}
// ListByLibrary returns paginated media items for a library.
func (r *MediaRepository) ListByLibrary(ctx context.Context, libraryID string, offset, limit int) ([]model.Media, int64, error) {
return r.ListByLibraryFiltered(ctx, libraryID, offset, limit, MediaQueryFilter{IncludeNSFW: true})
@@ -332,6 +332,11 @@ func addMediaPlacementUpdates(updates map[string]any, existing, incoming model.M
if episodeChanged {
updates["episode_num"] = incoming.EpisodeNum
}
// 半集的小数部分独立于整数集号比较:S01E11.5 重命名/新增/删除时都要落库,
// 否则「11」与「11.5」会被当成同一集折叠。
if existing.EpisodeFraction != incoming.EpisodeFraction {
updates["episode_fraction"] = incoming.EpisodeFraction
}
if strings.TrimSpace(existing.ScrapeStatus) == "no_match" && incoming.ScrapeStatus != "matched" && (seasonChanged || episodeChanged) {
updates["scrape_status"] = "pending"
}
@@ -3,6 +3,7 @@ package repository
import (
"context"
"errors"
"time"
"gorm.io/gorm"
"gorm.io/gorm/clause"
@@ -23,6 +24,18 @@ func (r *MediaSegmentRepository) ListByMedia(ctx context.Context, mediaID string
return rows, err
}
// ListByMediaSource returns the segments contributed by one source only, ordered
// by start. Used to read the ffprobe-extracted chapters independently of the
// community-database rows, so the player can pick between them.
func (r *MediaSegmentRepository) ListByMediaSource(ctx context.Context, mediaID, source string) ([]model.MediaSegment, error) {
rows := make([]model.MediaSegment, 0, 4)
err := r.db.WithContext(ctx).
Where("media_id = ? AND source = ?", mediaID, source).
Order("start_ms asc").
Find(&rows).Error
return rows, err
}
// ReplaceForMedia swaps the segments contributed by one source in a single
// transaction, so a provider refresh can never leave a half-updated set.
//
@@ -72,3 +85,45 @@ func (r *MediaSegmentRepository) UpsertFetch(ctx context.Context, row *model.Med
}
return r.db.WithContext(ctx).Clauses(onConflict).Create(row).Error
}
// ListPrewarmCandidates returns media that should be refreshed from source:
// no ledger, expired miss (fetched_at < missBefore), or expired hit (fetched_at < hitBefore).
// Recently played items come first so hot titles recover coverage sooner.
func (r *MediaSegmentRepository) ListPrewarmCandidates(
ctx context.Context,
source string,
missBefore, hitBefore time.Time,
limit int,
) ([]model.Media, error) {
if r == nil || limit <= 0 {
return nil, nil
}
rows := make([]model.Media, 0, limit)
// 可查询:有 TMDb,且是剧集(有季集)或电影(无季集)。
err := r.db.WithContext(ctx).Raw(`
SELECT m.*
FROM media m
LEFT JOIN media_segment_fetches f
ON f.media_id = m.id AND f.source = ? AND f.deleted_at IS NULL
LEFT JOIN (
SELECT media_id, MAX(updated_at) AS last_played
FROM playback_histories
WHERE deleted_at IS NULL
GROUP BY media_id
) ph ON ph.media_id = m.id
WHERE m.deleted_at IS NULL
AND m.tm_db_id > 0
AND (
(m.season_num > 0 AND m.episode_num > 0)
OR (COALESCE(m.season_num, 0) = 0 AND COALESCE(m.episode_num, 0) = 0)
)
AND (
f.id IS NULL
OR (f.found = 0 AND f.fetched_at < ?)
OR (f.found = 1 AND f.fetched_at < ?)
)
ORDER BY ph.last_played DESC
LIMIT ?
`, source, missBefore, hitBefore, limit).Scan(&rows).Error
return rows, err
}
@@ -152,3 +152,56 @@ func TestUpsertFetchKeepsOneRowPerMediaAndSource(t *testing.T) {
t.Fatalf("ledger should be updated in place, got %#v", got)
}
}
func TestListSeasonSiblingsSharesLibraryTitleTMDb(t *testing.T) {
repos := newSegmentTestRepos(t)
ctx := t.Context()
eps := []*model.Media{
{Base: model.Base{ID: "a"}, LibraryID: "lib", Title: "Show", Path: "/a", SeasonNum: 1, EpisodeNum: 1, TMDbID: 99},
{Base: model.Base{ID: "b"}, LibraryID: "lib", Title: "Show", Path: "/b", SeasonNum: 1, EpisodeNum: 2, TMDbID: 99},
{Base: model.Base{ID: "c"}, LibraryID: "lib", Title: "Show", Path: "/c", SeasonNum: 2, EpisodeNum: 1, TMDbID: 99},
}
for _, ep := range eps {
if err := repos.DB.Create(ep).Error; err != nil {
t.Fatal(err)
}
}
got, err := repos.Media.ListSeasonSiblings(ctx, eps[0])
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].ID != "b" {
t.Fatalf("siblings = %#v, want only same-season ep b", got)
}
}
func TestListPrewarmCandidatesOrdersRecentPlaysFirst(t *testing.T) {
repos := newSegmentTestRepos(t)
ctx := t.Context()
now := time.Now()
for _, m := range []*model.Media{
{Base: model.Base{ID: "cold"}, Path: "/cold", TMDbID: 1},
{Base: model.Base{ID: "hot"}, Path: "/hot", TMDbID: 2},
} {
if err := repos.DB.Create(m).Error; err != nil {
t.Fatal(err)
}
}
if err := repos.DB.Create(&model.PlaybackHistory{
Base: model.Base{ID: "ph1"}, UserID: "u", MediaID: "hot",
}).Error; err != nil {
t.Fatal(err)
}
got, err := repos.MediaSegment.ListPrewarmCandidates(
ctx, "theintrodb", now.Add(-time.Hour), now.Add(-time.Hour), 10,
)
if err != nil {
t.Fatal(err)
}
if len(got) < 2 {
t.Fatalf("candidates = %#v, want both", got)
}
if got[0].ID != "hot" {
t.Fatalf("first = %s, want hot (recently played)", got[0].ID)
}
}
+2
View File
@@ -16,6 +16,7 @@ type Container struct {
Series *SeriesRepository
History *HistoryRepository
MediaSegment *MediaSegmentRepository
MediaProbe *MediaProbeRepository
Favorite *FavoriteRepository
Playlist *PlaylistRepository
Setting *SettingRepository
@@ -47,6 +48,7 @@ func New(db *gorm.DB) *Container {
Series: &SeriesRepository{db: db},
History: &HistoryRepository{db: db},
MediaSegment: &MediaSegmentRepository{db: db},
MediaProbe: &MediaProbeRepository{db: db},
Favorite: &FavoriteRepository{db: db},
Playlist: &PlaylistRepository{db: db},
Setting: &SettingRepository{db: db},
+9 -36
View File
@@ -49,12 +49,6 @@ const (
// dandanplay protocol (search/episodes + comment/{episodeId}) may be used.
const DanmakuDefaultSource = "https://api.dandanplay.net"
const (
minPlayerPlaybackRate = 0.5
maxPlayerPlaybackRate = 3.0
defaultPlayerPlaybackRate = 1.0
)
// danmakuOfficialBase is where identification (/api/v2/match) and the
// comment/search fallback always go, regardless of the configured source.
// A package var (not a const) so tests can point it at a local server.
@@ -69,8 +63,6 @@ type DanmakuRenderConfig struct {
FontSize string `json:"font_size"`
Area string `json:"area"`
Volume float64 `json:"volume"`
// PlaybackRate 是当前用户的播放倍速偏好(按用户存储)。
PlaybackRate float64 `json:"playback_rate"`
// Vr360GuideSeen 表示当前用户是否已经看过 VR 全景播放的首次操作说明。
Vr360GuideSeen bool `json:"vr360_guide_seen"`
// MergeSources 是当前用户的弹幕合并偏好(按用户存储)。
@@ -99,7 +91,6 @@ type DanmakuSettingsPatch struct {
Area *float64 `json:"area"`
MergeSources *bool `json:"merge_sources"`
Volume *float64 `json:"volume"`
PlaybackRate *float64 `json:"playback_rate"`
// Vr360GuideSeen 标记 VR 全景播放的首次操作说明是否已经看过。
Vr360GuideSeen *bool `json:"vr360_guide_seen"`
}
@@ -358,11 +349,10 @@ func cloneDanmakuAnimeList(in []DanmakuAnime) []DanmakuAnime {
// Config reads danmaku settings from the runtime settings table.
func (s *DanmakuService) Config(ctx context.Context) DanmakuRenderConfig {
cfg := DanmakuRenderConfig{
Opacity: "1",
FontSize: "24",
Area: "1",
Volume: 1,
PlaybackRate: defaultPlayerPlaybackRate,
Opacity: "1",
FontSize: "24",
Area: "1",
Volume: 1,
}
if s == nil || s.repo == nil || s.repo.Setting == nil {
return cfg
@@ -414,12 +404,11 @@ func (s *DanmakuService) findUser(ctx context.Context, userID string) (*model.Us
func danmakuConfigFromUser(user *model.User) DanmakuRenderConfig {
if user == nil {
return DanmakuRenderConfig{
Enabled: true,
Opacity: "1",
FontSize: "24",
Area: "1",
Volume: 1,
PlaybackRate: defaultPlayerPlaybackRate,
Enabled: true,
Opacity: "1",
FontSize: "24",
Area: "1",
Volume: 1,
}
}
opacity := user.DanmakuOpacity
@@ -438,10 +427,6 @@ func danmakuConfigFromUser(user *model.User) DanmakuRenderConfig {
if volume < 0 || volume > 1 {
volume = 1
}
playbackRate := user.PlayerPlaybackRate
if playbackRate < minPlayerPlaybackRate || playbackRate > maxPlayerPlaybackRate {
playbackRate = defaultPlayerPlaybackRate
}
return DanmakuRenderConfig{
Enabled: user.DanmakuEnabled,
Source: strings.TrimSpace(user.DanmakuSource),
@@ -450,7 +435,6 @@ func danmakuConfigFromUser(user *model.User) DanmakuRenderConfig {
FontSize: strconv.Itoa(fontSize),
Area: strconv.FormatFloat(area, 'f', -1, 64),
Volume: volume,
PlaybackRate: playbackRate,
Vr360GuideSeen: user.PlayerVr360GuideSeen,
MergeSources: user.DanmakuMergeSources,
AppKeyConfigured: strings.TrimSpace(user.DanmakuAppKey) != "",
@@ -522,17 +506,6 @@ func (s *DanmakuService) UpdateUserSettings(ctx context.Context, userID string,
}
updates["player_volume"] = *patch.Volume
}
if patch.PlaybackRate != nil {
if *patch.PlaybackRate < minPlayerPlaybackRate || *patch.PlaybackRate > maxPlayerPlaybackRate {
return DanmakuRenderConfig{}, fmt.Errorf(
"%w: playback_rate must be between %.2f and %.2f",
ErrInvalidDanmakuSettings,
minPlayerPlaybackRate,
maxPlayerPlaybackRate,
)
}
updates["player_playback_rate"] = *patch.PlaybackRate
}
if patch.Vr360GuideSeen != nil {
updates["player_vr360_guide_seen"] = *patch.Vr360GuideSeen
}
-2
View File
@@ -422,7 +422,6 @@ func TestDanmakuFetchUsesPerUserSourceAndCredentials(t *testing.T) {
user.DanmakuFontSize = 30
user.DanmakuArea = 0.7
user.PlayerVolume = 0.42
user.PlayerPlaybackRate = 1.25
require.NoError(t, svc.repo.User.Create(ctx, &user))
seedDanmakuMedia(t, svc, "per-user-media", "测试动画", "", 0)
@@ -431,7 +430,6 @@ func TestDanmakuFetchUsesPerUserSourceAndCredentials(t *testing.T) {
require.Contains(t, res.Raw, "弹幕A")
require.Equal(t, srv.URL(), res.Source)
require.Equal(t, 0.42, res.Volume)
require.Equal(t, 1.25, res.PlaybackRate)
require.Equal(t, "0.65", res.Opacity)
require.Equal(t, "30", res.FontSize)
require.Equal(t, srv.lastHeaders.Get("X-AppId"), "user-app-id")
+143 -1
View File
@@ -6,6 +6,8 @@ import (
"encoding/hex"
"strings"
"time"
"github.com/truewhile/MeBox/internal/model"
)
// Emby 发现类接口:NextUp / Similar / Genres。
@@ -21,7 +23,8 @@ const (
)
// NextUp 返回「每部在看的剧的下一集」,即 Emby 客户端首页「接下来播放」的数据源。
func (e *EmbyService) NextUp(ctx context.Context, userID string, limit int) (map[string]any, error) {
// seriesID 非空时只返回该剧的下一集(剧集详情页「继续播放」);空则返回全站列表。
func (e *EmbyService) NextUp(ctx context.Context, userID, seriesID string, limit int) (map[string]any, error) {
if limit <= 0 {
limit = embyNextUpDefaultLimit
}
@@ -31,11 +34,16 @@ func (e *EmbyService) NextUp(ctx context.Context, userID string, limit int) (map
if strings.TrimSpace(userID) == "" {
return emptyItemsEnvelope(0), nil
}
seriesID = strings.TrimSpace(seriesID)
discovery := e.discoveryService()
if discovery == nil {
return emptyItemsEnvelope(0), nil
}
if seriesID != "" {
return e.nextUpForSeries(ctx, userID, seriesID, limit)
}
rows, err := discovery.NextUpCandidates(ctx, userID, limit, e.mediaVisibility(ctx, userID))
if err != nil {
return nil, err
@@ -52,6 +60,140 @@ func (e *EmbyService) NextUp(ctx context.Context, userID string, limit int) (map
}, nil
}
// nextUpForSeries 只解析指定剧的下一集。远程挂载剧集按本机播放历史 + 远程
// 分集列表计算,避免把其它本地剧的 NextUp 塞进详情页继续播放按钮。
func (e *EmbyService) nextUpForSeries(ctx context.Context, userID, seriesID string, limit int) (map[string]any, error) {
if IsEmbyRemoteID(seriesID) {
return e.nextUpForRemoteSeries(ctx, userID, seriesID, limit)
}
discovery := e.discoveryService()
if discovery == nil {
return emptyItemsEnvelope(0), nil
}
// 多取候选再按 SeriesId 精确过滤,避免「全站 TopN」把目标剧挤掉。
scanLimit := embyNextUpMaxLimit
if limit > scanLimit {
scanLimit = limit
}
rows, err := discovery.NextUpCandidates(ctx, userID, scanLimit, e.mediaVisibility(ctx, userID))
if err != nil {
return nil, err
}
items, err := e.payloadsForMedia(ctx, rows, userID)
if err != nil {
return nil, err
}
filtered := make([]map[string]any, 0, 1)
for _, item := range items {
itemSeries, _ := item["SeriesId"].(string)
if itemSeries == seriesID {
filtered = append(filtered, item)
if len(filtered) >= limit {
break
}
}
}
return map[string]any{
"Items": filtered,
"TotalRecordCount": int64(len(filtered)),
}, nil
}
// nextUpForRemoteSeries 用 MeBox 本地播放历史在远程剧的分集里找「下一集」。
// 不透传远程账号的 NextUp,避免多用户共用挂载账号时串进度。
func (e *EmbyService) nextUpForRemoteSeries(ctx context.Context, userID, seriesID string, limit int) (map[string]any, error) {
if e == nil || e.remote == nil || strings.TrimSpace(userID) == "" {
return emptyItemsEnvelope(0), nil
}
if limit <= 0 {
limit = 1
}
mountID, remoteSeriesID, ok := DecodeEmbyRemoteID(seriesID)
if !ok {
return emptyItemsEnvelope(0), nil
}
mount, acct, err := e.remote.ResolveMount(ctx, mountID)
if err != nil || mount == nil || acct == nil {
return emptyItemsEnvelope(0), nil
}
if !EmbyMountLibraryAllowed(e.mediaVisibility(ctx, userID), mount) {
return emptyItemsEnvelope(0), nil
}
prefix := EmbyRemoteIDPrefix + mountID + "~"
var hist []model.PlaybackHistory
if err := e.repo.DB.WithContext(ctx).
Where("user_id = ? AND position_ms > 0 AND media_id LIKE ?", userID, prefix+"%").
Order("watched_at desc").
Limit(nextUpHistoryScanLimit).
Find(&hist).Error; err != nil {
return nil, err
}
if len(hist) == 0 {
return emptyItemsEnvelope(0), nil
}
episodes, err := e.remote.RemoteEpisodes(ctx, mount, acct, remoteSeriesID)
if err != nil || len(episodes) == 0 {
return emptyItemsEnvelope(0), nil
}
epByID := make(map[string]*model.Media, len(episodes))
for i := range episodes {
epByID[episodes[i].ID] = &episodes[i]
}
var current *model.Media
currentCompleted := false
for i := range hist {
if m := epByID[hist[i].MediaID]; m != nil {
current = m
currentCompleted = hist[i].Completed
break
}
}
if current == nil {
return emptyItemsEnvelope(0), nil
}
completed := map[string]bool{}
epIDs := make([]string, 0, len(episodes))
for i := range episodes {
epIDs = append(epIDs, episodes[i].ID)
}
var done []model.PlaybackHistory
if err := e.repo.DB.WithContext(ctx).
Where("user_id = ? AND completed = ? AND media_id IN ?", userID, true, epIDs).
Find(&done).Error; err == nil {
for _, h := range done {
completed[h.MediaID] = true
}
}
next, ok := pickNextEpisode(episodes, current, currentCompleted, completed)
if !ok {
return emptyItemsEnvelope(0), nil
}
_, remoteEpID, ok := DecodeEmbyRemoteID(next.ID)
if !ok {
return emptyItemsEnvelope(0), nil
}
item, err := e.remote.RemoteItem(ctx, mount, acct, remoteEpID)
if err != nil || item == nil {
return emptyItemsEnvelope(0), nil
}
if err := e.mergeRemoteUserData(ctx, userID, item); err != nil {
return nil, err
}
items := []map[string]any{item}
if limit < len(items) {
items = items[:limit]
}
return map[string]any{
"Items": items,
"TotalRecordCount": int64(len(items)),
}, nil
}
// SimilarItems 返回与指定条目相似的本地媒体。
//
// 找不到条目(或该条目对当前用户不可见)时返回空列表而不是错误:客户端会在
+47 -5
View File
@@ -299,8 +299,14 @@ func embyLatestSeriesRowLimit(limit int) int {
}
// ResumeItems 列出有未完成播放进度的媒体。
func (e *EmbyService) ResumeItems(ctx context.Context, userID string, limit int) (map[string]any, error) {
return e.resumableItems(ctx, ItemsParams{UserID: userID, Limit: limit})
// parentID 非空时收窄到该库 / 该剧(含虚拟 msgo-series-* ID)。
func (e *EmbyService) ResumeItems(ctx context.Context, userID, parentID string, limit, startIndex int) (map[string]any, error) {
return e.resumableItems(ctx, ItemsParams{
UserID: userID,
ParentID: strings.TrimSpace(parentID),
Limit: limit,
StartIndex: startIndex,
})
}
// favoriteItems returns favourited media for Emby clients, including mounted
@@ -434,6 +440,30 @@ func favoriteMatchesParent(ctx context.Context, e *EmbyService, parentID, mediaI
return wantMountID != "" && gotMountID == wantMountID
}
// resumeMatchesParent 判断续播条目是否属于 ParentId / SeriesId 作用域。
// 本地剧集的 series_id 常为空,实际对外 ID 是 msgo-series-* 虚拟 ID,必须用
// seriesIDForMedia 对齐,否则按剧收窄永远匹配不上。
func resumeMatchesParent(ctx context.Context, e *EmbyService, parentID, libraryID, seriesID string, m *model.Media) bool {
if parentID == "" {
return true
}
if libraryID == parentID || seriesID == parentID {
return true
}
if m != nil && e.seriesIDForMedia(ctx, m) == parentID {
return true
}
if m != nil && e.seasonIDForMedia(ctx, m) == parentID {
return true
}
for _, id := range e.mergedLibraryIDs(ctx, parentID) {
if id == libraryID {
return true
}
}
return false
}
// resumableItems 返回未完成播放进度的媒体(包含本地媒体与挂载的远程媒体),支持分页。
func (e *EmbyService) resumableItems(ctx context.Context, p ItemsParams) (map[string]any, error) {
if p.Limit <= 0 || p.Limit > 100 {
@@ -500,7 +530,7 @@ func (e *EmbyService) resumableItems(ctx context.Context, p ItemsParams) (map[st
localTotal, remoteTotal := 0, 0
for _, h := range hist {
if m, ok := byID[h.MediaID]; ok {
if p.ParentID != "" && m.LibraryID != p.ParentID && m.SeriesID != p.ParentID {
if p.ParentID != "" && !resumeMatchesParent(ctx, e, p.ParentID, m.LibraryID, m.SeriesID, m) {
continue
}
localTotal++
@@ -652,9 +682,21 @@ func (e *EmbyService) itemPayload(ctx context.Context, m *model.Media, fav bool,
seasonID = e.seasonIDForMedia(ctx, m)
parentID = seasonID
episodeTitle := strings.TrimSpace(m.EpisodeTitle)
if episodeTitle != "" {
// 半集(S01E11.5)在 Emby 协议里只能占用整数 IndexNumber 11,标题里
// 不再带上小数就会和真正的第 11 集完全分不清,所以显式补上集号。
halfLabel := ""
if m.EpisodeFraction > 0 && m.EpisodeNum > 0 {
halfLabel = fmt.Sprintf("第 %s 集", FormatEpisodeNumber(m.EpisodeNum, m.EpisodeFraction))
}
switch {
case episodeTitle != "":
name = episodeTitle
} else if m.EpisodeNum > 0 {
if halfLabel != "" && !strings.HasPrefix(episodeTitle, halfLabel) {
name = halfLabel + " · " + episodeTitle
}
case halfLabel != "":
name = halfLabel
case m.EpisodeNum > 0:
name = fmt.Sprintf("第 %d 集", m.EpisodeNum)
}
}
+7
View File
@@ -153,8 +153,15 @@ func (e *EmbyService) episodeItems(ctx context.Context, rows []model.Media, p It
if rows[i].EpisodeNum != rows[j].EpisodeNum {
return rows[i].EpisodeNum < rows[j].EpisodeNum
}
if rows[i].EpisodeFraction != rows[j].EpisodeFraction {
return rows[i].EpisodeFraction < rows[j].EpisodeFraction
}
return rows[i].CreatedAt.Before(rows[j].CreatedAt)
})
// 先折叠同集的多个版本再统计总数与分页,与 payloadsForMedia 内部保持同一步骤。
// 若按未折叠的行数报 TotalRecordCount(例如 S01E11 与 S01E11.5 两行折叠成一条),
// 客户端会认为还有一条没取到,反复请求下一页 —— 分集列表就会一直加载不出来。
rows = e.collapseMediaVersionRows(ctx, rows)
total := len(rows)
items, err := e.payloadsForMedia(ctx, pageSlice(rows, p.StartIndex, p.Limit), p.UserID)
if err != nil {
+10 -6
View File
@@ -33,7 +33,7 @@ func (e *EmbyService) mediaVersionSiblings(ctx context.Context, m *model.Media)
}
q := e.repo.DB.WithContext(ctx).Model(&model.Media{}).
Where("library_id IN ?", libraryIDs).
Where("season_num = ? AND episode_num = ?", m.SeasonNum, m.EpisodeNum)
Where("season_num = ? AND episode_num = ? AND episode_fraction = ?", m.SeasonNum, m.EpisodeNum, m.EpisodeFraction)
if m.TMDbID > 0 {
q = q.Where("tm_db_id = ?", m.TMDbID)
} else if m.BangumiID > 0 {
@@ -110,11 +110,15 @@ func (e *EmbyService) mediaVersionKey(ctx context.Context, m *model.Media) strin
}
kind := mediaSpecialKind(m.Path)
season, episode := m.SeasonNum, m.EpisodeNum
fraction := m.EpisodeFraction
if kind != "" && kind != mediaSpecialTheatrical && episode <= 0 {
if parsedSeason, parsedEpisode := ParseEpisode(m.Path); parsedEpisode > 0 {
season, episode = parsedSeason, parsedEpisode
if parsedSeason, parsedEpisode, parsedFraction := ParseEpisodeParts(m.Path); parsedEpisode > 0 {
season, episode, fraction = parsedSeason, parsedEpisode, parsedFraction
}
}
// 集号后缀让 S01E11.5 与 S01E11 属于不同版本组;没有小数时后缀为空,
// 键与历史完全一致。
fractionSuffix := episodeFractionSuffix(fraction)
kindKey := ""
if kind != "" {
kindKey = "|kind:" + kind
@@ -126,10 +130,10 @@ func (e *EmbyService) mediaVersionKey(ctx context.Context, m *model.Media) strin
return libraryGroup + "|special-item:" + kind + "|id:" + m.ID
}
if m.TMDbID > 0 {
return fmt.Sprintf("%s|tmdb:%d|s:%d|e:%d%s", libraryGroup, m.TMDbID, season, episode, kindKey)
return fmt.Sprintf("%s|tmdb:%d|s:%d|e:%d%s%s", libraryGroup, m.TMDbID, season, episode, fractionSuffix, kindKey)
}
if m.BangumiID > 0 {
return fmt.Sprintf("%s|bangumi:%d|s:%d|e:%d%s", libraryGroup, m.BangumiID, season, episode, kindKey)
return fmt.Sprintf("%s|bangumi:%d|s:%d|e:%d%s%s", libraryGroup, m.BangumiID, season, episode, fractionSuffix, kindKey)
}
title := strings.ToLower(strings.TrimSpace(m.Title))
if title == "" {
@@ -138,7 +142,7 @@ func (e *EmbyService) mediaVersionKey(ctx context.Context, m *model.Media) strin
if title == "" {
return ""
}
return fmt.Sprintf("%s|title:%s|y:%d|s:%d|e:%d%s", libraryGroup, title, m.Year, season, episode, kindKey)
return fmt.Sprintf("%s|title:%s|y:%d|s:%d|e:%d%s%s", libraryGroup, title, m.Year, season, episode, fractionSuffix, kindKey)
}
func preferMediaVersion(candidate, current model.Media) bool {
+240
View File
@@ -0,0 +1,240 @@
package service
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"go.uber.org/zap"
"github.com/truewhile/MeBox/internal/config"
"github.com/truewhile/MeBox/internal/model"
"github.com/truewhile/MeBox/internal/repository"
)
// 回归:挂载的远程 Emby 剧集里,只看了几秒就退出的那一集必须仍是 NextUp 的结果。
// 客户端(Yamby 等)剧集详情页的「继续播放」直接取 NextUp 第一条,跳集会播错集。
func TestMountedRemoteNextUpKeepsPartiallyWatchedEpisode(t *testing.T) {
episode := func(id string, index int) map[string]any {
return map[string]any{
"Id": id,
"Name": "第" + strconv.Itoa(index) + "集",
"Type": "Episode",
"SeriesId": "series-100",
"ParentIndexNumber": 1,
"IndexNumber": index,
"RunTimeTicks": 14400640000,
}
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/emby/Users/uid-1/Items/series-100":
_ = json.NewEncoder(w).Encode(map[string]any{"Id": "series-100", "Name": "剧一", "Type": "Series"})
case "/emby/Users/uid-1/Items/ep-2":
_ = json.NewEncoder(w).Encode(episode("ep-2", 2))
case "/emby/Users/uid-1/Items":
q := r.URL.Query()
if q.Get("IncludeItemTypes") != "Episode" || q.Get("ParentId") != "series-100" {
w.WriteHeader(http.StatusBadRequest)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"TotalRecordCount": 3,
"Items": []map[string]any{
episode("ep-1", 1),
episode("ep-2", 2),
episode("ep-3", 3),
},
})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
db := newServiceTestDB(t, &model.StrmAccount{}, &model.EmbyMount{}, &model.PlaybackHistory{}, &model.User{})
repos := repository.New(db)
cfg := &config.Config{}
remote := NewEmbyRemoteService(cfg, zap.NewNop(), repos, NewCryptoService("", zap.NewNop()))
svc := NewEmbyService(cfg, zap.NewNop(), repos).SetEmbyRemote(remote)
rawConfig, _ := json.Marshal(map[string]string{
"url": server.URL,
"api_key": "test-api-key",
"remote_user_id": "uid-1",
})
acct := &model.StrmAccount{
Base: model.Base{ID: "acct-1"},
Name: "远程 Emby",
Provider: model.StrmProviderEmbyRemote,
Config: string(rawConfig),
Enabled: true,
}
if err := repos.StrmAccount.Create(t.Context(), acct); err != nil {
t.Fatalf("create account: %v", err)
}
mount := &model.EmbyMount{
Base: model.Base{ID: "mount-1"},
AccountID: acct.ID,
RemoteViewID: "view-1",
RemoteViewName: "新番连载",
CollectionType: "tvshows",
Enabled: true,
}
if err := repos.EmbyMount.Create(t.Context(), mount); err != nil {
t.Fatalf("create mount: %v", err)
}
user := &model.User{
Base: model.Base{ID: "user-1"},
Username: "viewer",
PasswordHash: "x",
Role: "user",
Tier: "free",
IsActive: true,
}
if err := repos.User.Create(t.Context(), user); err != nil {
t.Fatalf("create user: %v", err)
}
// 第 2 集只播了 3.5 秒就退出:有进度、未标记看完。
if err := repos.DB.Create(&model.PlaybackHistory{
UserID: user.ID,
MediaID: EncodeEmbyRemoteID(mount.ID, "ep-2"),
PositionMs: 3582,
DurationMs: 1440064,
WatchedAt: time.Now(),
Completed: false,
}).Error; err != nil {
t.Fatalf("create history: %v", err)
}
envelope, err := svc.NextUp(t.Context(), user.ID, EncodeEmbyRemoteID(mount.ID, "series-100"), 1)
if err != nil {
t.Fatalf("NextUp: %v", err)
}
items, _ := envelope["Items"].([]map[string]any)
if len(items) != 1 {
t.Fatalf("items = %d, want 1 (%#v)", len(items), envelope)
}
wantID := EncodeEmbyRemoteID(mount.ID, "ep-2")
if id, _ := items[0]["Id"].(string); id != wantID {
t.Fatalf("Id = %q, want %q (未看完的那一集不能被跳过)", id, wantID)
}
userData, _ := items[0]["UserData"].(map[string]any)
if ticks, _ := userData["PlaybackPositionTicks"].(int64); ticks != 35820000 {
t.Fatalf("PlaybackPositionTicks = %#v, want 35820000 (详情页要能续播到 3.5 秒)", userData["PlaybackPositionTicks"])
}
}
// 远程剧集已看完当前一集时,NextUp 仍要指向下一集。
func TestMountedRemoteNextUpAfterCompletedEpisode(t *testing.T) {
episode := func(id string, index int) map[string]any {
return map[string]any{
"Id": id,
"Name": "第" + strconv.Itoa(index) + "集",
"Type": "Episode",
"SeriesId": "series-100",
"ParentIndexNumber": 1,
"IndexNumber": index,
"RunTimeTicks": 14400640000,
}
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/emby/Users/uid-1/Items/series-100":
_ = json.NewEncoder(w).Encode(map[string]any{"Id": "series-100", "Name": "剧一", "Type": "Series"})
case "/emby/Users/uid-1/Items/ep-3":
_ = json.NewEncoder(w).Encode(episode("ep-3", 3))
case "/emby/Users/uid-1/Items":
_ = json.NewEncoder(w).Encode(map[string]any{
"TotalRecordCount": 3,
"Items": []map[string]any{
episode("ep-1", 1),
episode("ep-2", 2),
episode("ep-3", 3),
},
})
default:
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
db := newServiceTestDB(t, &model.StrmAccount{}, &model.EmbyMount{}, &model.PlaybackHistory{}, &model.User{})
repos := repository.New(db)
cfg := &config.Config{}
remote := NewEmbyRemoteService(cfg, zap.NewNop(), repos, NewCryptoService("", zap.NewNop()))
svc := NewEmbyService(cfg, zap.NewNop(), repos).SetEmbyRemote(remote)
rawConfig, _ := json.Marshal(map[string]string{
"url": server.URL,
"api_key": "test-api-key",
"remote_user_id": "uid-1",
})
acct := &model.StrmAccount{
Base: model.Base{ID: "acct-1"},
Name: "远程 Emby",
Provider: model.StrmProviderEmbyRemote,
Config: string(rawConfig),
Enabled: true,
}
if err := repos.StrmAccount.Create(t.Context(), acct); err != nil {
t.Fatalf("create account: %v", err)
}
mount := &model.EmbyMount{
Base: model.Base{ID: "mount-1"},
AccountID: acct.ID,
RemoteViewID: "view-1",
RemoteViewName: "新番连载",
CollectionType: "tvshows",
Enabled: true,
}
if err := repos.EmbyMount.Create(t.Context(), mount); err != nil {
t.Fatalf("create mount: %v", err)
}
user := &model.User{
Base: model.Base{ID: "user-1"},
Username: "viewer",
PasswordHash: "x",
Role: "user",
Tier: "free",
IsActive: true,
}
if err := repos.User.Create(t.Context(), user); err != nil {
t.Fatalf("create user: %v", err)
}
// 第 2 集已整集看完。
if err := repos.DB.Create(&model.PlaybackHistory{
UserID: user.ID,
MediaID: EncodeEmbyRemoteID(mount.ID, "ep-2"),
PositionMs: 1440064,
DurationMs: 1440064,
WatchedAt: time.Now(),
Completed: true,
}).Error; err != nil {
t.Fatalf("create history: %v", err)
}
envelope, err := svc.NextUp(context.Background(), user.ID, EncodeEmbyRemoteID(mount.ID, "series-100"), 1)
if err != nil {
t.Fatalf("NextUp: %v", err)
}
items, _ := envelope["Items"].([]map[string]any)
if len(items) != 1 {
t.Fatalf("items = %d, want 1 (%#v)", len(items), envelope)
}
if id, _ := items[0]["Id"].(string); id != EncodeEmbyRemoteID(mount.ID, "ep-3") {
t.Fatalf("Id = %q, want ep-3", id)
}
}
+39 -36
View File
@@ -55,7 +55,7 @@ func (e *EmbyService) findSeriesGroup(ctx context.Context, id, userID string) (e
// 有 series_id 时分组 key 就是 series_id 本身(UUID,不带虚拟前缀)。
q = q.Where("series_id IS NULL OR series_id = ''")
}
if err := q.Order("media.season_num asc, media.episode_num asc, media.created_at asc").Limit(embySeriesGroupingLimit).Find(&rows).Error; err != nil {
if err := q.Order("media.season_num asc, media.episode_num asc, media.episode_fraction asc, media.created_at asc").Limit(embySeriesGroupingLimit).Find(&rows).Error; err != nil {
return embySeriesGroup{}, false, err
}
for _, group := range e.seriesGroupsFromMedia(ctx, rows) {
@@ -68,20 +68,20 @@ func (e *EmbyService) findSeriesGroup(ctx context.Context, id, userID string) (e
if series, err := e.repo.Series.FindByID(ctx, id); err != nil {
return embySeriesGroup{}, false, err
} else if series != nil {
return embySeriesGroup{
ID: series.ID,
LibraryID: series.LibraryID,
Name: series.Title,
PosterURL: series.PosterURL,
BackdropURL: series.BackdropURL,
Overview: series.Overview,
Rating: series.Rating,
Year: series.Year,
TMDbID: series.TMDbID,
BangumiID: series.BangumiID,
CreatedAt: series.CreatedAt,
DateLastMediaAdded: series.CreatedAt,
}, true, nil
return embySeriesGroup{
ID: series.ID,
LibraryID: series.LibraryID,
Name: series.Title,
PosterURL: series.PosterURL,
BackdropURL: series.BackdropURL,
Overview: series.Overview,
Rating: series.Rating,
Year: series.Year,
TMDbID: series.TMDbID,
BangumiID: series.BangumiID,
CreatedAt: series.CreatedAt,
DateLastMediaAdded: series.CreatedAt,
}, true, nil
}
}
return embySeriesGroup{}, false, nil
@@ -108,7 +108,7 @@ func (e *EmbyService) findSeasonGroup(ctx context.Context, id, userID string) (e
Where("(series_id IS NULL OR series_id = '') AND (season_num > 0 OR episode_num > 0)")
q = e.applyUserMediaVisibility(ctx, q, userID)
if err := q.
Order("media.season_num asc, media.episode_num asc, media.created_at asc").
Order("media.season_num asc, media.episode_num asc, media.episode_fraction asc, media.created_at asc").
Limit(embySeriesGroupingLimit).
Find(&rows).Error; err != nil {
return embySeasonGroup{}, false, err
@@ -167,7 +167,7 @@ func (e *EmbyService) seasonGroupForSeries(ctx context.Context, id, seriesID, us
Where("series_id = ? AND (season_num > 0 OR episode_num > 0)", seriesID)
rq = e.applyUserMediaVisibility(ctx, rq, userID)
if err := rq.
Order("media.season_num asc, media.episode_num asc, media.created_at asc").
Order("media.season_num asc, media.episode_num asc, media.episode_fraction asc, media.created_at asc").
Limit(embySeriesGroupingLimit).
Find(&rows).Error; err != nil {
return embySeasonGroup{}, false, err
@@ -194,26 +194,26 @@ func (e *EmbyService) seriesGroupsFromMedia(ctx context.Context, rows []model.Me
seriesID := e.seriesIDForMedia(ctx, &row)
group, ok := byID[seriesID]
if !ok {
group = &embySeriesGroup{
ID: seriesID,
LibraryID: row.LibraryID,
Name: e.seriesNameForMedia(ctx, &row),
Year: row.Year,
ReleaseDate: row.ReleaseDate,
TMDbID: row.TMDbID,
BangumiID: row.BangumiID,
CreatedAt: row.CreatedAt,
DateLastMediaAdded: row.CreatedAt,
}
byID[seriesID] = group
order = append(order, seriesID)
}
if row.CreatedAt.Before(group.CreatedAt) || group.CreatedAt.IsZero() {
group.CreatedAt = row.CreatedAt
}
if row.CreatedAt.After(group.DateLastMediaAdded) {
group.DateLastMediaAdded = row.CreatedAt
group = &embySeriesGroup{
ID: seriesID,
LibraryID: row.LibraryID,
Name: e.seriesNameForMedia(ctx, &row),
Year: row.Year,
ReleaseDate: row.ReleaseDate,
TMDbID: row.TMDbID,
BangumiID: row.BangumiID,
CreatedAt: row.CreatedAt,
DateLastMediaAdded: row.CreatedAt,
}
byID[seriesID] = group
order = append(order, seriesID)
}
if row.CreatedAt.Before(group.CreatedAt) || group.CreatedAt.IsZero() {
group.CreatedAt = row.CreatedAt
}
if row.CreatedAt.After(group.DateLastMediaAdded) {
group.DateLastMediaAdded = row.CreatedAt
}
if strings.TrimSpace(row.ReleaseDate) != "" && mediaReleaseSortTime(row).After(embySeriesReleaseSortTime(*group)) {
group.ReleaseDate = row.ReleaseDate
if row.Year > 0 {
@@ -249,6 +249,9 @@ func (e *EmbyService) seriesGroupsFromMedia(ctx context.Context, rows []model.Me
if group.Episodes[i].EpisodeNum != group.Episodes[j].EpisodeNum {
return group.Episodes[i].EpisodeNum < group.Episodes[j].EpisodeNum
}
if group.Episodes[i].EpisodeFraction != group.Episodes[j].EpisodeFraction {
return group.Episodes[i].EpisodeFraction < group.Episodes[j].EpisodeFraction
}
return group.Episodes[i].CreatedAt.Before(group.Episodes[j].CreatedAt)
})
groups = append(groups, group)
+78 -3
View File
@@ -28,9 +28,13 @@ import (
)
var (
patSEnE = regexp.MustCompile(`(?i)s(\d{1,2})e(\d{1,3})`)
patSEnERange = regexp.MustCompile(`(?i)s(\d{1,2})e(\d{1,3})\s*[-~–—]\s*(?:s(\d{1,2}))?e?(\d{1,3})(?:[^0-9]|$)`)
patDanglingSE = regexp.MustCompile(`(?i)(?:^|[\s._-])s\d{1,2}e(?:[\s._-]|$)`)
patSEnE = regexp.MustCompile(`(?i)s(\d{1,2})e(\d{1,3})`)
// patSEnEFraction 捕获 S01E11.5 这类「半集」写法的小数位:只在 SxxEyy 后
// 紧跟一个点加一位数字时成立;解析时还要确认该数字后面不是另一个数字,
// 否则 S01E11.1080p 的分辨率会被误读成小数集号。
patSEnEFraction = regexp.MustCompile(`(?i)s(\d{1,2})e(\d{1,3})\.(\d)`)
patSEnERange = regexp.MustCompile(`(?i)s(\d{1,2})e(\d{1,3})\s*[-~–—]\s*(?:s(\d{1,2}))?e?(\d{1,3})(?:[^0-9]|$)`)
patDanglingSE = regexp.MustCompile(`(?i)(?:^|[\s._-])s\d{1,2}e(?:[\s._-]|$)`)
// patNxE 匹配 1x02 这类季集写法的捕获组,同时用于从标题里剔除季集残留
// (ReplaceAllString),因此本身不带边界守卫。解析时改用 patNxEGuarded,
// 避免 "1920x1080" 被从中间匹配出 "20x108" 而误判成 S20E108。
@@ -123,6 +127,62 @@ func ParseEpisode(path string) (season, episode int) {
return 0, 0
}
// ParseEpisodeParts 在 ParseEpisode 的基础上返回集号的小数部分:
// S01E11.5 → (1, 11, 0.5)。小数部分用来把「半集」和同季的整集区分开
// (折叠/身份键、排序、网页端显示集号都带上它),解析不出来时为 0。
func ParseEpisodeParts(path string) (season, episode int, fraction float64) {
name := mediaSidecarBase(path)
if name == "" {
name = strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
}
if m := patSEnE.FindStringSubmatch(name); len(m) == 3 {
return mustAtoi(m[1]), mustAtoi(m[2]), episodeFractionFromName(name)
}
season, episode = ParseEpisode(path)
return season, episode, 0
}
// episodeFractionFromName 从 "S01E11.5" 这类名字里取出小数部分(0.5)。
// 只在 SxxEyy 后紧跟「点 + 一位数字」、且该数字之后不是数字时才成立,
// 否则 S01E11.1080p / S01E05.10bit 会被读成小数集号。
func episodeFractionFromName(name string) float64 {
lower := strings.ToLower(name)
m := patSEnEFraction.FindStringSubmatchIndex(lower)
if len(m) != 8 {
return 0
}
digitsStart, digitsEnd := m[6], m[7]
if digitsStart < 0 || digitsEnd <= digitsStart {
return 0
}
if digitsEnd < len(lower) && lower[digitsEnd] >= '0' && lower[digitsEnd] <= '9' {
return 0
}
value, err := strconv.ParseFloat("0."+lower[digitsStart:digitsEnd], 64)
if err != nil || value <= 0 || value >= 1 {
return 0
}
return value
}
// episodeFractionSuffix 把小数部分渲染成身份/显示后缀:0.5 → ".5",无小数 → ""。
// 折叠键用它拼接,所以没有小数的集(绝大多数)产生的键与历史完全一致。
func episodeFractionSuffix(fraction float64) string {
if fraction <= 0 || fraction >= 1 {
return ""
}
formatted := strconv.FormatFloat(fraction, 'f', -1, 64)
if !strings.HasPrefix(formatted, "0.") {
return ""
}
return formatted[1:]
}
// FormatEpisodeNumber 把整数集号与小数部分拼成显示集号:11 → "11",11.5 → "11.5"。
func FormatEpisodeNumber(episode int, fraction float64) string {
return strconv.Itoa(episode) + episodeFractionSuffix(fraction)
}
// resolutionEpisodeArtifact returns the bogus (season, episode) pair the legacy
// `(\d{1,2})x(\d{1,3})` pattern would extract from a WxH pixel-dimension token
// in path — e.g. 1920x1080 -> (20, 108), 3840x2160 -> (40, 216). Reports ok=false
@@ -215,6 +275,21 @@ func onlineEpisodeIdentityFromPath(path string) (season, episode int) {
return season, episode
}
// onlineEpisodeFractionFromPath 返回按在线身份规则得到的集号小数部分:
// S01E11.5 → 0.5。只有在线身份未被重映射(SxxE00 特别篇那种改写)时才给出,
// 避免把重映射后的特别篇又带上原季的小数。
func onlineEpisodeFractionFromPath(path string) float64 {
season, episode, fraction := ParseEpisodeParts(path)
if fraction <= 0 || episode <= 0 {
return 0
}
onlineSeason, onlineEpisode := onlineEpisodeIdentityFromPath(path)
if onlineSeason != season || onlineEpisode != episode {
return 0
}
return fraction
}
type episodeRef struct {
Season int
Episode int
+57
View File
@@ -152,3 +152,60 @@ func TestDropResolutionArtifactEpisodeIdentity(t *testing.T) {
t.Fatalf("legitimate S20E108 identity was cleared: %+v", legit)
}
}
// S01E11.5 这类「半集」要保留小数部分,才能和真正的第 11 集区分开;
// 同时不能把 S01E11.1080p / S01E05.10bit 这类分辨率、位深读成小数集号。
func TestParseEpisodePartsReadsHalfEpisodes(t *testing.T) {
cases := []struct {
name string
path string
wantSeason int
wantEpisode int
wantFraction float64
}{
{"half episode in anime folder", `动漫/三月的狮子/3月的狮子 S01E11.5.mkv.strm`, 1, 11, 0.5},
{"half episode bare", `三月的狮子 S01E07.5.mkv`, 1, 7, 0.5},
{"plain episode has no fraction", `三月的狮子 S01E11.mkv`, 1, 11, 0},
{"resolution is not a fraction", `Show S01E11.1080p.WEB-DL.mkv`, 1, 11, 0},
{"bit depth is not a fraction", `Show S01E05.10bit.mkv`, 1, 5, 0},
{"special naming keeps integral episode", `三月的狮子 S00E11.mkv`, 0, 11, 0},
{"no episode marker", `movie.mkv`, 0, 0, 0},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
season, episode, fraction := ParseEpisodeParts(tc.path)
if season != tc.wantSeason || episode != tc.wantEpisode || fraction != tc.wantFraction {
t.Fatalf("ParseEpisodeParts(%q) = (%d, %d, %v), want (%d, %d, %v)",
tc.path, season, episode, fraction, tc.wantSeason, tc.wantEpisode, tc.wantFraction)
}
// ParseEpisode 必须保持原行为(只返回整数季集号)。
plainSeason, plainEpisode := ParseEpisode(tc.path)
if plainSeason != tc.wantSeason || plainEpisode != tc.wantEpisode {
t.Fatalf("ParseEpisode(%q) = (%d, %d), want (%d, %d)",
tc.path, plainSeason, plainEpisode, tc.wantSeason, tc.wantEpisode)
}
})
}
}
func TestOnlineEpisodeFractionFromPath(t *testing.T) {
if got := onlineEpisodeFractionFromPath(`动漫/三月的狮子/3月的狮子 S01E11.5.mkv.strm`); got != 0.5 {
t.Fatalf("half episode fraction = %v, want 0.5", got)
}
if got := onlineEpisodeFractionFromPath(`三月的狮子 S01E11.mkv`); got != 0 {
t.Fatalf("plain episode fraction = %v, want 0", got)
}
// S01E00 会被重映射成特别篇 S00E01,此时不能再带上原季的小数规则。
if got := onlineEpisodeFractionFromPath(`三月的狮子 S01E00.mkv`); got != 0 {
t.Fatalf("remapped special fraction = %v, want 0", got)
}
}
func TestFormatEpisodeNumber(t *testing.T) {
if got := FormatEpisodeNumber(11, 0.5); got != "11.5" {
t.Fatalf("FormatEpisodeNumber(11, 0.5) = %q, want 11.5", got)
}
if got := FormatEpisodeNumber(11, 0); got != "11" {
t.Fatalf("FormatEpisodeNumber(11, 0) = %q, want 11", got)
}
}
+283
View File
@@ -0,0 +1,283 @@
package service
import (
"context"
"encoding/json"
"errors"
"fmt"
"os/exec"
"strconv"
"strings"
"time"
)
// ffprobeFullTimeout 是一次全量探测(含章节)的超时。远端直链实测 3~4 秒,
// 留足余量的同时不能让一个坏源把工作协程永久占住。
const ffprobeFullTimeout = 60 * time.Second
// ProbeInput 是喂给 ffprobe 的输入:本地文件给路径,STRM / 云盘给已解析的最终
// 直链加绑定请求头。解析由调用方负责(复用播放链路的换链逻辑),否则会踩到
// 115 CDN 的防盗链 403。
type ProbeInput struct {
Source string
Headers map[string]string
}
// ProbeStream 是一路轨道的关键字段,供详情页展示。
type ProbeStream struct {
Index int `json:"index"`
Type string `json:"type"`
Codec string `json:"codec,omitempty"`
Profile string `json:"profile,omitempty"`
Language string `json:"language,omitempty"`
Title string `json:"title,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
PixFmt string `json:"pix_fmt,omitempty"`
FrameRate string `json:"frame_rate,omitempty"`
Channels int `json:"channels,omitempty"`
ChannelLayout string `json:"channel_layout,omitempty"`
SampleRate int `json:"sample_rate,omitempty"`
BitRate int64 `json:"bit_rate,omitempty"`
Default bool `json:"default,omitempty"`
Forced bool `json:"forced,omitempty"`
}
// ProbeChapter 是一个内嵌章节区间。
type ProbeChapter struct {
Index int `json:"index"`
StartMs int64 `json:"start_ms"`
EndMs int64 `json:"end_ms"`
Title string `json:"title,omitempty"`
}
// FullProbeResult 是一次全量探测的裁剪结果。
type FullProbeResult struct {
Container string
DurationSec int
BitRate int64
Streams []ProbeStream
Chapters []ProbeChapter
}
// mediaProbePayload 是落库的媒体信息结构。它只包含白名单字段——刻意不接受
// ffprobe 的原始 JSON,因为其中的 format.filename 是播放直链(含签名与
// pickcode),原样保存会外泄。
type mediaProbePayload struct {
Container string `json:"container,omitempty"`
DurationSec int `json:"duration_sec"`
BitRate int64 `json:"bit_rate,omitempty"`
Streams []ProbeStream `json:"streams"`
Chapters []ProbeChapter `json:"chapters,omitempty"`
}
// StreamsOfType 返回指定类型的轨道,供详情页分组展示。
func (r *FullProbeResult) StreamsOfType(kind string) []ProbeStream {
if r == nil {
return nil
}
out := make([]ProbeStream, 0, len(r.Streams))
for _, s := range r.Streams {
if s.Type == kind {
out = append(out, s)
}
}
return out
}
// PayloadJSON 序列化落库用的媒体信息。
func (r *FullProbeResult) PayloadJSON() (string, error) {
if r == nil {
return "", nil
}
streams := r.Streams
if streams == nil {
streams = []ProbeStream{}
}
body, err := json.Marshal(mediaProbePayload{
Container: r.Container,
DurationSec: r.DurationSec,
BitRate: r.BitRate,
Streams: streams,
Chapters: r.Chapters,
})
if err != nil {
return "", err
}
return string(body), nil
}
// ProbeFull 跑一次全量探测:容器信息 + 全部轨道 + 内嵌章节。
//
// 与 Probe 的区别:Probe 只取扫描需要的几个字段、对着媒体行的本地路径跑;
// ProbeFull 接受调用方解析好的输入(远端直链 + 绑定请求头),并额外抓章节。
func (f *FFprobeService) ProbeFull(ctx context.Context, input ProbeInput) (*FullProbeResult, error) {
if f == nil || f.cfg == nil {
return nil, errors.New("ffprobe service nil")
}
source := strings.TrimSpace(input.Source)
if source == "" {
return nil, errors.New("empty probe source")
}
token, err := f.acquire(ctx)
if err != nil {
return nil, err
}
defer f.release(token)
bin, err := resolveLocalExecutable(f.cfg.App.FFprobePath, "ffprobe")
if err != nil {
return nil, fmt.Errorf("ffprobe unavailable: %w", err)
}
probeCtx, cancel := context.WithTimeout(ctx, ffprobeFullTimeout)
defer cancel()
args := []string{"-v", "error"}
if headerText := ffmpegHeaderText(input.Headers); headerText != "" {
args = append(args, "-headers", headerText)
}
args = append(args,
"-print_format", "json",
"-show_format",
"-show_streams",
"-show_chapters",
source,
)
out, err := exec.CommandContext(probeCtx, bin, args...).Output() // #nosec G204 -- bin is resolved by resolveLocalExecutable before execution.
if err != nil {
return nil, fmt.Errorf("ffprobe full: %w", err)
}
return parseFullProbeJSON(out)
}
// probeNumber 兼容 ffprobe 把数值输出成字符串或裸数字两种形态
// (duration 是字符串,chapter 的 start_time 也可能是数字)。解析不出来就保持
// 零值:这些字段都只是展示用,不该因为一个格式差异让整次探测失败。
type probeNumber float64
func (p *probeNumber) UnmarshalJSON(data []byte) error {
text := strings.TrimSpace(strings.Trim(string(data), `"`))
if text == "" || text == "null" {
return nil
}
value, err := strconv.ParseFloat(text, 64)
if err != nil {
return nil
}
*p = probeNumber(value)
return nil
}
func (p probeNumber) float() float64 { return float64(p) }
func (p probeNumber) int() int { return int(float64(p)) }
func (p probeNumber) int64() int64 { return int64(float64(p)) }
// rawFullProbe 镜像 ffprobe -show_format -show_streams -show_chapters 的输出。
// 只声明用得到的字段;format.filename 刻意不声明,避免它进入任何落库路径。
type rawFullProbe struct {
Format struct {
FormatName string `json:"format_name"`
Duration probeNumber `json:"duration"`
BitRate probeNumber `json:"bit_rate"`
} `json:"format"`
Streams []struct {
Index int `json:"index"`
CodecType string `json:"codec_type"`
CodecName string `json:"codec_name"`
Profile string `json:"profile"`
Width int `json:"width"`
Height int `json:"height"`
PixFmt string `json:"pix_fmt"`
AvgFrameRate string `json:"avg_frame_rate"`
Channels int `json:"channels"`
ChannelLayout string `json:"channel_layout"`
SampleRate probeNumber `json:"sample_rate"`
BitRate probeNumber `json:"bit_rate"`
Tags struct {
Language string `json:"language"`
Title string `json:"title"`
} `json:"tags"`
Disposition struct {
Default int `json:"default"`
Forced int `json:"forced"`
} `json:"disposition"`
} `json:"streams"`
Chapters []struct {
StartTime probeNumber `json:"start_time"`
EndTime probeNumber `json:"end_time"`
Tags struct {
Title string `json:"title"`
} `json:"tags"`
} `json:"chapters"`
}
func parseFullProbeJSON(data []byte) (*FullProbeResult, error) {
var raw rawFullProbe
if err := json.Unmarshal(data, &raw); err != nil {
return nil, fmt.Errorf("parse ffprobe json: %w", err)
}
result := &FullProbeResult{
Container: strings.TrimSpace(raw.Format.FormatName),
DurationSec: raw.Format.Duration.int(),
BitRate: raw.Format.BitRate.int64(),
Streams: make([]ProbeStream, 0, len(raw.Streams)),
Chapters: make([]ProbeChapter, 0, len(raw.Chapters)),
}
for _, s := range raw.Streams {
stream := ProbeStream{
Index: s.Index,
Type: s.CodecType,
Codec: s.CodecName,
Profile: s.Profile,
Language: strings.TrimSpace(s.Tags.Language),
Title: strings.TrimSpace(s.Tags.Title),
Width: s.Width,
Height: s.Height,
PixFmt: s.PixFmt,
FrameRate: normalizeFrameRate(s.AvgFrameRate),
Channels: s.Channels,
ChannelLayout: s.ChannelLayout,
SampleRate: s.SampleRate.int(),
BitRate: s.BitRate.int64(),
Default: s.Disposition.Default != 0,
Forced: s.Disposition.Forced != 0,
}
result.Streams = append(result.Streams, stream)
}
for index, chapter := range raw.Chapters {
result.Chapters = append(result.Chapters, ProbeChapter{
Index: index,
StartMs: secondsToMillis(chapter.StartTime.float()),
EndMs: secondsToMillis(chapter.EndTime.float()),
Title: strings.TrimSpace(chapter.Tags.Title),
})
}
return result, nil
}
// secondsToMillis 把 ffprobe 的秒(浮点)转成毫秒整数。
func secondsToMillis(seconds float64) int64 {
if seconds <= 0 {
return 0
}
return int64(seconds*1000 + 0.5)
}
// normalizeFrameRate 把 "24000/1001" 这类分数帧率换算成可读形式;"0/0"
// (未知)返回空串。
func normalizeFrameRate(raw string) string {
raw = strings.TrimSpace(raw)
if raw == "" {
return ""
}
parts := strings.SplitN(raw, "/", 2)
if len(parts) != 2 {
return raw
}
num, errNum := strconv.ParseFloat(parts[0], 64)
den, errDen := strconv.ParseFloat(parts[1], 64)
if errNum != nil || errDen != nil || den == 0 || num <= 0 {
return ""
}
return strconv.FormatFloat(num/den, 'f', 3, 64)
}
+149
View File
@@ -0,0 +1,149 @@
package service
import (
"strings"
"testing"
)
const fullProbeFixture = `{
"format": {
"format_name": "matroska,webm",
"duration": "1451.024000",
"bit_rate": "8000000",
"filename": "https://cdn.example.com/secret/movie.mkv?d=vip-abc-pickcode&token=xyz"
},
"streams": [
{"index": 0, "codec_type": "video", "codec_name": "hevc", "profile": "Main 10",
"width": 3840, "height": 2160, "pix_fmt": "yuv420p10le", "avg_frame_rate": "24000/1001",
"bit_rate": "7800000", "disposition": {"default": 1}},
{"index": 1, "codec_type": "audio", "codec_name": "eac3", "channels": 6,
"channel_layout": "5.1(side)", "sample_rate": "48000",
"tags": {"language": "eng", "title": "Surround"}, "disposition": {"default": 1}},
{"index": 2, "codec_type": "subtitle", "codec_name": "ass",
"tags": {"language": "chi"}, "disposition": {"forced": 1}}
],
"chapters": [
{"start_time": "0.000000", "end_time": "95.000000", "tags": {"title": "Chapter 01"}},
{"start_time": "228.664000", "end_time": "246.143000", "tags": {"title": "Opening"}}
]
}`
func TestParseFullProbeJSONExtractsStreamsAndChapters(t *testing.T) {
got, err := parseFullProbeJSON([]byte(fullProbeFixture))
if err != nil {
t.Fatalf("parseFullProbeJSON: %v", err)
}
if got.Container != "matroska,webm" || got.DurationSec != 1451 || got.BitRate != 8_000_000 {
t.Fatalf("container/duration/bitrate = %q/%d/%d", got.Container, got.DurationSec, got.BitRate)
}
if len(got.Streams) != 3 {
t.Fatalf("streams = %#v, want 3", got.Streams)
}
video := got.Streams[0]
if video.Type != "video" || video.Codec != "hevc" || video.Width != 3840 || video.Height != 2160 {
t.Fatalf("video stream = %#v", video)
}
if video.FrameRate != "23.976" {
t.Fatalf("frame rate = %q, want 23.976 (converted from 24000/1001)", video.FrameRate)
}
if !video.Default {
t.Fatal("video stream should be flagged default")
}
audio := got.Streams[1]
if audio.Codec != "eac3" || audio.Language != "eng" || audio.Title != "Surround" || audio.Channels != 6 || audio.SampleRate != 48000 {
t.Fatalf("audio stream = %#v", audio)
}
sub := got.Streams[2]
if sub.Type != "subtitle" || sub.Language != "chi" || !sub.Forced {
t.Fatalf("subtitle stream = %#v", sub)
}
if len(got.Chapters) != 2 {
t.Fatalf("chapters = %#v, want 2", got.Chapters)
}
if got.Chapters[1].StartMs != 228_664 || got.Chapters[1].EndMs != 246_143 || got.Chapters[1].Title != "Opening" {
t.Fatalf("chapter[1] = %#v", got.Chapters[1])
}
if videoCount := len(got.StreamsOfType("video")); videoCount != 1 {
t.Fatalf("StreamsOfType(video) = %d, want 1", videoCount)
}
if audioCount := len(got.StreamsOfType("audio")); audioCount != 1 {
t.Fatalf("StreamsOfType(audio) = %d, want 1", audioCount)
}
}
// 落库的 payload 绝不能带 ffprobe 的 format.filename:那是解析后的播放直链,
// 里面是网盘签名和 pickcode,存进数据库等于把可直接下载的链接留下来。
func TestFullProbePayloadNeverLeaksSourceURL(t *testing.T) {
got, err := parseFullProbeJSON([]byte(fullProbeFixture))
if err != nil {
t.Fatalf("parseFullProbeJSON: %v", err)
}
payload, err := got.PayloadJSON()
if err != nil {
t.Fatalf("PayloadJSON: %v", err)
}
for _, needle := range []string{"filename", "cdn.example.com", "pickcode", "token=", "http"} {
if strings.Contains(payload, needle) {
t.Fatalf("payload 泄漏了 %q:\n%s", needle, payload)
}
}
// 技术信息本身必须保留。
for _, needle := range []string{"matroska,webm", "hevc", "3840", "Opening"} {
if !strings.Contains(payload, needle) {
t.Fatalf("payload 缺少 %q:\n%s", needle, payload)
}
}
}
// ffprobe 对 duration / start_time 有时输出字符串、有时输出裸数字,两种都要能读。
func TestParseFullProbeJSONAcceptsNumericAndStringTimes(t *testing.T) {
got, err := parseFullProbeJSON([]byte(`{
"format": {"format_name": "mp4", "duration": 125.5},
"streams": [],
"chapters": [{"start_time": 12.5, "end_time": 20}]
}`))
if err != nil {
t.Fatalf("parseFullProbeJSON: %v", err)
}
if got.DurationSec != 125 {
t.Fatalf("duration = %d, want 125", got.DurationSec)
}
if len(got.Chapters) != 1 || got.Chapters[0].StartMs != 12_500 || got.Chapters[0].EndMs != 20_000 {
t.Fatalf("chapters = %#v", got.Chapters)
}
}
func TestParseFullProbeJSONToleratesMissingSections(t *testing.T) {
got, err := parseFullProbeJSON([]byte(`{}`))
if err != nil {
t.Fatalf("parseFullProbeJSON: %v", err)
}
if got.DurationSec != 0 || len(got.Streams) != 0 || len(got.Chapters) != 0 {
t.Fatalf("result = %#v, want empty", got)
}
payload, err := got.PayloadJSON()
if err != nil {
t.Fatalf("PayloadJSON: %v", err)
}
// 空轨道必须序列化成 [],不能是 null——详情页前端按数组消费。
if !strings.Contains(payload, `"streams":[]`) {
t.Fatalf("payload = %s, want an empty streams array", payload)
}
}
func TestNormalizeFrameRate(t *testing.T) {
cases := map[string]string{
"24000/1001": "23.976",
"25/1": "25.000",
"0/0": "",
"": "",
"25": "25",
}
for input, want := range cases {
if got := normalizeFrameRate(input); got != want {
t.Errorf("normalizeFrameRate(%q) = %q, want %q", input, got, want)
}
}
}
+119 -12
View File
@@ -29,6 +29,15 @@ const (
introDBTimeout = 8 * time.Second
introDBMaxBodySize = 1 << 20
// introDBMaxAttempts 是一次 Fetch 允许的请求次数(原请求 + 1 次重试)。
// 实测:短时间连发 45 个请求有 15 个被返回 429,加 1.2 秒间隔重试后
// 其中 10 个成功,所以限流是真实存在的、值得一次重试。
introDBMaxAttempts = 2
// introDBRetryDelay 是服务端没给 Retry-After 时的默认重试间隔。
introDBRetryDelay = time.Second
// introDBMaxRetryDelay 限制服务端要求的等待时间:一次播放不值得为它
// 挂住几十秒,等待超过这个值就按这个值等(然后可能再次被限流)。
introDBMaxRetryDelay = 3 * time.Second
)
// IntroDBSpan is one resolved skip range, still in provider terms.
@@ -42,18 +51,20 @@ type IntroDBSpan struct {
// IntroDBService queries TheIntroDB for one media item.
type IntroDBService struct {
log *zap.Logger
client *http.Client
baseURL string
log *zap.Logger
client *http.Client
baseURL string
retryDelay time.Duration
}
// NewIntroDBService is the constructor. The client honours environment and OS
// proxy settings so it behaves like the other third-party API clients.
func NewIntroDBService(log *zap.Logger) *IntroDBService {
return &IntroDBService{
log: log,
client: NewExternalHTTPClient(introDBTimeout),
baseURL: IntroDBBaseURL,
log: log,
client: NewExternalHTTPClient(introDBTimeout),
baseURL: IntroDBBaseURL,
retryDelay: introDBRetryDelay,
}
}
@@ -65,6 +76,15 @@ func (s *IntroDBService) SetBaseURL(base string) *IntroDBService {
return s
}
// SetRetryDelay overrides the wait between attempts. Tests set it to 0 so a
// retry does not really sleep.
func (s *IntroDBService) SetRetryDelay(delay time.Duration) *IntroDBService {
if s != nil {
s.retryDelay = delay
}
return s
}
// introDBRange mirrors one entry of a segment array. start_ms/end_ms are
// pointers because the API distinguishes null (= open-ended) from 0.
type introDBRange struct {
@@ -86,6 +106,10 @@ type introDBResponse struct {
// caller records it as a negative cache entry.
//
// season/episode are required for TV; pass 0/0 for movies.
//
// 429/503 会重试一次(社区库在短时间连发下确实会限流)。重试前会先确认调用方
// 的 deadline 还够用;预算不够就直接返回错误,让调用方保留自己的缓存,
// 把「拿不到片段」维持在「少一个跳过按钮」的量级。
func (s *IntroDBService) Fetch(ctx context.Context, tmdbID, season, episode int) ([]IntroDBSpan, error) {
if s == nil || s.client == nil {
return nil, errors.New("introdb service nil")
@@ -94,28 +118,111 @@ func (s *IntroDBService) Fetch(ctx context.Context, tmdbID, season, episode int)
return nil, nil
}
endpoint := s.mediaURL(tmdbID, season, episode)
for attempt := 1; ; attempt++ {
result := s.fetchOnce(ctx, endpoint)
if result.err == nil {
return result.spans, nil
}
if !result.retryable || attempt >= introDBMaxAttempts {
return nil, result.err
}
if !waitForIntroDBRetry(ctx, s.retryWait(result.retryAfter)) {
return nil, result.err
}
}
}
// introDBFetchAttempt 是一次请求的结果:数据或错误,外加「值不值得重试」。
type introDBFetchAttempt struct {
spans []IntroDBSpan
err error
retryable bool
retryAfter time.Duration
}
func (s *IntroDBService) fetchOnce(ctx context.Context, endpoint string) introDBFetchAttempt {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
return introDBFetchAttempt{err: err}
}
req.Header.Set("Accept", "application/json")
resp, err := s.client.Do(req)
if err != nil {
return nil, err
return introDBFetchAttempt{err: err}
}
defer func() { _ = resp.Body.Close() }()
switch {
case resp.StatusCode == http.StatusNotFound:
return nil, nil
// 「查到但社区库里没有」不是错误,调用方据此写负缓存。
return introDBFetchAttempt{}
case resp.StatusCode < 200 || resp.StatusCode >= 300:
return nil, fmt.Errorf("introdb: unexpected status %d", resp.StatusCode)
return introDBFetchAttempt{
err: fmt.Errorf("introdb: unexpected status %d", resp.StatusCode),
retryable: introDBRetryableStatus(resp.StatusCode),
retryAfter: parseIntroDBRetryAfter(resp.Header.Get("Retry-After")),
}
}
body, err := io.ReadAll(io.LimitReader(resp.Body, introDBMaxBodySize))
if err != nil {
return nil, err
return introDBFetchAttempt{err: err}
}
spans, err := parseIntroDBResponse(body)
if err != nil {
return introDBFetchAttempt{err: err}
}
return introDBFetchAttempt{spans: spans}
}
// introDBRetryableStatus 只认明确的「稍后再来」状态。500 之类的服务端故障
// 重试也不会变好,却会白占调用方的等待预算。
func introDBRetryableStatus(status int) bool {
switch status {
case http.StatusTooManyRequests, http.StatusServiceUnavailable:
return true
default:
return false
}
}
// parseIntroDBRetryAfter 解析 Retry-After 的秒数形式;HTTP-date 形式在限流
// 场景很少见,解析不出来就退回默认间隔。
func parseIntroDBRetryAfter(value string) time.Duration {
seconds, err := strconv.Atoi(strings.TrimSpace(value))
if err != nil || seconds <= 0 {
return 0
}
return time.Duration(seconds) * time.Second
}
func (s *IntroDBService) retryWait(retryAfter time.Duration) time.Duration {
wait := retryAfter
if wait <= 0 {
wait = s.retryDelay
}
if wait > introDBMaxRetryDelay {
wait = introDBMaxRetryDelay
}
return wait
}
// waitForIntroDBRetry 睡到重试时刻,或调用方的 ctx 先结束。返回 false 表示
// 预算已经用完,调用方不该再等。
func waitForIntroDBRetry(ctx context.Context, wait time.Duration) bool {
if ctx.Err() != nil {
return false
}
if wait <= 0 {
return true
}
timer := time.NewTimer(wait)
defer timer.Stop()
select {
case <-timer.C:
return true
case <-ctx.Done():
return false
}
return parseIntroDBResponse(body)
}
func (s *IntroDBService) mediaURL(tmdbID, season, episode int) string {
+142
View File
@@ -1,9 +1,12 @@
package service
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
"go.uber.org/zap"
)
@@ -160,3 +163,142 @@ func TestIntroDBFetchParsesBody(t *testing.T) {
t.Fatalf("spans = %#v", spans)
}
}
// 实测:从生产机连发 45 个请求有 15 个被返回 429,加间隔重试后其中 10 个成功。
// 所以限流值得一次重试,否则那部分播放会静默少掉「跳过片头」按钮。
func TestIntroDBFetchRetriesRateLimit(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
if atomic.AddInt32(&calls, 1) == 1 {
w.WriteHeader(http.StatusTooManyRequests)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(introDBTVPayload))
}))
defer server.Close()
svc := NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL).SetRetryDelay(0)
spans, err := svc.Fetch(t.Context(), 1396, 1, 1)
if err != nil {
t.Fatalf("fetch: %v", err)
}
if len(spans) != 2 {
t.Fatalf("spans = %#v, want the retried response", spans)
}
if got := atomic.LoadInt32(&calls); got != 2 {
t.Fatalf("calls = %d, want 2 (the original plus one retry)", got)
}
}
func TestIntroDBFetchGivesUpAfterRetryBudget(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusTooManyRequests)
}))
defer server.Close()
svc := NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL).SetRetryDelay(0)
if _, err := svc.Fetch(t.Context(), 1396, 1, 1); err == nil {
t.Fatal("a persistent 429 must surface as an error so the caller keeps its cache")
}
// 只重试一次:限流通常不是靠密集重试解决的,而调用方的等待预算有限。
if got := atomic.LoadInt32(&calls); got != 2 {
t.Fatalf("calls = %d, want 2 (one retry, then give up)", got)
}
}
func TestIntroDBFetchDoesNotRetryNotFound(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusNotFound)
}))
defer server.Close()
svc := NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL).SetRetryDelay(0)
spans, err := svc.Fetch(t.Context(), 424242, 0, 0)
if err != nil || len(spans) != 0 {
t.Fatalf("spans = %#v err = %v, want a cached miss", spans, err)
}
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("calls = %d, want 1: 404 means \"no data\", it is not worth retrying", got)
}
}
// 调用方预算不够时不能为了重试干等:Emby 只给 5 秒,等下去会把
// 「少一个跳过按钮」升级成「请求超时」。
func TestIntroDBFetchSkipsRetryWhenCallerBudgetIsSpent(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusTooManyRequests)
}))
defer server.Close()
svc := NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL).SetRetryDelay(time.Minute)
ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond)
defer cancel()
started := time.Now()
if _, err := svc.Fetch(ctx, 1396, 1, 1); err == nil {
t.Fatal("want an error when the budget is spent")
}
if elapsed := time.Since(started); elapsed > 2*time.Second {
t.Fatalf("Fetch waited %s, want it to give up promptly", elapsed)
}
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("calls = %d, want 1 without a retry", got)
}
}
func TestIntroDBRetryWaitPrefersRetryAfterHeader(t *testing.T) {
svc := NewIntroDBService(zap.NewNop())
if got := svc.retryWait(2 * time.Second); got != 2*time.Second {
t.Fatalf("retryWait(2s) = %s, want the server's value", got)
}
if got := svc.retryWait(0); got != introDBRetryDelay {
t.Fatalf("retryWait(0) = %s, want the default %s", got, introDBRetryDelay)
}
// 服务端可以要求等很久,但一次播放不值得为它挂住几十秒。
if got := svc.retryWait(10 * time.Minute); got != introDBMaxRetryDelay {
t.Fatalf("retryWait(10m) = %s, want it capped at %s", got, introDBMaxRetryDelay)
}
}
func TestParseIntroDBRetryAfter(t *testing.T) {
cases := []struct {
value string
want time.Duration
}{
{"2", 2 * time.Second},
{" 3 ", 3 * time.Second},
{"", 0},
{"abc", 0},
{"-5", 0},
{"0", 0},
}
for _, tc := range cases {
if got := parseIntroDBRetryAfter(tc.value); got != tc.want {
t.Fatalf("parseIntroDBRetryAfter(%q) = %s, want %s", tc.value, got, tc.want)
}
}
}
func TestIntroDBRetryableStatus(t *testing.T) {
retryable := []int{http.StatusTooManyRequests, http.StatusServiceUnavailable}
for _, status := range retryable {
if !introDBRetryableStatus(status) {
t.Fatalf("status %d should be retryable", status)
}
}
// 404 由 fetchOnce 单独处理;500 之类的服务端故障重试也不会变好,
// 却会白占调用方的等待预算。
notRetryable := []int{http.StatusNotFound, http.StatusInternalServerError, http.StatusBadRequest}
for _, status := range notRetryable {
if introDBRetryableStatus(status) {
t.Fatalf("status %d should not be retryable", status)
}
}
}
+1 -1
View File
@@ -53,7 +53,7 @@ func LookupLocalAvailability(ctx context.Context, repo *repository.Container, ti
var rows []model.Media
if err := repo.DB.WithContext(ctx).
Where("title LIKE ? OR original_name LIKE ? OR path LIKE ?", like, like, like).
Order("season_num asc, episode_num asc, created_at desc").
Order("season_num asc, episode_num asc, episode_fraction asc, created_at desc").
Limit(2000).
Find(&rows).Error; err != nil {
return out
+5 -1
View File
@@ -68,7 +68,11 @@ func applyLocalEpisodeMetadata(m *model.Media, local *LocalMetadata) {
if local.EpisodeTitle != "" {
m.EpisodeTitle = local.EpisodeTitle
}
if local.SeasonNum > 0 || local.EpisodeNum > 0 {
// tvshow.nfo 里常带 scraper 写的 <season>-1</season> / <episode>-1</episode>
// 哨兵(表示「整剧级、不适用」)。负季号不是有效季号:一旦让它覆盖,该集会被
// 写成 -1,网页端按 seasonLabel(-1) 显示成「剧场版」。因此负值一律忽略,
// 保留文件名解析出来的季号(S00Exx 会被解析成第 0 季 = 特别篇)。
if local.SeasonNum >= 0 && (local.SeasonNum > 0 || local.EpisodeNum > 0) {
m.SeasonNum = local.SeasonNum
}
if local.EpisodeNum > 0 {
+47
View File
@@ -4,6 +4,8 @@ import (
"os"
"path/filepath"
"testing"
"github.com/truewhile/MeBox/internal/model"
)
func TestReadLocalMetadataDropsResolutionArtifactEpisode(t *testing.T) {
@@ -436,3 +438,48 @@ func TestReadLocalMetadataFindsLegacyImgPoster(t *testing.T) {
t.Fatalf("PosterURL = %q, want legacy .img poster %q", got.PosterURL, poster)
}
}
// tvshow.nfo 里 scraper 写的 <season>-1</season>(整剧级"不适用"哨兵)不能覆盖
// 文件名解析出的季号:一旦覆盖,该集会被写成 -1,网页端按 seasonLabel(-1) 显示成
// 「剧场版」,Emby 端也只能落进特别篇。S00Exx 这类特别篇命名应保留第 0 季。
func TestApplyLocalEpisodeMetadataIgnoresNegativeSeasonSentinel(t *testing.T) {
cases := []struct {
name string
parsed model.Media
local LocalMetadata
wantSeason int
wantEpisode int
}{
{
name: "show level sentinel keeps parsed season",
parsed: model.Media{SeasonNum: 2, EpisodeNum: 5},
local: LocalMetadata{SeasonNum: -1, EpisodeNum: 5, HasNFO: true},
wantSeason: 2,
wantEpisode: 5,
},
{
name: "special naming keeps season zero",
parsed: model.Media{SeasonNum: 0, EpisodeNum: 11},
local: LocalMetadata{SeasonNum: -1, EpisodeNum: 11, HasNFO: true},
wantSeason: 0,
wantEpisode: 11,
},
{
name: "positive episode season still applies",
parsed: model.Media{SeasonNum: 0, EpisodeNum: 11},
local: LocalMetadata{SeasonNum: 1, EpisodeNum: 11, HasNFO: true},
wantSeason: 1,
wantEpisode: 11,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
media := tc.parsed
local := tc.local
applyLocalMetadata(&media, &local)
if media.SeasonNum != tc.wantSeason || media.EpisodeNum != tc.wantEpisode {
t.Fatalf("season/episode = %d/%d, want %d/%d", media.SeasonNum, media.EpisodeNum, tc.wantSeason, tc.wantEpisode)
}
})
}
}
+4
View File
@@ -82,10 +82,14 @@ func rebuildManualScrapeIdentity(media *model.Media, lib *model.Library, match *
switch mediaType {
case "tv", "anime", "variety":
media.SeasonNum, media.EpisodeNum = onlineEpisodeIdentityFromPath(media.Path)
// 半集(S01E11.5)的小数部分同样按路径重建,否则重新刮削会把 11.5
// 变回普通的第 11 集。
media.EpisodeFraction = onlineEpisodeFractionFromPath(media.Path)
media.EpisodeTitle = ""
case "movie", "adult":
media.SeasonNum = 0
media.EpisodeNum = 0
media.EpisodeFraction = 0
media.EpisodeTitle = ""
}
}
+22 -10
View File
@@ -212,10 +212,11 @@ func (s *MediaDiscoveryService) NextUpCandidates(ctx context.Context, userID str
byID[watchedRows[i].ID] = &watchedRows[i]
}
// 按最近观看顺序归并到「剧」维度,同时记住该剧最近的已看集。
// 按最近观看顺序归并到「剧」维度,同时记住该剧最近看的那一集以及它是否看完。
type seriesState struct {
key string
current *model.Media
key string
current *model.Media
completed bool
}
states := make([]seriesState, 0, len(histories))
seen := make(map[string]bool, len(histories))
@@ -229,7 +230,7 @@ func (s *MediaDiscoveryService) NextUpCandidates(ctx context.Context, userID str
continue
}
seen[key] = true
states = append(states, seriesState{key: key, current: m})
states = append(states, seriesState{key: key, current: m, completed: h.Completed})
}
if len(states) == 0 {
return nil, nil
@@ -293,7 +294,7 @@ func (s *MediaDiscoveryService) NextUpCandidates(ctx context.Context, userID str
if len(out) >= limit {
break
}
next, ok := pickNextEpisode(bySeries[st.key], st.current, completed)
next, ok := pickNextEpisode(bySeries[st.key], st.current, st.completed, completed)
if !ok {
continue
}
@@ -341,16 +342,27 @@ func (s *MediaDiscoveryService) completedMediaIDs(ctx context.Context, userID st
return out
}
// pickNextEpisode 在候选集中选出严格晚于 current 的、编号最小的一集。
// 比较顺序为 (季, 集),因此跨季时自然落到下一季第一集。
func pickNextEpisode(candidates []model.Media, current *model.Media, completed map[string]bool) (model.Media, bool) {
// pickNextEpisode 选出这部剧「接下来该看的那一集」。
//
// anchor 是这部剧最近一次播放的那一集,anchorCompleted 表示那一集是否已看完:
// - 没看完(只播了几秒就退出、或中途暂停)时,接下来该看的仍是这一集本身。
// 否则详情页的「继续播放」会直接跳到下一集,用户刚看的那一集被静默跳过。
// - 已看完时,才在候选集里取严格晚于它的、编号最小的一集;比较顺序为
// (季, 集),因此跨季时自然落到下一季第一集。
func pickNextEpisode(candidates []model.Media, anchor *model.Media, anchorCompleted bool, completed map[string]bool) (model.Media, bool) {
if anchor == nil {
return model.Media{}, false
}
if !anchorCompleted {
return *anchor, true
}
var best model.Media
found := false
for _, candidate := range candidates {
if candidate.ID == current.ID || completed[candidate.ID] {
if candidate.ID == anchor.ID || completed[candidate.ID] {
continue
}
if !episodeAfter(candidate, *current) {
if !episodeAfter(candidate, *anchor) {
continue
}
if !found || episodeBefore(candidate, best) {
@@ -13,6 +13,9 @@ import (
)
// seedEpisode 插入一集,并把播放历史指向 `watched`(nil 表示没有历史)。
//
// position_ms / duration_ms 只是占位值,NextUp 只看历史行的 completed 字段:
// 未看完的那一集本身就是「接下来该看的一集」。
func seedEpisode(
t *testing.T,
repos *repository.Container,
@@ -78,7 +81,7 @@ func TestNextUpPicksNextEpisode(t *testing.T) {
libID := seedDiscoveryLibrary(t, repos, "tv")
watchedAt := time.Now().Add(-time.Hour)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 1, &watchedAt, false)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 1, &watchedAt, true) // 第 1 集已看完
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 2, nil, false)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 3, nil, false)
@@ -89,6 +92,40 @@ func TestNextUpPicksNextEpisode(t *testing.T) {
}
}
// 回归:只播了几秒就退出(未看完)时,「接下来该看的一集」仍是这一集本身。
// 客户端(Yamby 等)剧集详情页的「继续播放」直接取 NextUp 第一条,跳集会播错集。
func TestNextUpKeepsPartiallyWatchedEpisode(t *testing.T) {
repos := newDiscoveryTestDB(t)
libID := seedDiscoveryLibrary(t, repos, "tv")
watchedAt := time.Now().Add(-time.Minute)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 1, nil, true)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 2, &watchedAt, false) // 第 2 集只看了几秒
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 3, nil, false)
svc := NewMediaDiscoveryService(zap.NewNop(), repos)
got := nextUpIDs(t, svc, MediaVisibility{IncludeNSFW: true})
if len(got) != 1 || got[0] != "S1E2" {
t.Fatalf("next up = %v, want [S1E2] (未看完的那一集不能跳过)", got)
}
}
// 未看完的是这部剧的最后一集时也要返回它,不能因为「后面没有集了」而返回空。
func TestNextUpKeepsPartiallyWatchedFinalEpisode(t *testing.T) {
repos := newDiscoveryTestDB(t)
libID := seedDiscoveryLibrary(t, repos, "tv")
watchedAt := time.Now().Add(-time.Minute)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 1, nil, true)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 2, &watchedAt, false) // 最后一集未看完
svc := NewMediaDiscoveryService(zap.NewNop(), repos)
got := nextUpIDs(t, svc, MediaVisibility{IncludeNSFW: true})
if len(got) != 1 || got[0] != "S1E2" {
t.Fatalf("next up = %v, want [S1E2]", got)
}
}
// 电影不进 NextUp:NextUp 的语义是「下一集」,电影由 Resume 接口负责。
func TestNextUpSkipsMovies(t *testing.T) {
repos := newDiscoveryTestDB(t)
@@ -104,6 +141,10 @@ func TestNextUpSkipsMovies(t *testing.T) {
}
// 同一部剧有多条未看完历史时,只能出一条,且指向最靠后的已看集的下一集。
// 同一部剧有多条未看完历史时只能出一条,且指向最近看过的那一集。
//
// 最近那一集(S1E2)本身还没看完,所以它就是「接下来该看的一集」;
// S1E1 只是更早的中间进度,不能据此跳到 S1E3。
func TestNextUpOneEntryPerSeries(t *testing.T) {
repos := newDiscoveryTestDB(t)
libID := seedDiscoveryLibrary(t, repos, "tv")
@@ -116,8 +157,8 @@ func TestNextUpOneEntryPerSeries(t *testing.T) {
svc := NewMediaDiscoveryService(zap.NewNop(), repos)
got := nextUpIDs(t, svc, MediaVisibility{IncludeNSFW: true})
if len(got) != 1 || got[0] != "S1E3" {
t.Fatalf("next up = %v, want [S1E3]", got)
if len(got) != 1 || got[0] != "S1E2" {
t.Fatalf("next up = %v, want [S1E2]", got)
}
}
@@ -127,7 +168,7 @@ func TestNextUpCrossesSeason(t *testing.T) {
libID := seedDiscoveryLibrary(t, repos, "tv")
watchedAt := time.Now().Add(-time.Hour)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 12, &watchedAt, false)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 12, &watchedAt, true) // 第 1 季最后一集已看完
seedEpisode(t, repos, libID, "series-1", "剧一", 2, 1, nil, false)
seedEpisode(t, repos, libID, "series-1", "剧一", 2, 2, nil, false)
@@ -144,8 +185,8 @@ func TestNextUpSkipsCompletedEpisode(t *testing.T) {
libID := seedDiscoveryLibrary(t, repos, "tv")
watchedAt := time.Now().Add(-time.Hour)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 1, &watchedAt, false)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 2, nil, true) // 已看完
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 1, &watchedAt, true) // 已看完,下一集是 S1E3
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 2, nil, true) // 已看完
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 3, nil, false)
svc := NewMediaDiscoveryService(zap.NewNop(), repos)
@@ -155,13 +196,13 @@ func TestNextUpSkipsCompletedEpisode(t *testing.T) {
}
}
// 追到最后一集时没有下一集,结果为空而不是重复返回最后一集。
// 追到最后一集且已看完时没有下一集,结果为空而不是重复返回最后一集。
func TestNextUpEmptyAtSeriesEnd(t *testing.T) {
repos := newDiscoveryTestDB(t)
libID := seedDiscoveryLibrary(t, repos, "tv")
watchedAt := time.Now().Add(-time.Hour)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 3, &watchedAt, false)
seedEpisode(t, repos, libID, "series-1", "剧一", 1, 3, &watchedAt, true)
svc := NewMediaDiscoveryService(zap.NewNop(), repos)
if got := nextUpIDs(t, svc, MediaVisibility{IncludeNSFW: true}); len(got) != 0 {
@@ -176,7 +217,7 @@ func TestNextUpRespectsVisibility(t *testing.T) {
hiddenLib := seedDiscoveryLibrary(t, repos, "tv")
watchedAt := time.Now().Add(-time.Hour)
seedEpisode(t, repos, visibleLib, "series-1", "剧一", 1, 1, &watchedAt, false)
seedEpisode(t, repos, visibleLib, "series-1", "剧一", 1, 1, &watchedAt, true)
seedEpisode(t, repos, hiddenLib, "series-1", "剧一", 1, 2, nil, false)
svc := NewMediaDiscoveryService(zap.NewNop(), repos)
+16 -1
View File
@@ -19,7 +19,10 @@ type MediaMetadataUpdate struct {
Rating *float32 `json:"rating"`
SeasonNum *int `json:"season_num"`
EpisodeNum *int `json:"episode_num"`
TMDbID *int `json:"tmdb_id"`
// EpisodeFraction 是集号的小数部分(11.5 的 0.5)。单独提供是为了让编辑
// 「集」时不会把半集写成整数集号,也不会把整集写成小数。
EpisodeFraction *float64 `json:"episode_fraction"`
TMDbID *int `json:"tmdb_id"`
BangumiID *int `json:"bangumi_id"`
DoubanID *string `json:"douban_id"`
TheTVDBID *string `json:"thetvdb_id"`
@@ -77,6 +80,9 @@ func (s *MediaService) UpdateMetadata(ctx context.Context, id string, req MediaM
if req.EpisodeNum != nil {
updates["episode_num"] = clampNonNegativeInt(*req.EpisodeNum)
}
if req.EpisodeFraction != nil {
updates["episode_fraction"] = clampEpisodeFraction(*req.EpisodeFraction)
}
if req.TMDbID != nil {
updates["tm_db_id"] = clampNonNegativeInt(*req.TMDbID)
}
@@ -141,6 +147,15 @@ func clampNonNegativeInt(value int) int {
return value
}
// clampEpisodeFraction 把集号小数部分夹到 [0,1):小于 0 或大于等于 1 都视为
// 「没有小数」,避免把非法值写进库。
func clampEpisodeFraction(value float64) float64 {
if value <= 0 || value >= 1 {
return 0
}
return value
}
func clampRating(value float32) float32 {
if value < 0 {
return 0
+321
View File
@@ -0,0 +1,321 @@
package service
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
"sync"
"time"
"go.uber.org/zap"
"github.com/truewhile/MeBox/internal/helper"
"github.com/truewhile/MeBox/internal/model"
"github.com/truewhile/MeBox/internal/repository"
)
// 探测结果的缓存与预算策略。
const (
// mediaProbeTimeout 是一次后台探测的总预算(含把 strm 目标解析成直链)。
mediaProbeTimeout = 90 * time.Second
// mediaProbeFailureRetry 是探测失败后允许重新探测的间隔。失败的结果也会落库,
// 否则每次播放都会为一个坏源重跑一次。
mediaProbeFailureRetry = 6 * time.Hour
// mediaProbeErrorLimit 限制落库的错误信息长度(列宽 512 字节,错误里可能
// 带 URL,截断同时避免超长)。
mediaProbeErrorLimit = 200
)
// mediaProber 是 MediaProbeService 需要的探测能力。抽成接口是为了在测试里注入
// 桩,避免依赖真实 ffprobe 二进制。
type mediaProber interface {
ProbeFull(ctx context.Context, input ProbeInput) (*FullProbeResult, error)
}
// MediaProbeService 用 ffprobe 提取媒体的基础信息(容器、每路轨道、内嵌章节)
// 并落库缓存。
//
// 定位是「播放时顺带补齐媒体信息」:STRM / 云盘媒体在扫描阶段拿不到时长,而
// 播放链路要用缓存里的时长来换算「延续到片尾」这类区间,详情页将来也直接读这份
// 媒体信息。
//
// 它**不参与片头/片尾判定**:章节标题绝大多数没有语义(生产库实测抽样 64 个
// 文件,命中 0 个),拿它去猜跳过点只会给出错误的位置,时间轴数据仍然只信
// TheIntroDB。
//
// 核心约束:一次探测要 2~4.5 秒(远端直链要跨洋跑几次 HTTP 事务),所以只允许
// 异步跑,播放链路永远只读缓存。
type MediaProbeService struct {
log *zap.Logger
repo *repository.Container
probe mediaProber
// resolve 把 strm 播放目标解析成最终直链(含绑定 UA 的请求头)。与转码、
// 内嵌字幕发现走同一条换链路径,否则会踩到网盘 CDN 的防盗链 403。
resolve func(ctx context.Context, raw, userAgent string) (*StrmPlayResult, error)
mu sync.Mutex
inFlight map[string]struct{}
}
// NewMediaProbeService is the constructor.
func NewMediaProbeService(log *zap.Logger, repo *repository.Container, probe *FFprobeService) *MediaProbeService {
svc := &MediaProbeService{
log: log,
repo: repo,
inFlight: make(map[string]struct{}),
}
if probe != nil {
svc.probe = probe
}
return svc
}
// SetPlayTargetResolver injects the strm → direct-link resolver.
func (s *MediaProbeService) SetPlayTargetResolver(resolve func(ctx context.Context, raw, userAgent string) (*StrmPlayResult, error)) *MediaProbeService {
if s != nil {
s.resolve = resolve
}
return s
}
// EnsureAsync 保证这部媒体的探测已排上队,并立刻返回。
//
// 已经有同一条媒体的探测在跑、或服务未配置好时返回 false;这次新排上一条返回
// true——调用方据此告诉客户端「稍后再拉一次」。
func (s *MediaProbeService) EnsureAsync(m *model.Media) bool {
if s == nil || s.probe == nil || s.repo == nil || m == nil || strings.TrimSpace(m.ID) == "" {
return false
}
if !s.reserve(m.ID) {
return false
}
// 复制一份媒体行:调用方的对象可能属于请求作用域,后台协程不该继续引用它。
snapshot := *m
helper.Go(s.log, "service.mediaProbe", func() {
defer s.release(snapshot.ID)
s.run(context.Background(), &snapshot)
})
return true
}
func (s *MediaProbeService) reserve(mediaID string) bool {
s.mu.Lock()
defer s.mu.Unlock()
if s.inFlight == nil {
s.inFlight = make(map[string]struct{})
}
if _, running := s.inFlight[mediaID]; running {
return false
}
s.inFlight[mediaID] = struct{}{}
return true
}
func (s *MediaProbeService) release(mediaID string) {
s.mu.Lock()
delete(s.inFlight, mediaID)
s.mu.Unlock()
}
// run 执行一次探测并落库。它跑在后台,没有调用方能接收错误,所以任何失败都只
// 记录、不外抛。
func (s *MediaProbeService) run(ctx context.Context, m *model.Media) {
ctx, cancel := context.WithTimeout(ctx, mediaProbeTimeout)
defer cancel()
input, err := s.probeInput(ctx, m)
if err != nil {
s.markFailure(ctx, m.ID, err)
return
}
result, err := s.probe.ProbeFull(ctx, input)
if err != nil {
s.markFailure(ctx, m.ID, err)
return
}
if err := s.persistProbe(ctx, m, result); err != nil {
s.markFailure(ctx, m.ID, err)
}
}
// probeInput 把媒体行解析成 ffprobe 能直接打开的输入。
//
// 本地文件给路径;STRM / 云盘先解析成最终直链并带上绑定的请求头——直链与 UA
// 必须配套,用错会被 CDN 拒绝。
func (s *MediaProbeService) probeInput(ctx context.Context, m *model.Media) (ProbeInput, error) {
if m == nil {
return ProbeInput{}, ErrMediaNotFound
}
if !isStrmMediaRow(m) {
if _, err := os.Stat(m.Path); err != nil {
return ProbeInput{}, ErrMediaNotFound
}
return ProbeInput{Source: m.Path}, nil
}
raw := strings.TrimSpace(m.STRMURL)
if raw == "" && strings.HasSuffix(strings.ToLower(strings.TrimSpace(m.Path)), ".strm") {
parsed, err := readLocalSTRMTarget(m.Path)
if err == nil {
raw = strings.TrimSpace(parsed)
}
}
if raw == "" {
return ProbeInput{}, errors.New("strm play target missing")
}
if s.resolve != nil {
resolved, err := s.resolve(ctx, raw, "")
if err != nil {
return ProbeInput{}, err
}
in, err := transcodeInputFromPlayResult(resolved)
if err != nil {
return ProbeInput{}, err
}
return ProbeInput{Source: in.Source, Headers: in.Headers}, nil
}
if isHTTPPlaybackTarget(raw) {
return ProbeInput{Source: raw}, nil
}
return ProbeInput{}, errors.New("strm probe source unavailable")
}
// persistProbe 把一次成功的探测落库。
func (s *MediaProbeService) persistProbe(ctx context.Context, m *model.Media, result *FullProbeResult) error {
if s.repo == nil || s.repo.MediaProbe == nil {
return errors.New("media probe repository not wired")
}
payload, err := result.PayloadJSON()
if err != nil {
return err
}
row := &model.MediaProbe{
MediaID: m.ID,
Signature: mediaProbeSignature(m),
Source: mediaProbeInputKind(m),
Container: result.Container,
DurationSec: result.DurationSec,
BitRate: result.BitRate,
Width: firstStreamDimension(result, "video", true),
Height: firstStreamDimension(result, "video", false),
VideoCodec: firstStreamCodec(result, "video"),
AudioCodec: firstStreamCodec(result, "audio"),
VideoStreams: len(result.StreamsOfType("video")),
AudioStreams: len(result.StreamsOfType("audio")),
SubtitleStreams: len(result.StreamsOfType("subtitle")),
ChapterCount: len(result.Chapters),
Payload: payload,
ProbedAt: time.Now(),
}
if err := s.repo.MediaProbe.Upsert(ctx, row); err != nil {
return err
}
s.backfillDuration(ctx, m, result.DurationSec)
return nil
}
// backfillDuration 把探测到的时长补进 media.duration_sec。STRM / 云盘媒体在扫描
// 阶段拿不到时长,而末段区间(end_ms = 0)要靠它才能换算出真实结束时间。
func (s *MediaProbeService) backfillDuration(ctx context.Context, m *model.Media, durationSec int) {
if s.repo == nil || s.repo.DB == nil || m == nil || durationSec <= 0 || m.DurationSec > 0 {
return
}
err := s.repo.DB.WithContext(ctx).Model(&model.Media{}).
Where("id = ? AND duration_sec <= 0", m.ID).
Update("duration_sec", durationSec).Error
if err != nil && s.log != nil {
s.log.Debug("backfill probed duration failed", zap.String("media_id", m.ID), zap.Error(err))
}
}
func (s *MediaProbeService) markFailure(ctx context.Context, mediaID string, probeErr error) {
if s == nil || s.repo == nil || probeErr == nil {
return
}
message := truncateProbeError(probeErr)
if err := s.repo.MediaProbe.MarkFailure(ctx, mediaID, message, time.Now()); err != nil && s.log != nil {
s.log.Debug("record media probe failure failed", zap.String("media_id", mediaID), zap.Error(err))
}
if s.log != nil {
s.log.Debug("media probe failed", zap.String("media_id", mediaID), zap.Error(probeErr))
}
}
// truncateProbeError 限制错误信息长度。错误里可能带被拒绝的直链,落库时截断,
// 避免超长并减少敏感内容。
func truncateProbeError(err error) string {
message := strings.TrimSpace(err.Error())
runes := []rune(message)
if len(runes) > mediaProbeErrorLimit {
return string(runes[:mediaProbeErrorLimit])
}
return message
}
// mediaProbeInputKind 记录输入形态,供详情页判断「这个时长是本地读的还是远端读的」。
func mediaProbeInputKind(m *model.Media) string {
if m == nil {
return ""
}
if isStrmMediaRow(m) {
return "strm"
}
return "local"
}
// mediaProbeSignature 是「探的是哪个文件」的指纹。
//
// 本地文件用路径 + 大小 + 修改时间;STRM / 云盘没有本地文件,用固化的播放目标,
// 且绝不能用解析后的直链(每次签名都不同,缓存会永远失效)。整体做哈希:
// 既固定长度,也不把路径或 pickcode 再抄一份进数据库。
func mediaProbeSignature(m *model.Media) string {
if m == nil {
return ""
}
var base string
if isStrmMediaRow(m) {
target := strings.TrimSpace(m.STRMURL)
if target == "" {
target = strings.TrimSpace(m.Path)
}
base = "strm|" + target
} else if info, err := os.Stat(m.Path); err == nil {
base = fmt.Sprintf("local|%s|%d|%d", m.Path, info.Size(), info.ModTime().Unix())
} else {
base = "local|" + m.Path
}
sum := sha256.Sum256([]byte(base))
return hex.EncodeToString(sum[:16])
}
func firstStreamCodec(result *FullProbeResult, kind string) string {
if result == nil {
return ""
}
for _, stream := range result.Streams {
if stream.Type == kind && stream.Codec != "" {
return stream.Codec
}
}
return ""
}
// firstStreamDimension 取第一路指定类型轨道的宽(width=true)或高。
func firstStreamDimension(result *FullProbeResult, kind string, width bool) int {
if result == nil {
return 0
}
for _, stream := range result.Streams {
if stream.Type != kind {
continue
}
if width {
return stream.Width
}
return stream.Height
}
return 0
}
+308
View File
@@ -0,0 +1,308 @@
package service
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"go.uber.org/zap"
"github.com/truewhile/MeBox/internal/model"
"github.com/truewhile/MeBox/internal/repository"
)
// stubProber 是 mediaProber 的测试桩:可以拦住探测(gate)用来验证并发去重,
// 也可以直接返回预置结果或错误。
type stubProber struct {
gate chan struct{}
result *FullProbeResult
err error
mu sync.Mutex
calls int
last ProbeInput
}
func (s *stubProber) ProbeFull(_ context.Context, input ProbeInput) (*FullProbeResult, error) {
s.mu.Lock()
s.calls++
s.last = input
gate := s.gate
result := s.result
err := s.err
s.mu.Unlock()
if gate != nil {
<-gate
}
return result, err
}
func (s *stubProber) callCount() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.calls
}
func (s *stubProber) lastInput() ProbeInput {
s.mu.Lock()
defer s.mu.Unlock()
return s.last
}
func chapterProbeResult() *FullProbeResult {
return &FullProbeResult{
Container: "matroska,webm",
DurationSec: 1451,
BitRate: 8_000_000,
Streams: []ProbeStream{
{Index: 0, Type: "video", Codec: "hevc", Width: 3840, Height: 2160},
{Index: 1, Type: "audio", Codec: "eac3"},
{Index: 2, Type: "subtitle", Codec: "ass"},
},
Chapters: []ProbeChapter{
{Index: 0, StartMs: 0, EndMs: 95_000, Title: "Chapter 01"},
{Index: 1, StartMs: 228_664, EndMs: 246_143, Title: "Opening"},
{Index: 2, StartMs: 3_431_000, EndMs: 0, Title: "End Credits"},
},
}
}
// newProbeFixture 建一条本地媒体(真实落在临时目录里,因为 probeInput 会 stat
// 它)和一个可注入桩的 MediaProbeService。
func newProbeFixture(t *testing.T) (*MediaProbeService, *repository.Container, *model.Media) {
t.Helper()
repos := repository.New(newServiceTestDB(t))
dir := t.TempDir()
path := filepath.Join(dir, "S01E01.mkv")
if err := os.WriteFile(path, []byte("not-really-a-video"), 0o600); err != nil {
t.Fatal(err)
}
m := &model.Media{
Base: model.Base{ID: "ep-1"},
LibraryID: "lib-anime",
SeriesID: "s-1",
Title: "某剧",
Path: path,
SeasonNum: 1,
EpisodeNum: 1,
}
if err := repos.DB.Create(m).Error; err != nil {
t.Fatal(err)
}
svc := NewMediaProbeService(zap.NewNop(), repos, nil)
return svc, repos, m
}
func waitForCondition(t *testing.T, timeout time.Duration, cond func() bool) {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if cond() {
return
}
time.Sleep(5 * time.Millisecond)
}
t.Fatal("condition was not met before the deadline")
}
func TestMediaProbeRunPersistsProbeRow(t *testing.T) {
svc, repos, m := newProbeFixture(t)
prober := &stubProber{result: chapterProbeResult()}
svc.probe = prober
// 直接调 run(而不是 EnsureAsync)以获得确定性的断言:这里要验证的是落库,
// 不是调度。
svc.run(context.Background(), m)
if got := prober.lastInput().Source; got != m.Path {
t.Fatalf("probe source = %q, want the local path %q", got, m.Path)
}
probe, err := repos.MediaProbe.Get(t.Context(), m.ID)
if err != nil {
t.Fatal(err)
}
if probe == nil {
t.Fatal("probe row missing")
}
if probe.Container != "matroska,webm" || probe.DurationSec != 1451 || probe.ChapterCount != 3 {
t.Fatalf("probe summary = %#v", probe)
}
if probe.VideoStreams != 1 || probe.AudioStreams != 1 || probe.SubtitleStreams != 1 {
t.Fatalf("stream counts = %d/%d/%d", probe.VideoStreams, probe.AudioStreams, probe.SubtitleStreams)
}
if probe.Width != 3840 || probe.Height != 2160 || probe.VideoCodec != "hevc" || probe.AudioCodec != "eac3" {
t.Fatalf("probe primaries = %#v", probe)
}
if probe.Source != "local" || probe.Signature == "" {
t.Fatalf("probe source/signature = %q/%q", probe.Source, probe.Signature)
}
if probe.LastError != "" {
t.Fatalf("last error = %q, want empty", probe.LastError)
}
if !strings.Contains(probe.Payload, "hevc") {
t.Fatalf("payload should carry the parsed streams: %s", probe.Payload)
}
// 时长回填:STRM / 云盘媒体扫描时拿不到时长,末段区间要靠它换算结束时间。
var refreshed model.Media
if err := repos.DB.First(&refreshed, "id = ?", m.ID).Error; err != nil {
t.Fatal(err)
}
if refreshed.DurationSec != 1451 {
t.Fatalf("media duration = %d, want the probed 1451", refreshed.DurationSec)
}
}
func TestMediaProbeFailureKeepsPreviousSummary(t *testing.T) {
svc, repos, m := newProbeFixture(t)
prober := &stubProber{result: chapterProbeResult()}
svc.probe = prober
svc.run(context.Background(), m)
prober.err = errors.New("ffprobe full: exit status 1")
svc.run(context.Background(), m)
probe, err := repos.MediaProbe.Get(t.Context(), m.ID)
if err != nil {
t.Fatal(err)
}
if probe == nil || probe.LastError == "" {
t.Fatal("a failed re-probe must record the error")
}
// 关键:失败只更新时间与错误信息,上一次成功的媒体信息必须留着——
// 否则一次失败的重探会把已经拿到的时长(片尾区间换算要用)抹掉。
if probe.DurationSec != 1451 || probe.Container != "matroska,webm" || probe.Payload == "" {
t.Fatalf("a failed re-probe wiped the previous summary: %#v", probe)
}
}
// 探测有结论后就不再重探;失败要等冷却期过去才允许重试。
func TestMediaProbeSettledSkipsReprobeButStaleFailureRetries(t *testing.T) {
if !mediaProbeSettled(&model.MediaProbe{ProbedAt: time.Now()}) {
t.Fatal("a completed probe is settled")
}
if !mediaProbeSettled(&model.MediaProbe{ProbedAt: time.Now(), LastError: "boom"}) {
t.Fatal("a recent failure is settled: it must not be retried on every play")
}
if mediaProbeSettled(&model.MediaProbe{ProbedAt: time.Now().Add(-mediaProbeFailureRetry - time.Minute), LastError: "boom"}) {
t.Fatal("a failure past the cooldown should be retried")
}
if mediaProbeSettled(nil) {
t.Fatal("a missing probe row is not settled")
}
}
func TestMediaProbeEnsureAsyncDedupesInFlightProbes(t *testing.T) {
svc, _, m := newProbeFixture(t)
gate := make(chan struct{})
prober := &stubProber{result: chapterProbeResult(), gate: gate}
svc.probe = prober
if !svc.EnsureAsync(m) {
t.Fatal("first EnsureAsync should enqueue a probe")
}
// 同一条媒体在跑的时候不能重复排队:否则每次播放请求都会再起一次 3 秒探测。
if svc.EnsureAsync(m) {
t.Fatal("second EnsureAsync must report that a probe is already running")
}
close(gate)
waitForCondition(t, 5*time.Second, func() bool {
svc.mu.Lock()
defer svc.mu.Unlock()
return len(svc.inFlight) == 0
})
if got := prober.callCount(); got != 1 {
t.Fatalf("probe calls = %d, want 1", got)
}
// 跑完之后允许再次排队(例如失败冷却期到了之后的重试)。
if !svc.EnsureAsync(m) {
t.Fatal("EnsureAsync should enqueue again once the previous run finished")
}
waitForCondition(t, 5*time.Second, func() bool { return prober.callCount() == 2 })
}
func TestMediaProbeEnsureAsyncWithoutProberIsNotPending(t *testing.T) {
svc, _, m := newProbeFixture(t)
// probe 未注入(ffprobe 不可用时就是这样):不能谎报「提取中」,否则客户端
// 会白轮询一轮。
if svc.EnsureAsync(m) {
t.Fatal("EnsureAsync must return false without a prober")
}
}
func TestMediaProbeInputRejectsMissingLocalFile(t *testing.T) {
svc, _, _ := newProbeFixture(t)
_, err := svc.probeInput(t.Context(), &model.Media{
Base: model.Base{ID: "missing"}, Path: filepath.Join(t.TempDir(), "nope.mkv"),
})
if !errors.Is(err, ErrMediaNotFound) {
t.Fatalf("err = %v, want ErrMediaNotFound", err)
}
}
func TestMediaProbeInputResolvesStrmWithHeaders(t *testing.T) {
svc := &MediaProbeService{}
svc.SetPlayTargetResolver(func(_ context.Context, raw, userAgent string) (*StrmPlayResult, error) {
if raw != "https://pan.example.com/api/strm/play/115?v=1" {
t.Fatalf("resolver got raw = %q", raw)
}
if userAgent != "" {
t.Fatalf("userAgent = %q, want empty for a background probe", userAgent)
}
return &StrmPlayResult{RedirectURL: "https://cdn.example.com/a.mkv"}, nil
})
input, err := svc.probeInput(t.Context(), &model.Media{
Base: model.Base{ID: "strm-1"},
Path: "/media/a.mkv.strm",
STRMURL: "https://pan.example.com/api/strm/play/115?v=1",
})
if err != nil {
t.Fatalf("probeInput: %v", err)
}
if input.Source != "https://cdn.example.com/a.mkv" {
t.Fatalf("source = %q, want the resolved direct link", input.Source)
}
}
// 签名不能把播放目标(含 pickcode)再抄一份进数据库,所以整体做哈希。
func TestMediaProbeSignatureHidesPlayTargetAndTracksFileChanges(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "movie.mkv")
if err := os.WriteFile(path, []byte("v1"), 0o600); err != nil {
t.Fatal(err)
}
local := &model.Media{Base: model.Base{ID: "l-1"}, Path: path}
first := mediaProbeSignature(local)
if len(first) != 32 {
t.Fatalf("signature = %q, want a 32-char hex digest", first)
}
if strings.Contains(first, "movie.mkv") {
t.Fatalf("signature leaked the path: %q", first)
}
if err := os.WriteFile(path, []byte("v2-changed-size"), 0o600); err != nil {
t.Fatal(err)
}
if mediaProbeSignature(local) == first {
t.Fatal("the signature must change when the file changes")
}
strm := &model.Media{
Base: model.Base{ID: "s-1"},
Path: "/media/movie.mkv.strm",
STRMURL: "https://pan.example.com/api/strm/play/115?pickcode=secret-pickcode",
}
strmSig := mediaProbeSignature(strm)
if strings.Contains(strmSig, "secret-pickcode") || strings.Contains(strmSig, "pan.example.com") {
t.Fatalf("strm signature leaked the play target: %q", strmSig)
}
if strmSig == "" {
t.Fatal("strm signature must not be empty")
}
}
+296 -25
View File
@@ -6,6 +6,7 @@ package service
import (
"context"
"strings"
"time"
"go.uber.org/zap"
@@ -17,9 +18,21 @@ import (
// 片段数据的缓存时长。命中过说明社区库里已有记录、数据很少变动,可以放很久;
// 未命中说明这部片还没人贡献,隔一段时间再试一次即可——负缓存是必须的,否则
// 每次播放一部没有片段数据的影片都会打一次外网。
//
// 未命中 TTL 刻意短于命中:社区库在持续补充,尤其是热门剧,24h 重试一次比
// 锁死 7 天更能跟上贡献节奏;预热任务也会优先扫最近播放过的 miss。
const (
segmentFoundTTL = 30 * 24 * time.Hour
segmentMissingTTL = 7 * 24 * time.Hour
segmentMissingTTL = 24 * time.Hour
// SegmentSourceManual / Propagated 与 IntroDBSource 并列,播放时按优先级合并。
SegmentSourceManual = "manual"
SegmentSourcePropagated = "propagated"
// segmentPrewarmDefaultLimit 是一次预热任务最多处理的媒体数,避免单次跑太久。
segmentPrewarmDefaultLimit = 200
// segmentPrewarmInterval 是预热请求之间的间隔,压低对 TheIntroDB 的 429。
segmentPrewarmInterval = time.Second
)
// SegmentView 是播放器消费的最小片段结构,避免把库内字段(source 等)暴露给前端。
@@ -43,11 +56,29 @@ type MediaSegmentService struct {
log *zap.Logger
repo *repository.Container
introdb *IntroDBService
// probe 负责异步补齐媒体信息(主要是 STRM 时长)。未注入时只用 TheIntroDB。
probe *MediaProbeService
// prewarmGap 是预热两次 IntroDB 请求之间的间隔;测试可设为 0。
prewarmGap time.Duration
now func() time.Time
}
// NewMediaSegmentService is the constructor.
func NewMediaSegmentService(log *zap.Logger, repo *repository.Container) *MediaSegmentService {
return &MediaSegmentService{log: log, repo: repo}
return &MediaSegmentService{
log: log,
repo: repo,
prewarmGap: segmentPrewarmInterval,
now: time.Now,
}
}
// SetPrewarmGap overrides the delay between prewarm fetches (tests use 0).
func (s *MediaSegmentService) SetPrewarmGap(gap time.Duration) *MediaSegmentService {
if s != nil {
s.prewarmGap = gap
}
return s
}
// SetIntroDB wires the provider. Without it the service only reads cached rows.
@@ -58,36 +89,139 @@ func (s *MediaSegmentService) SetIntroDB(p *IntroDBService) *MediaSegmentService
return s
}
// SetProbe wires the async media probe (duration backfill for STRM). Without it
// open-ended credits still work once duration is known from elsewhere.
func (s *MediaSegmentService) SetProbe(p *MediaProbeService) *MediaSegmentService {
if s != nil {
s.probe = p
}
return s
}
// ListForPlayback returns the segments known for a media item, refreshing from
// the provider when the cache is stale.
//
// 它不做任何阻塞起播的事情——调用方是在播放已经开始之后用一次独立请求进来的,
// 抓取失败也只是少一个「跳过片头」按钮,绝不能让播放报错。
//
// 时间轴数据只来自 TheIntroDB。顺带在返回前起一次异步探测补齐媒体信息(主要是
// STRM 媒体的时长),但探测结果不参与片段判定,详见 MediaProbeService 的说明。
func (s *MediaSegmentService) ListForPlayback(ctx context.Context, m *model.Media) ([]model.MediaSegment, error) {
if s == nil || s.repo == nil || m == nil || m.ID == "" {
return nil, nil
}
cached, err := s.repo.MediaSegment.ListByMedia(ctx, m.ID)
if err != nil {
return nil, err
// 用 defer 保证探测一定在本次请求所有数据库读写之后才启动:后台探测自己也要
// 写库,若在本次写事务还没结束时启动,两个写事务会抢同一把锁(SQLite 下就是
// SQLITE_BUSY,实测能直接把社区库的落库打失败)。
defer s.ensureMediaProbe(ctx, m)
return s.introDBSegments(ctx, m)
}
// ensureMediaProbe 在还没探过(或上次失败已过冷却期)时起一次异步探测。
//
// 它只为「补齐媒体信息」服务:失败只是拿不到时长,不影响播放,也不影响片段。
func (s *MediaSegmentService) ensureMediaProbe(ctx context.Context, m *model.Media) {
if s == nil || s.probe == nil || m == nil {
return
}
cached, err := s.repo.MediaProbe.Get(ctx, m.ID)
if err != nil {
s.debug("get media probe failed", m.ID, err)
return
}
if mediaProbeSettled(cached) {
return
}
s.probe.EnsureAsync(m)
}
// mediaProbeSettled 判断这部媒体的探测是否已经「有结论」——成功过,或者失败但还在
// 冷却期内。有结论就不必再探;失败且已过冷却期时返回 false,让下一次播放重试。
func mediaProbeSettled(row *model.MediaProbe) bool {
if row == nil {
return false
}
if strings.TrimSpace(row.LastError) == "" {
return true
}
return time.Since(row.ProbedAt) < mediaProbeFailureRetry
}
// introDBSegments 读社区库的片段,缓存过期时按调用方的预算抓一次并落库。
// 返回值是按来源优先级合并后的结果(manual > theintrodb > propagated)。
func (s *MediaSegmentService) introDBSegments(ctx context.Context, m *model.Media) ([]model.MediaSegment, error) {
ledger, err := s.repo.MediaSegment.GetFetch(ctx, m.ID, IntroDBSource)
if err != nil {
// 调用方预算耗尽时仍读本地缓存,绝不能让播放接口报错。
if ctx.Err() != nil {
return s.mergeForPlayback(context.WithoutCancel(ctx), m.ID)
}
return nil, err
}
if ledger != nil && ledgerFresh(ledger) {
return cached, nil
if ledger == nil || !ledgerFresh(ledger) {
if _, _, err := s.refresh(ctx, m); err != nil {
// 社区库不可达或返回异常:沿用已有缓存,不影响播放;也不写负缓存。
logIntroDBFailure(s.log, 0, err)
}
}
refreshed, attempted, err := s.refresh(ctx, m)
// 合并读库只碰本地,脱离调用方 deadline,避免外网抓取耗尽预算后读缓存也失败。
return s.mergeForPlayback(context.WithoutCancel(ctx), m.ID)
}
// mergeForPlayback 合并同一媒体上多来源片段。同 kind 只保留优先级最高的来源。
func (s *MediaSegmentService) mergeForPlayback(ctx context.Context, mediaID string) ([]model.MediaSegment, error) {
rows, err := s.repo.MediaSegment.ListByMedia(ctx, mediaID)
if err != nil {
// 社区库不可达或返回异常:沿用已有缓存,不影响播放。
logIntroDBFailure(s.log, 0, err)
return cached, nil
return nil, err
}
if !attempted {
return cached, nil
return preferSegmentsBySource(rows), nil
}
// segmentSourcePriority 数值越大越优先。未知来源视为最低,避免挡住已知源。
func segmentSourcePriority(source string) int {
switch source {
case SegmentSourceManual:
return 3
case IntroDBSource:
return 2
case SegmentSourcePropagated:
return 1
default:
return 0
}
return refreshed, nil
}
// preferSegmentsBySource 按 kind 选取最高优先级来源的全部区间。
func preferSegmentsBySource(rows []model.MediaSegment) []model.MediaSegment {
bestPri := make(map[string]int, 4)
byKind := make(map[string][]model.MediaSegment, 4)
for _, row := range rows {
pri := segmentSourcePriority(row.Source)
cur, seen := bestPri[row.Kind]
if !seen || pri > cur {
bestPri[row.Kind] = pri
byKind[row.Kind] = []model.MediaSegment{row}
continue
}
if pri == cur {
byKind[row.Kind] = append(byKind[row.Kind], row)
}
}
out := make([]model.MediaSegment, 0, len(rows))
for _, kind := range []string{
model.SegmentKindIntro, model.SegmentKindRecap,
model.SegmentKindCredits, model.SegmentKindPreview,
} {
out = append(out, byKind[kind]...)
}
return out
}
func (s *MediaSegmentService) debug(message, mediaID string, err error) {
if s == nil || s.log == nil {
return
}
s.log.Debug(message, zap.String("media_id", mediaID), zap.Error(err))
}
// refresh 向提供方查询并落库,返回 (rows, 是否真的发起过查询, error)。
@@ -148,22 +282,124 @@ func (s *MediaSegmentService) refresh(ctx context.Context, m *model.Media) ([]mo
}); err != nil {
return nil, true, err
}
s.propagateIntroToSeason(fetchCtx, m, rows)
return rows, true, nil
}
// queryIDs resolves the provider query key. Movies use their own TMDb id;
// episodes need the *series* TMDb id plus season/episode, because scraping
// stores the episode-level TMDb id on Media.TMDbID.
// propagateIntroToSeason 把本集 IntroDB 命中的 intro 复制到同季还没有
// theintrodb intro 的兄弟集。片尾/预告不传播:各集时长与片尾位置经常不同。
func (s *MediaSegmentService) propagateIntroToSeason(ctx context.Context, m *model.Media, rows []model.MediaSegment) {
if s == nil || s.repo == nil || m == nil || m.SeasonNum <= 0 {
return
}
intros := introSpansFrom(rows)
if len(intros) == 0 {
return
}
siblings, err := s.repo.Media.ListSeasonSiblings(ctx, m)
if err != nil {
s.debug("list season siblings failed", m.ID, err)
return
}
for i := range siblings {
sib := &siblings[i]
existing, err := s.repo.MediaSegment.ListByMediaSource(ctx, sib.ID, IntroDBSource)
if err != nil {
s.debug("list sibling introdb segments failed", sib.ID, err)
continue
}
if hasSegmentKind(existing, model.SegmentKindIntro) {
continue
}
copied := make([]model.MediaSegment, 0, len(intros))
for _, intro := range intros {
copied = append(copied, model.MediaSegment{
MediaID: sib.ID,
SeriesID: sib.SeriesID,
Kind: model.SegmentKindIntro,
StartMs: intro.StartMs,
EndMs: intro.EndMs,
Source: SegmentSourcePropagated,
})
}
if err := s.repo.MediaSegment.ReplaceForMedia(ctx, sib.ID, SegmentSourcePropagated, copied); err != nil {
s.debug("propagate intro failed", sib.ID, err)
}
}
}
func introSpansFrom(rows []model.MediaSegment) []model.MediaSegment {
out := make([]model.MediaSegment, 0, 1)
for _, row := range rows {
if row.Kind == model.SegmentKindIntro {
out = append(out, row)
}
}
return out
}
func hasSegmentKind(rows []model.MediaSegment, kind string) bool {
for _, row := range rows {
if row.Kind == kind {
return true
}
}
return false
}
// Prewarm 批量向 TheIntroDB 补齐过期/缺失账本的可查询媒体。返回实际发起过查询的数量。
func (s *MediaSegmentService) Prewarm(ctx context.Context, limit int) (int, error) {
if s == nil || s.repo == nil || s.introdb == nil {
return 0, nil
}
if limit <= 0 {
limit = segmentPrewarmDefaultLimit
}
now := time.Now()
if s.now != nil {
now = s.now()
}
candidates, err := s.repo.MediaSegment.ListPrewarmCandidates(
ctx, IntroDBSource, now.Add(-segmentMissingTTL), now.Add(-segmentFoundTTL), limit,
)
if err != nil {
return 0, err
}
attempted := 0
for i := range candidates {
if err := ctx.Err(); err != nil {
return attempted, err
}
m := &candidates[i]
_, did, err := s.refresh(ctx, m)
if err != nil {
logIntroDBFailure(s.log, m.TMDbID, err)
}
if did {
attempted++
}
if i+1 < len(candidates) && s.prewarmGap > 0 {
if !waitForIntroDBRetry(ctx, s.prewarmGap) {
return attempted, ctx.Err()
}
}
}
return attempted, nil
}
// queryIDs resolves the provider query key. Movies use their own TMDb id.
//
// 剧集需要「剧集级」TMDb id 加季/集。优先取 Series.TMDbID;但有些刮削路径
// 不建 Series 行,而是把剧集级 id 直接写在 Media.TMDbID 上(生产环境动漫库
// 实测如此:同一剧名下各集共用同一个 id,52/52 个剧名都唯一)。这类行原先
// 一律解析不出 id,等于整库查不到任何片段,所以这里补一条兜底。
//
// 兜底必须验证「是不是剧集级 id」:Media.TMDbID 在另一些刮削路径下存的是
// 单集自己的 id,拿它去查会命中别的片子。判据是多集共用(见
// MediaRepository.ExistsSiblingWithTMDbID)——单集 id 不会在兄弟集上重复。
func (s *MediaSegmentService) queryIDs(ctx context.Context, m *model.Media) (tmdbID, season, episode int) {
if m.SeasonNum > 0 || m.EpisodeNum > 0 {
if m.SeriesID == "" || m.SeasonNum <= 0 || m.EpisodeNum <= 0 {
return 0, 0, 0
}
series, err := s.repo.Series.FindByID(ctx, m.SeriesID)
if err != nil || series == nil || series.TMDbID <= 0 {
return 0, 0, 0
}
return series.TMDbID, m.SeasonNum, m.EpisodeNum
return s.episodeQueryIDs(ctx, m)
}
if m.TMDbID > 0 {
return m.TMDbID, 0, 0
@@ -171,6 +407,41 @@ func (s *MediaSegmentService) queryIDs(ctx context.Context, m *model.Media) (tmd
return 0, 0, 0
}
func (s *MediaSegmentService) episodeQueryIDs(ctx context.Context, m *model.Media) (tmdbID, season, episode int) {
if m.SeasonNum <= 0 || m.EpisodeNum <= 0 {
return 0, 0, 0
}
if seriesTMDbID := s.seriesTMDbID(ctx, m); seriesTMDbID > 0 {
return seriesTMDbID, m.SeasonNum, m.EpisodeNum
}
if !s.mediaTMDbIDLooksLikeSeries(ctx, m) {
return 0, 0, 0
}
return m.TMDbID, m.SeasonNum, m.EpisodeNum
}
// seriesTMDbID returns the series-level TMDb id, or 0 when the row has no
// Series association or that Series was never matched.
func (s *MediaSegmentService) seriesTMDbID(ctx context.Context, m *model.Media) int {
if strings.TrimSpace(m.SeriesID) == "" {
return 0
}
series, err := s.repo.Series.FindByID(ctx, m.SeriesID)
if err != nil || series == nil || series.TMDbID <= 0 {
return 0
}
return series.TMDbID
}
// mediaTMDbIDLooksLikeSeries reports whether Media.TMDbID can stand in for the
// series id: only an id shared by other episodes of the same show qualifies.
func (s *MediaSegmentService) mediaTMDbIDLooksLikeSeries(ctx context.Context, m *model.Media) bool {
if s == nil || s.repo == nil || m == nil || m.TMDbID <= 0 {
return false
}
return s.repo.Media.ExistsSiblingWithTMDbID(ctx, m)
}
// ledgerFresh reports whether a previous lookup is still within its TTL.
func ledgerFresh(row *model.MediaSegmentFetch) bool {
if row == nil {
@@ -0,0 +1,77 @@
package service
import (
"testing"
"time"
"go.uber.org/zap"
)
// 播放请求要顺带把媒体信息(主要是 STRM 媒体的时长)补齐,但探测不能影响片段:
// ListForPlayback 只读社区库,探测在返回前才异步排上队。
func TestListForPlaybackTriggersMediaProbe(t *testing.T) {
probeSvc, repos, m := newProbeFixture(t)
prober := &stubProber{result: chapterProbeResult()}
probeSvc.probe = prober
segments := NewMediaSegmentService(zap.NewNop(), repos).SetProbe(probeSvc)
rows, err := segments.ListForPlayback(t.Context(), m)
if err != nil {
t.Fatalf("ListForPlayback: %v", err)
}
if len(rows) != 0 {
t.Fatalf("rows = %#v, want none without a provider", rows)
}
// 探测是异步的:等它跑完并落库。
waitForCondition(t, 5*time.Second, func() bool { return prober.callCount() >= 1 })
probeRow, err := repos.MediaProbe.Get(t.Context(), m.ID)
if err != nil {
t.Fatal(err)
}
if probeRow == nil {
t.Fatal("the playback path should have probed the media info")
}
// 已经有结论的媒体不该被反复探测:每次播放重跑一次 2~4.5 秒的远端读取太贵。
if _, err := segments.ListForPlayback(t.Context(), m); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
if got := prober.callCount(); got != 1 {
t.Fatalf("probe calls = %d, want 1 (a settled probe must not repeat)", got)
}
}
// 探测失败也要有结论:冷却期内不再重探。
func TestListForPlaybackDoesNotReprobeWithinFailureCooldown(t *testing.T) {
probeSvc, repos, m := newProbeFixture(t)
prober := &stubProber{result: chapterProbeResult()}
probeSvc.probe = prober
if err := repos.MediaProbe.MarkFailure(t.Context(), m.ID, "boom", time.Now()); err != nil {
t.Fatal(err)
}
segments := NewMediaSegmentService(zap.NewNop(), repos).SetProbe(probeSvc)
if _, err := segments.ListForPlayback(t.Context(), m); err != nil {
t.Fatal(err)
}
time.Sleep(50 * time.Millisecond)
if got := prober.callCount(); got != 0 {
t.Fatalf("probe calls = %d, want 0 inside the cooldown", got)
}
}
// 没注入探测服务时(ffprobe 不可用的精简部署)播放链路必须照常工作。
func TestListForPlaybackWithoutProbeStillWorks(t *testing.T) {
_, repos, m := newProbeFixture(t)
segments := NewMediaSegmentService(zap.NewNop(), repos)
rows, err := segments.ListForPlayback(t.Context(), m)
if err != nil {
t.Fatalf("ListForPlayback: %v", err)
}
if len(rows) != 0 {
t.Fatalf("rows = %#v, want none", rows)
}
}
+243
View File
@@ -92,6 +92,131 @@ func TestQueryIDsIsNotResolvableBeforeScrape(t *testing.T) {
}
}
// 部分刮削路径(生产环境动漫库实测如此)不建 Series 行,而是把「剧集级」
// TMDb id 直接写在 Media.TMDbID 上:同一剧名下各集共用同一个 id。
// 原先这类行一律解析不出 id,整个动漫库等于查不到任何片段。
func TestQueryIDsFallsBackToMediaTMDbWhenSiblingsShareIt(t *testing.T) {
repos := repository.New(newServiceTestDB(t))
svc := NewMediaSegmentService(zap.NewNop(), repos)
ctx := t.Context()
episodes := []*model.Media{
{Base: model.Base{ID: "ep-6"}, LibraryID: "lib-anime", Title: "便·当", Path: "/anime/ben-to/S01E06.mkv", SeasonNum: 1, EpisodeNum: 6, TMDbID: 61970},
{Base: model.Base{ID: "ep-7"}, LibraryID: "lib-anime", Title: "便·当", Path: "/anime/ben-to/S01E07.mkv", SeasonNum: 1, EpisodeNum: 7, TMDbID: 61970},
}
for _, ep := range episodes {
if err := repos.DB.Create(ep).Error; err != nil {
t.Fatal(err)
}
}
tmdbID, season, episode := svc.queryIDs(ctx, episodes[0])
if tmdbID != 61970 || season != 1 || episode != 6 {
t.Fatalf("query = (%d,%d,%d), want (61970,1,6): a shared id is a series id", tmdbID, season, episode)
}
}
// 反例(重要):另一些刮削路径把「单集自己的」id 写在 Media.TMDbID 上,
// 每集都不同。这种 id 不能当剧集 id 用——拿它去查会命中完全不相干的片子。
func TestQueryIDsRejectsPerEpisodeTMDbWithoutSiblings(t *testing.T) {
repos := repository.New(newServiceTestDB(t))
svc := NewMediaSegmentService(zap.NewNop(), repos)
ctx := t.Context()
episodes := []*model.Media{
{Base: model.Base{ID: "ep-1"}, LibraryID: "lib-tv", Title: "某剧", Path: "/tv/some/S01E01.mkv", SeasonNum: 1, EpisodeNum: 1, TMDbID: 4_375_419},
{Base: model.Base{ID: "ep-2"}, LibraryID: "lib-tv", Title: "某剧", Path: "/tv/some/S01E02.mkv", SeasonNum: 1, EpisodeNum: 2, TMDbID: 4_375_420},
}
for _, ep := range episodes {
if err := repos.DB.Create(ep).Error; err != nil {
t.Fatal(err)
}
}
if tmdbID, _, _ := svc.queryIDs(ctx, episodes[0]); tmdbID != 0 {
t.Fatalf("tmdbID = %d, want 0: a per-episode id must not be used as a series id", tmdbID)
}
}
// Series 行存在时永远优先,哪怕 Media.TMDbID 看起来也像个共用 id。
func TestQueryIDsPrefersSeriesTMDbOverSharedMediaTMDb(t *testing.T) {
repos := repository.New(newServiceTestDB(t))
svc := NewMediaSegmentService(zap.NewNop(), repos)
ctx := t.Context()
if err := repos.DB.Create(&model.Series{
Base: model.Base{ID: "s-1"}, Title: "便·当", TMDbID: 1396,
}).Error; err != nil {
t.Fatal(err)
}
episodes := []*model.Media{
{Base: model.Base{ID: "ep-a"}, SeriesID: "s-1", LibraryID: "lib-anime", Title: "便·当", Path: "/anime/ben-to/S01E06.mkv", SeasonNum: 1, EpisodeNum: 6, TMDbID: 61970},
{Base: model.Base{ID: "ep-b"}, SeriesID: "s-1", LibraryID: "lib-anime", Title: "便·当", Path: "/anime/ben-to/S01E07.mkv", SeasonNum: 1, EpisodeNum: 7, TMDbID: 61970},
}
for _, ep := range episodes {
if err := repos.DB.Create(ep).Error; err != nil {
t.Fatal(err)
}
}
if tmdbID, _, _ := svc.queryIDs(ctx, episodes[0]); tmdbID != 1396 {
t.Fatalf("tmdbID = %d, want the Series id 1396", tmdbID)
}
}
// 关联了 Series 但那条 Series 没刮到 id 时,仍然走 Media.TMDbID 兜底。
func TestQueryIDsFallsBackWhenSeriesHasNoTMDb(t *testing.T) {
repos := repository.New(newServiceTestDB(t))
svc := NewMediaSegmentService(zap.NewNop(), repos)
ctx := t.Context()
if err := repos.DB.Create(&model.Series{
Base: model.Base{ID: "s-2"}, Title: "便·当", TMDbID: 0,
}).Error; err != nil {
t.Fatal(err)
}
episodes := []*model.Media{
{Base: model.Base{ID: "ep-c"}, SeriesID: "s-2", Path: "/anime/ben-to/S01E06.mkv", SeasonNum: 1, EpisodeNum: 6, TMDbID: 61970},
{Base: model.Base{ID: "ep-d"}, SeriesID: "s-2", Path: "/anime/ben-to/S01E07.mkv", SeasonNum: 1, EpisodeNum: 7, TMDbID: 61970},
}
for _, ep := range episodes {
if err := repos.DB.Create(ep).Error; err != nil {
t.Fatal(err)
}
}
if tmdbID, _, _ := svc.queryIDs(ctx, episodes[0]); tmdbID != 61970 {
t.Fatalf("tmdbID = %d, want the shared Media id 61970", tmdbID)
}
}
func TestListForPlaybackQueriesEpisodesWithSharedSeriesTMDb(t *testing.T) {
svc, repos, calls := newSegmentServiceFixture(t, writeJSONBody(introDBTVPayload))
ctx := t.Context()
episodes := []*model.Media{
{Base: model.Base{ID: "ep-x"}, LibraryID: "lib-anime", Title: "便·当",
Path: "/anime/ben-to/S01E06.mkv", SeasonNum: 1, EpisodeNum: 6, TMDbID: 61970},
{Base: model.Base{ID: "ep-y"}, LibraryID: "lib-anime", Title: "便·当",
Path: "/anime/ben-to/S01E07.mkv", SeasonNum: 1, EpisodeNum: 7, TMDbID: 61970},
}
for _, ep := range episodes {
if err := repos.DB.Create(ep).Error; err != nil {
t.Fatal(err)
}
}
rows, err := svc.ListForPlayback(ctx, episodes[0])
if err != nil {
t.Fatalf("call: %v", err)
}
if len(rows) != 2 {
t.Fatalf("rows = %#v, want the two spans the provider returned", rows)
}
if got := atomic.LoadInt32(calls); got != 1 {
t.Fatalf("provider calls = %d, want 1", got)
}
}
func TestListForPlaybackFetchesOnceThenServesCache(t *testing.T) {
svc, repos, calls := newSegmentServiceFixture(t, writeJSONBody(introDBMoviePayload))
ctx := t.Context()
@@ -260,6 +385,123 @@ func TestListForPlaybackRespectsCallerDeadline(t *testing.T) {
}
}
func TestPreferSegmentsBySourcePrefersManualThenIntroDBThenPropagated(t *testing.T) {
rows := []model.MediaSegment{
{Kind: model.SegmentKindIntro, StartMs: 1, EndMs: 2, Source: SegmentSourcePropagated},
{Kind: model.SegmentKindIntro, StartMs: 10, EndMs: 20, Source: IntroDBSource},
{Kind: model.SegmentKindIntro, StartMs: 100, EndMs: 200, Source: SegmentSourceManual},
{Kind: model.SegmentKindCredits, StartMs: 1000, EndMs: 0, Source: SegmentSourcePropagated},
{Kind: model.SegmentKindCredits, StartMs: 2000, EndMs: 0, Source: IntroDBSource},
}
got := preferSegmentsBySource(rows)
if len(got) != 2 {
t.Fatalf("got %#v, want manual intro + introdb credits", got)
}
if got[0].Source != SegmentSourceManual || got[0].StartMs != 100 {
t.Fatalf("intro = %#v, want manual", got[0])
}
if got[1].Source != IntroDBSource || got[1].StartMs != 2000 {
t.Fatalf("credits = %#v, want theintrodb", got[1])
}
}
func TestListForPlaybackPropagatesIntroToSeasonSiblings(t *testing.T) {
svc, repos, _ := newSegmentServiceFixture(t, writeJSONBody(introDBTVPayload))
svc.SetPrewarmGap(0)
ctx := t.Context()
episodes := []*model.Media{
{Base: model.Base{ID: "ep-1"}, LibraryID: "lib-anime", Title: "便·当",
Path: "/anime/S01E01.mkv", SeasonNum: 1, EpisodeNum: 1, TMDbID: 61970},
{Base: model.Base{ID: "ep-2"}, LibraryID: "lib-anime", Title: "便·当",
Path: "/anime/S01E02.mkv", SeasonNum: 1, EpisodeNum: 2, TMDbID: 61970},
}
for _, ep := range episodes {
if err := repos.DB.Create(ep).Error; err != nil {
t.Fatal(err)
}
}
rows, err := svc.ListForPlayback(ctx, episodes[0])
if err != nil {
t.Fatal(err)
}
if len(rows) != 2 {
t.Fatalf("ep1 rows = %#v, want intro+credits from provider", rows)
}
sib, err := repos.MediaSegment.ListByMediaSource(ctx, "ep-2", SegmentSourcePropagated)
if err != nil {
t.Fatal(err)
}
if len(sib) != 1 || sib[0].Kind != model.SegmentKindIntro {
t.Fatalf("sibling propagated = %#v, want intro only", sib)
}
if sib[0].StartMs != 228_664 || sib[0].EndMs != 246_143 {
t.Fatalf("propagated window = %#v", sib[0])
}
// 兄弟集播放时应直接看到传播来的 intro(即使自己还没打过 IntroDB)。
if err := repos.MediaSegment.UpsertFetch(ctx, &model.MediaSegmentFetch{
MediaID: "ep-2", Source: IntroDBSource, FetchedAt: time.Now(), Found: false,
}); err != nil {
t.Fatal(err)
}
merged, err := svc.ListForPlayback(ctx, episodes[1])
if err != nil {
t.Fatal(err)
}
if len(merged) != 1 || merged[0].Kind != model.SegmentKindIntro || merged[0].Source != SegmentSourcePropagated {
t.Fatalf("ep2 playback = %#v, want propagated intro", merged)
}
}
func TestPrewarmFetchesStaleMissesPreferringRecentPlays(t *testing.T) {
svc, repos, calls := newSegmentServiceFixture(t, writeJSONBody(introDBMoviePayload))
svc.SetPrewarmGap(0)
ctx := t.Context()
old := &model.Media{Base: model.Base{ID: "mv-old"}, Path: "/a.mkv", TMDbID: 111}
hot := &model.Media{Base: model.Base{ID: "mv-hot"}, Path: "/b.mkv", TMDbID: 27205}
for _, m := range []*model.Media{old, hot} {
if err := repos.DB.Create(m).Error; err != nil {
t.Fatal(err)
}
}
// 两条都是过期 miss,热播的应优先被预热。
stale := time.Now().Add(-segmentMissingTTL - time.Hour)
for _, id := range []string{"mv-old", "mv-hot"} {
if err := repos.MediaSegment.UpsertFetch(ctx, &model.MediaSegmentFetch{
MediaID: id, Source: IntroDBSource, FetchedAt: stale, Found: false,
}); err != nil {
t.Fatal(err)
}
}
if err := repos.DB.Create(&model.PlaybackHistory{
Base: model.Base{ID: "h-1"}, UserID: "u1", MediaID: "mv-hot",
}).Error; err != nil {
t.Fatal(err)
}
n, err := svc.Prewarm(ctx, 1)
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Fatalf("attempted = %d, want 1", n)
}
if got := atomic.LoadInt32(calls); got != 1 {
t.Fatalf("provider calls = %d, want 1", got)
}
ledger, err := repos.MediaSegment.GetFetch(ctx, "mv-hot", IntroDBSource)
if err != nil || ledger == nil || !ledger.Found {
t.Fatalf("hot ledger = %#v err=%v, want found", ledger, err)
}
oldLedger, err := repos.MediaSegment.GetFetch(ctx, "mv-old", IntroDBSource)
if err != nil || oldLedger == nil || oldLedger.Found {
t.Fatalf("old ledger should remain a miss, got %#v err=%v", oldLedger, err)
}
}
func TestLedgerFreshUsesLongerTTLWhenDataWasFound(t *testing.T) {
now := time.Now()
found := &model.MediaSegmentFetch{FetchedAt: now.Add(-segmentMissingTTL), Found: true}
@@ -274,3 +516,4 @@ func TestLedgerFreshUsesLongerTTLWhenDataWasFound(t *testing.T) {
t.Fatal("a missing ledger must not be considered fresh")
}
}
+7 -1
View File
@@ -287,7 +287,7 @@ func (s *MediaService) ListLibrarySeriesEpisodes(ctx context.Context, libraryID,
return out, nil
}
// sortEpisodesForDisplay 与历史行为一致:季/集号升序,再按入库时间兜底。
// sortEpisodesForDisplay 与历史行为一致:季/集号升序(含半集的小数部分),再按入库时间兜底。
func sortEpisodesForDisplay(out []model.Media) {
sort.SliceStable(out, func(i, j int) bool {
if out[i].SeasonNum != out[j].SeasonNum {
@@ -296,6 +296,9 @@ func sortEpisodesForDisplay(out []model.Media) {
if out[i].EpisodeNum != out[j].EpisodeNum {
return out[i].EpisodeNum < out[j].EpisodeNum
}
if out[i].EpisodeFraction != out[j].EpisodeFraction {
return out[i].EpisodeFraction < out[j].EpisodeFraction
}
return out[i].CreatedAt.Before(out[j].CreatedAt)
})
}
@@ -403,6 +406,9 @@ func (s *MediaService) ListMediaEpisodes(ctx context.Context, mediaID string, vi
if out[i].EpisodeNum != out[j].EpisodeNum {
return out[i].EpisodeNum < out[j].EpisodeNum
}
if out[i].EpisodeFraction != out[j].EpisodeFraction {
return out[i].EpisodeFraction < out[j].EpisodeFraction
}
if out[i].Path != out[j].Path {
return out[i].Path < out[j].Path
}
+15 -8
View File
@@ -134,6 +134,9 @@ func GroupEpisodeVersionsForDisplay(items []model.Media) []MediaItem {
if grouped[i].EpisodeNum != grouped[j].EpisodeNum {
return grouped[i].EpisodeNum < grouped[j].EpisodeNum
}
if grouped[i].EpisodeFraction != grouped[j].EpisodeFraction {
return grouped[i].EpisodeFraction < grouped[j].EpisodeFraction
}
return grouped[i].CreatedAt.Before(grouped[j].CreatedAt)
})
return grouped
@@ -192,7 +195,7 @@ func adultVouchedGroupKey(m model.Media, vouched map[string]bool) string {
return fmt.Sprintf("adult:%s:%s", libKey, code)
}
func mediaVersionGroupKey(m model.Media) string { // 远程 Emby 挂载条目保持独立,不与其它远程条目或本地条目折叠合并。
func mediaVersionGroupKey(m model.Media) string { // 远程 Emby 挂载条目保持独立,不与其它远程条目或本地条目折叠合并。
if IsEmbyRemoteID(m.ID) {
return fmt.Sprintf("embyremote:%s", m.ID)
}
@@ -200,11 +203,15 @@ func mediaVersionGroupKey(m model.Media) string { // 远程 Emby 挂载条目保
libKey := mediaVersionLibraryKey(m)
specialKind := mediaSpecialKind(m.Path)
season, episode := m.SeasonNum, m.EpisodeNum
fraction := m.EpisodeFraction
if specialKind != "" && specialKind != mediaSpecialTheatrical && episode <= 0 {
if parsedSeason, parsedEpisode := ParseEpisode(m.Path); parsedEpisode > 0 {
season, episode = parsedSeason, parsedEpisode
if parsedSeason, parsedEpisode, parsedFraction := ParseEpisodeParts(m.Path); parsedEpisode > 0 {
season, episode, fraction = parsedSeason, parsedEpisode, parsedFraction
}
}
// 集号后缀:S01E11.5 记成 "11.5",没有小数的集后缀为空,
// 因此绝大多数条目的版本身份键与历史完全一致。
episodeTag := fmt.Sprintf("%d:%d%s", season, episode, episodeFractionSuffix(fraction))
if season > 0 || episode > 0 {
kind := specialKind
@@ -213,13 +220,13 @@ func mediaVersionGroupKey(m model.Media) string { // 远程 Emby 挂载条目保
}
switch {
case m.TMDbID > 0:
return fmt.Sprintf("episode:%s:tmdb:%d:%d:%d", kind, m.TMDbID, season, episode)
return fmt.Sprintf("episode:%s:tmdb:%d:%s", kind, m.TMDbID, episodeTag)
case m.BangumiID > 0:
return fmt.Sprintf("episode:%s:bangumi:%d:%d:%d", kind, m.BangumiID, season, episode)
return fmt.Sprintf("episode:%s:bangumi:%d:%s", kind, m.BangumiID, episodeTag)
case strings.TrimSpace(m.DoubanID) != "":
return fmt.Sprintf("episode:%s:douban:%s:%d:%d", kind, strings.ToLower(strings.TrimSpace(m.DoubanID)), season, episode)
return fmt.Sprintf("episode:%s:douban:%s:%s", kind, strings.ToLower(strings.TrimSpace(m.DoubanID)), episodeTag)
case strings.TrimSpace(m.TheTVDBID) != "":
return fmt.Sprintf("episode:%s:thetvdb:%s:%d:%d", kind, strings.ToLower(strings.TrimSpace(m.TheTVDBID)), season, episode)
return fmt.Sprintf("episode:%s:thetvdb:%s:%s", kind, strings.ToLower(strings.TrimSpace(m.TheTVDBID)), episodeTag)
}
title := firstNonEmpty(m.OriginalName, m.Title)
if title == "" {
@@ -234,7 +241,7 @@ func mediaVersionGroupKey(m model.Media) string { // 远程 Emby 挂载条目保
kind,
libKey,
title,
fmt.Sprintf("%d:%d", season, episode),
episodeTag,
}, "|")
}
+50
View File
@@ -737,3 +737,53 @@ func TestMediaAdultGroupCodeRequiresNSFW(t *testing.T) {
})
}
}
// S01E11 与 S01E11.5 必须折叠成两条不同记录(既不合并,也不丢集),
// 且 11.5 排在 11 之后、12 之前。这是「第一季分集加载不出来」那次的根因:
// 两条同季同集的记录被折成一条,计数与返回条目对不上。
func TestGroupEpisodeVersionsForDisplayKeepsHalfEpisodeSeparate(t *testing.T) {
rows := []model.Media{
{Base: model.Base{ID: "ep12"}, LibraryID: "anime", Title: "三月的狮子", SeasonNum: 1, EpisodeNum: 12, TMDbID: 65336, Path: "/anime/三月的狮子/3月的狮子 S01E12.mkv"},
{Base: model.Base{ID: "ep11"}, LibraryID: "anime", Title: "三月的狮子", SeasonNum: 1, EpisodeNum: 11, TMDbID: 65336, Path: "/anime/三月的狮子/3月的狮子 S01E11.mkv"},
{Base: model.Base{ID: "ep11half"}, LibraryID: "anime", Title: "三月的狮子", SeasonNum: 1, EpisodeNum: 11, EpisodeFraction: 0.5, TMDbID: 65336, Path: "/anime/三月的狮子/3月的狮子 S01E11.5.mkv"},
}
grouped := GroupEpisodeVersionsForDisplay(rows)
if len(grouped) != 3 {
t.Fatalf("grouped len = %d, want 3 (11 / 11.5 / 12): %#v", len(grouped), grouped)
}
wantOrder := []string{"ep11", "ep11half", "ep12"}
for i, want := range wantOrder {
if grouped[i].ID != want {
t.Fatalf("grouped[%d] = %q, want %q (order %#v)", i, grouped[i].ID, want, grouped)
}
}
for _, item := range grouped {
if len(item.Versions) != 0 {
t.Fatalf("half episode must not fold with the integral one: %#v", grouped)
}
}
}
// 版本身份键必须区分 11 与 11.5,同时保持没有小数时的历史键不变。
func TestMediaVersionGroupKeyDistinguishesHalfEpisode(t *testing.T) {
base := model.Media{LibraryID: "anime", Title: "三月的狮子", SeasonNum: 1, EpisodeNum: 11}
plain := base
half := base
half.EpisodeFraction = 0.5
plainKey := mediaVersionGroupKey(plain)
halfKey := mediaVersionGroupKey(half)
if plainKey == "" || halfKey == "" {
t.Fatalf("version keys must not be empty: %q %q", plainKey, halfKey)
}
if plainKey == halfKey {
t.Fatalf("11 and 11.5 must have different version keys, both = %q", plainKey)
}
if !strings.HasSuffix(halfKey, "1:11.5") {
t.Fatalf("half episode key = %q, want suffix 1:11.5", halfKey)
}
if !strings.HasSuffix(plainKey, "1:11") {
t.Fatalf("plain episode key = %q, want suffix 1:11", plainKey)
}
}
+3 -1
View File
@@ -142,7 +142,9 @@ func WriteMediaNFO(m *model.Media) (string, error) {
if m.SeasonNum > 0 || m.EpisodeNum > 0 {
title := strings.TrimSpace(m.EpisodeTitle)
if title == "" && m.EpisodeNum > 0 {
title = fmt.Sprintf("第 %d 集", m.EpisodeNum)
// 兜底标题也要带上小数:S01E11.5 导出成「第 11.5 集」,
// 否则回写 NFO 后重新入库会把半集写成普通的第 11 集。
title = fmt.Sprintf("第 %s 集", FormatEpisodeNumber(m.EpisodeNum, m.EpisodeFraction))
}
if title == "" {
title = strings.TrimSpace(m.Title)
+8 -5
View File
@@ -170,9 +170,12 @@ type existingLocalMedia struct {
TheTVDBID string
SeasonNum int
EpisodeNum int
Genres string
Countries string
Languages string
NSFW bool
ScrapeStatus string
// EpisodeFraction 是集号的小数部分(S01E11.5 → 0.5);变化同样要触发刷新,
// 否则已有行上的「半集」标记不会被写回。
EpisodeFraction float64
Genres string
Countries string
Languages string
NSFW bool
ScrapeStatus string
}
+2 -1
View File
@@ -33,7 +33,7 @@ func (s *ScannerService) existingLocalMediaSnapshotForRoot(ctx context.Context,
query = query.Where("(library_root_id = ? OR library_root_id = '' OR library_root_id IS NULL)", rootID)
}
rows, err := query.
Select("path", "library_root_id", "relative_path", "title", "original_name", "episode_title", "size_bytes", "duration_sec", "width", "height", "video_codec", "audio_codec", "container", "strm_url", "file_id", "poster_url", "backdrop_url", "overview", "year", "release_date", "rating", "tm_db_id", "bangumi_id", "douban_id", "thetvdb_id", "season_num", "episode_num", "genres", "countries", "languages", "nsfw", "scrape_status").
Select("path", "library_root_id", "relative_path", "title", "original_name", "episode_title", "size_bytes", "duration_sec", "width", "height", "video_codec", "audio_codec", "container", "strm_url", "file_id", "poster_url", "backdrop_url", "overview", "year", "release_date", "rating", "tm_db_id", "bangumi_id", "douban_id", "thetvdb_id", "season_num", "episode_num", "episode_fraction", "genres", "countries", "languages", "nsfw", "scrape_status").
Rows()
if err != nil {
return nil, err
@@ -74,6 +74,7 @@ func (s *ScannerService) existingLocalMediaSnapshotForRoot(ctx context.Context,
&row.TheTVDBID,
&row.SeasonNum,
&row.EpisodeNum,
&row.EpisodeFraction,
&row.Genres,
&row.Countries,
&row.Languages,
+33 -30
View File
@@ -23,18 +23,19 @@ func (s *ScannerService) ingestFile(ctx context.Context, lib *model.Library, roo
return
}
parsedSeason, parsedEpisode := ParseEpisode(path)
parsedSeason, parsedEpisode, parsedFraction := ParseEpisodeParts(path)
localMeta := s.readLocalScanMetadata(lib, root, path, parsedSeason, parsedEpisode)
media := s.buildLocalScanMedia(localScanMediaInput{
lib: lib,
root: root,
path: path,
ext: ext,
fileID: fileID,
size: size,
parsedSeason: parsedSeason,
parsedEpisode: parsedEpisode,
localMeta: localMeta,
lib: lib,
root: root,
path: path,
ext: ext,
fileID: fileID,
size: size,
parsedSeason: parsedSeason,
parsedEpisode: parsedEpisode,
parsedFraction: parsedFraction,
localMeta: localMeta,
})
isNewMedia, skipUnchanged := s.localMediaScanState(localMediaScanStateInput{
ctx: ctx,
@@ -169,15 +170,16 @@ func localMediaProbeDataMissing(existing existingLocalMedia) bool {
}
type localScanMediaInput struct {
lib *model.Library
root *model.LibraryRoot
path string
ext string
fileID string
size int64
parsedSeason int
parsedEpisode int
localMeta *LocalMetadata
lib *model.Library
root *model.LibraryRoot
path string
ext string
fileID string
size int64
parsedSeason int
parsedEpisode int
parsedFraction float64
localMeta *LocalMetadata
}
func (s *ScannerService) buildLocalScanMedia(in localScanMediaInput) *model.Media {
@@ -191,17 +193,18 @@ func (s *ScannerService) buildLocalScanMedia(in localScanMediaInput) *model.Medi
title, year = preferISOParentScrapeIdentity(in.path, in.lib.Path, title, year)
media := &model.Media{
LibraryID: in.lib.ID,
LibraryRootID: libraryRootID(in.root),
RelativePath: localRelativePath(in.path, in.root),
Title: title,
Year: year,
Path: in.path,
SizeBytes: in.size,
Container: strings.TrimPrefix(in.ext, "."),
FileID: in.fileID,
SeasonNum: in.parsedSeason,
EpisodeNum: in.parsedEpisode,
LibraryID: in.lib.ID,
LibraryRootID: libraryRootID(in.root),
RelativePath: localRelativePath(in.path, in.root),
Title: title,
Year: year,
Path: in.path,
SizeBytes: in.size,
Container: strings.TrimPrefix(in.ext, "."),
FileID: in.fileID,
SeasonNum: in.parsedSeason,
EpisodeNum: in.parsedEpisode,
EpisodeFraction: in.parsedFraction,
}
if in.ext == ".strm" {
media.Container = "strm"
+17 -10
View File
@@ -17,6 +17,9 @@ type scanDerivedMetadata struct {
TheTVDBID string
SeasonNum int
EpisodeNum int
// EpisodeFraction 是集号的小数部分(S01E11.5 → 0.5):它变化时同样要刷新,
// 否则已有行上的半集标记永远写不进去。
EpisodeFraction float64
}
func localMetadataNeedsRefresh(existing existingLocalMedia, local *LocalMetadata) bool {
@@ -97,16 +100,17 @@ func localDerivedMetadataNeedsRefresh(existing existingLocalMedia, incoming *mod
return true
}
return scanDerivedMetadataNeedsRefresh(scanDerivedMetadata{
Title: existing.Title,
ScrapeStatus: existing.ScrapeStatus,
Year: existing.Year,
ReleaseDate: existing.ReleaseDate,
TMDbID: existing.TMDbID,
BangumiID: existing.BangumiID,
DoubanID: existing.DoubanID,
TheTVDBID: existing.TheTVDBID,
SeasonNum: existing.SeasonNum,
EpisodeNum: existing.EpisodeNum,
Title: existing.Title,
ScrapeStatus: existing.ScrapeStatus,
Year: existing.Year,
ReleaseDate: existing.ReleaseDate,
TMDbID: existing.TMDbID,
BangumiID: existing.BangumiID,
DoubanID: existing.DoubanID,
TheTVDBID: existing.TheTVDBID,
SeasonNum: existing.SeasonNum,
EpisodeNum: existing.EpisodeNum,
EpisodeFraction: existing.EpisodeFraction,
}, incoming)
}
@@ -128,6 +132,9 @@ func scanDerivedMetadataNeedsRefresh(existing scanDerivedMetadata, incoming *mod
if incoming.EpisodeNum > 0 && existing.EpisodeNum != incoming.EpisodeNum {
return true
}
if (incoming.SeasonNum > 0 || incoming.EpisodeNum > 0) && existing.EpisodeFraction != incoming.EpisodeFraction {
return true
}
if incoming.TMDbID > 0 && existing.TMDbID != incoming.TMDbID {
return true
}
+13 -2
View File
@@ -8,6 +8,7 @@
// organize_source opt-in — organize the configured staging folder.
// transcode_cleanup every 24 h — purge HLS transcode artefacts
// older than 24 h.
// segment_prewarm every 6 h — fill IntroDB skip segments for queryable media.
//
// Each job runs at most once at a time (an in-flight run blocks the
// next tick). All work happens on a long-lived background context so
@@ -42,6 +43,8 @@ type SchedulerService struct {
imagesPolicyProvider func() ImageCachePolicy
segments *MediaSegmentService
mu sync.Mutex
stopCh chan struct{}
jobs []*scheduledJob
@@ -145,6 +148,14 @@ func (s *SchedulerService) Start(ctx context.Context) {
run: s.jobCleanImageCache,
},
}
// 片头预热只在注入了 Segments 时注册,避免测试跑无转外网任务。
if s.segments != nil {
s.jobs = append(s.jobs, &scheduledJob{
name: "segment_prewarm",
interval: segmentPrewarmJobInterval,
run: s.jobSegmentPrewarm,
})
}
// 到期提醒只在配置了巡检器时注册,避免测试与未启用通知的部署跑空转任务。
if s.expiryWatcher != nil {
s.jobs = append(s.jobs, &scheduledJob{
@@ -155,8 +166,8 @@ func (s *SchedulerService) Start(ctx context.Context) {
}
for _, j := range s.jobs {
initialDelay := 15 * time.Second
if j.name == "library_scan" || j.name == "organize_source" {
// 重启后不立即整库重扫/整理下载目录:更新窗口恰是登录高峰,
if j.name == "library_scan" || j.name == "organize_source" || j.name == "segment_prewarm" {
// 重启后不立即整库重扫/整理/预热:更新窗口恰是登录高峰,
// 15 秒即全量 walk + ffprobe 曾把 CPU/磁盘打满导致无法登录。
// 首轮等满一个完整周期再跑,平时节奏不变。
initialDelay = j.interval
@@ -0,0 +1,52 @@
package service
import (
"context"
"time"
"go.uber.org/zap"
)
const (
segmentPrewarmJobInterval = 6 * time.Hour
// SegmentPrewarmEnabledKey 控制是否后台预热 TheIntroDB 片段;默认关闭。
SegmentPrewarmEnabledKey = "segment.prewarm_enabled"
)
// SetSegments wires the IntroDB prewarm job. Without it the job is not registered.
func (s *SchedulerService) SetSegments(segments *MediaSegmentService) {
if s != nil {
s.segments = segments
}
}
func (s *SchedulerService) jobSegmentPrewarm(ctx context.Context) error {
if s == nil || s.segments == nil {
return nil
}
manual, _ := ctx.Value(schedulerManualRunKey{}).(bool)
if !manual && !s.segmentPrewarmEnabled(ctx) {
return nil
}
n, err := s.segments.Prewarm(ctx, segmentPrewarmDefaultLimit)
if s.log != nil {
s.log.Info("segment prewarm finished",
zap.Int("attempted", n),
zap.Error(err),
)
}
return err
}
// segmentPrewarmEnabled reports whether the operator opted into background
// IntroDB prewarm. Defaults to false so playback-on-demand remains the only path.
func (s *SchedulerService) segmentPrewarmEnabled(ctx context.Context) bool {
if s.repo == nil || s.repo.Setting == nil {
return false
}
v, err := s.repo.Setting.Get(ctx, SegmentPrewarmEnabledKey)
if err != nil {
return false
}
return parseBoolSetting(v, false)
}
@@ -0,0 +1,60 @@
package service
import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"go.uber.org/zap"
"github.com/truewhile/MeBox/internal/model"
"github.com/truewhile/MeBox/internal/repository"
)
func TestSegmentPrewarmDefaultsOff(t *testing.T) {
repos := repository.New(newServiceTestDB(t, &model.Setting{}))
scheduler := NewSchedulerService(zap.NewNop(), repos, nil, nil, nil, nil, "")
if scheduler.segmentPrewarmEnabled(t.Context()) {
t.Fatal("prewarm must default to off")
}
}
func TestJobSegmentPrewarmSkippedWhenDisabled(t *testing.T) {
var calls int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddInt32(&calls, 1)
w.WriteHeader(http.StatusNotFound)
}))
t.Cleanup(server.Close)
repos := repository.New(newServiceTestDB(t))
if err := repos.DB.Create(&model.Media{
Base: model.Base{ID: "mv-1"}, Path: "/a.mkv", TMDbID: 1,
}).Error; err != nil {
t.Fatal(err)
}
segments := NewMediaSegmentService(zap.NewNop(), repos).
SetIntroDB(NewIntroDBService(zap.NewNop()).SetBaseURL(server.URL)).
SetPrewarmGap(0)
scheduler := NewSchedulerService(zap.NewNop(), repos, nil, nil, nil, nil, "")
scheduler.SetSegments(segments)
if err := scheduler.jobSegmentPrewarm(t.Context()); err != nil {
t.Fatal(err)
}
if got := atomic.LoadInt32(&calls); got != 0 {
t.Fatalf("provider calls = %d, want 0 when prewarm is disabled", got)
}
if err := repos.Setting.Set(t.Context(), SegmentPrewarmEnabledKey, "true"); err != nil {
t.Fatal(err)
}
if err := scheduler.jobSegmentPrewarm(context.Background()); err != nil {
t.Fatal(err)
}
if got := atomic.LoadInt32(&calls); got != 1 {
t.Fatalf("provider calls = %d, want 1 after enabling prewarm", got)
}
}
+1
View File
@@ -36,6 +36,7 @@ type Container struct {
Scraper *ScraperService
Playback *PlaybackService
Segments *MediaSegmentService
MediaProbe *MediaProbeService
ImageProxy *ImageProxy
Watcher *WatcherService
Subtitle *SubtitleService
+6
View File
@@ -147,6 +147,11 @@ func (b *serviceContainerBuilder) initContentServices() {
b.c.Transcoder.SetStrmPlayTargetResolver(b.c.Strm.ResolvePlayTarget)
b.c.Transcoder.SetProbe(b.c.FFprobe)
b.c.Subtitle.SetStrmPlayTargetResolver(b.c.Strm.ResolvePlayTarget)
// 播放时的媒体信息提取(ffprobe 章节 → 跳过片头/片尾):完全异步,播放链路
// 只读缓存。换链复用与转码/字幕同一条路径,避免 CDN 防盗链 403。
b.c.MediaProbe = NewMediaProbeService(b.log, b.repos, b.c.FFprobe).
SetPlayTargetResolver(b.c.Strm.ResolvePlayTargetWithUA)
b.c.Segments.SetProbe(b.c.MediaProbe)
// 播放链路:/Videos/{id}/stream 与 /api/stream/{id} 在服务端完成换链后直接
// 302 到最终直链,客户端少跟随一次 302(高延迟线路上省一个往返)。
b.c.Stream.SetStrmPlayTargetResolver(b.c.Strm.ResolvePlayTargetWithUA)
@@ -187,6 +192,7 @@ func (b *serviceContainerBuilder) initAccessAndStorageServices() {
)
b.c.Scheduler.SetTaskTracker(b.c.Tasks)
b.c.Scheduler.SetOrganizePipeline(b.c.OrganizePipeline)
b.c.Scheduler.SetSegments(b.c.Segments)
b.c.Scheduler.SetImageCachePolicyProvider(func() ImageCachePolicy {
if b.cfg == nil {
return ImageCachePolicy{}
-2
View File
@@ -71,7 +71,6 @@ export interface DanmakuConfig {
font_size: string
area: string
volume: number
playback_rate: number
/** 当前用户是否已经看过 VR 全景播放的首次操作说明(按用户存储)。 */
vr360_guide_seen: boolean
/** Per-user preference: merge the same episode's multiple sources. */
@@ -90,7 +89,6 @@ export interface DanmakuSettingsPatch {
area?: number
merge_sources?: boolean
volume?: number
playback_rate?: number
/** 看过 VR 操作说明后置为 true,之后不再弹出。 */
vr360_guide_seen?: boolean
}
+2
View File
@@ -83,6 +83,8 @@ export interface MediaMetadataUpdate {
rating?: number
season_num?: number
episode_num?: number
/** 集号的小数部分(11.5 的 0.5);与 episode_num 一起构成显示集号。 */
episode_fraction?: number
tmdb_id?: number
bangumi_id?: number
douban_id?: string
+1 -1
View File
@@ -52,7 +52,7 @@ export function ExternalPlayerButton({
className={
compact
? 'shrink-0 inline-flex items-center rounded-lg border border-primary-400/35 bg-white px-2 py-1 text-xs font-semibold text-brand-500 hover:bg-primary-400/10 disabled:opacity-50 transition-colors whitespace-nowrap'
: 'btn-outline border-brand-500/30 px-5 text-[#c9954a] hover:border-brand-500 hover:bg-brand-50'
: 'btn-outline h-11 border-brand-500/30 px-5 text-[#c9954a] hover:border-brand-500 hover:bg-brand-50'
}
>
<PlaySquare size={compact ? 13 : 14} className="mr-1 inline" />
+7 -2
View File
@@ -4,6 +4,7 @@ import toast from 'react-hot-toast'
import { mediaAPI, type MediaMetadataUpdate } from '../api/library'
import type { Media } from '../types'
import { formatEpisodeNumber } from '../utils/episodeNumber'
interface MetadataEditDialogProps {
open: boolean
@@ -58,7 +59,7 @@ export function MetadataEditDialog({
release_date: media.release_date || '',
rating: media.rating > 0 ? String(media.rating) : '',
season_num: media.season_num > 0 || media.episode_num > 0 ? String(media.season_num || 0) : '',
episode_num: media.episode_num > 0 ? String(media.episode_num) : '',
episode_num: media.episode_num > 0 ? formatEpisodeNumber(media) : '',
tmdb_id: media.tmdb_id > 0 ? String(media.tmdb_id) : '',
bangumi_id: media.bangumi_id > 0 ? String(media.bangumi_id) : '',
douban_id: media.douban_id || '',
@@ -105,7 +106,11 @@ export function MetadataEditDialog({
if (!isSeries) {
payload.original_name = form.original_name
payload.season_num = Math.trunc(toNumber(form.season_num))
payload.episode_num = Math.trunc(toNumber(form.episode_num))
// 「集」输入框接受 11.5 这种半集写法:整数部分进 episode_num,
// 小数部分单独进 episode_fraction,避免保存后变成整集或残留小数。
const episodeValue = toNumber(form.episode_num)
payload.episode_num = Math.trunc(episodeValue)
payload.episode_fraction = Number((episodeValue - Math.trunc(episodeValue)).toFixed(3))
}
return payload
}
+4 -24
View File
@@ -133,10 +133,9 @@ type PlayerControlsProps = {
volume?: number
onVolumeChange?: (volume: number) => void
onVolumeCommit?: (volume: number) => void
/** 用户级播放倍速,由播放页从数据库读取。 */
/** 播放倍速(1x = 原速)。只作用于当前视频,不落库。 */
playbackRate?: number
onPlaybackRateChange?: (rate: number) => void
onPlaybackRateCommit?: (rate: number) => void
uiVisible: boolean
onUiVisibleChange: (visible: boolean) => void
/** 外部浮层(VR 工具条/设置面板)正被鼠标悬停:此时控制栏不自动隐藏。 */
@@ -213,7 +212,6 @@ export function PlayerControls({
onVolumeCommit,
playbackRate: playbackRateProp = 1,
onPlaybackRateChange,
onPlaybackRateCommit,
uiVisible,
onUiVisibleChange,
uiHold = false,
@@ -381,7 +379,7 @@ export function PlayerControls({
setMuted(next === 0)
}, [video, volumeProp])
// 倍速由播放页按用户持久化;配置加载、切换剧集或切换播放源后同步到当前 video。
// 倍速只影响当前视频:挂载、切换剧集或切换播放源后同步到当前 video。
useEffect(() => {
const el = video()
if (!el) return
@@ -626,15 +624,9 @@ export function PlayerControls({
}, PLAYBACK_RATE_HINT_MS)
}, [onPlaybackRateChange])
const commitPlaybackRate = useCallback(() => {
onPlaybackRateCommit?.(playbackRateRef.current)
}, [onPlaybackRateCommit])
const selectPlaybackRate = useCallback((next: number) => {
const normalized = normalizePlaybackRate(next)
changePlaybackRate(normalized)
onPlaybackRateCommit?.(normalized)
}, [changePlaybackRate, onPlaybackRateCommit])
changePlaybackRate(normalizePlaybackRate(next))
}, [changePlaybackRate])
const applyAbsoluteSeek = useCallback(
(absolute: number) => {
@@ -787,18 +779,6 @@ export function PlayerControls({
return () => window.removeEventListener('keydown', onKey, true)
}, [changePlaybackRate, queueRelativeSeek])
useEffect(() => {
const onKeyUp = (e: KeyboardEvent) => {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return
if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return
if (shouldIgnorePlayerSeekShortcut(e.target)) return
e.preventDefault()
commitPlaybackRate()
}
window.addEventListener('keyup', onKeyUp, true)
return () => window.removeEventListener('keyup', onKeyUp, true)
}, [commitPlaybackRate])
useEffect(() => {
return () => {
if (seekApplyTimerRef.current) clearTimeout(seekApplyTimerRef.current)
@@ -3,6 +3,7 @@ import type { ReactNode } from 'react'
import { ChevronDown, Layers, ListVideo } from 'lucide-react'
import type { Media } from '../types'
import { formatEpisodeLabel, formatEpisodeNumber } from '../utils/episodeNumber'
import { mediaVersionLabel, mediaVersionMatches } from '../utils/mediaVersion'
// PlayerMobileTheaterInfo — 竖屏剧场模式下视频下方的可滚动内容区。
@@ -38,15 +39,17 @@ type PlayerMobileTheaterInfoProps = {
}
function episodeShortLabel(ep: Media): string {
if (ep.episode_num > 0) return `${ep.episode_num}`
const number = formatEpisodeNumber(ep)
if (number) return number
const title = ep.episode_title?.trim() || ep.title?.trim() || ''
return title ? title.slice(0, 4) : '·'
}
function episodeFullLabel(ep: Media): string {
if (ep.episode_num > 0) {
const label = formatEpisodeLabel(ep)
if (label) {
const title = ep.episode_title?.trim()
return title && title !== ep.title?.trim() ? `第 ${ep.episode_num} 集 · ${title}` : `第 ${ep.episode_num} 集`
return title && title !== ep.title?.trim() ? `${label} · ${title}` : label
}
return ep.episode_title?.trim() || ep.title?.trim() || '未命名'
}
+7 -5
View File
@@ -2,6 +2,7 @@ import { useEffect, useMemo, useRef, useState } from 'react'
import { Check, Cloud, Film, Layers, Play, Search, X } from 'lucide-react'
import type { Media } from '../types'
import { formatEpisodeLabel, formatEpisodeNumber, episodeNumberValue } from '../utils/episodeNumber'
import { seasonLabel, seasonSortOrder, seriesTitleFromPath } from '../utils/groupSeries'
import {
isStrmMedia,
@@ -72,7 +73,7 @@ export function PlayerPlaylistPanel({
seasonsMap.get(s)!.push(ep)
}
for (const [, list] of seasonsMap) {
list.sort((a, b) => (a.episode_num || 0) - (b.episode_num || 0))
list.sort((a, b) => episodeNumberValue(a) - episodeNumberValue(b))
}
return Array.from(seasonsMap.entries())
.sort(([a], [b]) => seasonSortOrder(a) - seasonSortOrder(b))
@@ -118,7 +119,7 @@ export function PlayerPlaylistPanel({
? listToDisplay.filter((ep) => {
const query = filterText.trim().toLowerCase()
const title = (ep.episode_title || ep.title || '').toLowerCase()
const epNum = String(ep.episode_num)
const epNum = formatEpisodeNumber(ep)
return title.includes(query) || epNum === query || `e${epNum}`.includes(query) || `第${epNum}集`.includes(query)
})
: listToDisplay
@@ -293,7 +294,7 @@ export function PlayerPlaylistPanel({
const durationText =
ep.duration_sec > 0 ? `${Math.round(ep.duration_sec / 60)} 分钟` : ''
const cellTitle = [
ep.episode_num > 0 ? `第 ${ep.episode_num} 集` : '',
formatEpisodeLabel(ep),
displayTitle,
durationText,
versions.length > 0 ? `${versions.length} 个版本` : '',
@@ -316,7 +317,7 @@ export function PlayerPlaylistPanel({
{isPlaying ? (
<Play size={12} className="fill-current" />
) : ep.episode_num > 0 ? (
ep.episode_num
formatEpisodeNumber(ep)
) : (
<Film size={13} className="text-white/45" />
)}
@@ -429,7 +430,8 @@ function getEpisodeTitle(ep: Media, siblings: Media[]): string {
return mediaTitle
}
return ep.episode_num > 0 ? `第 ${ep.episode_num} 集` : mediaTitle || title || '未命名'
const episodeLabel = formatEpisodeLabel(ep)
return episodeLabel || mediaTitle || title || '未命名'
}
function looksLikeSeriesTitle(ep: Media, title: string, siblings: Media[]): boolean {
+2 -1
View File
@@ -28,6 +28,7 @@ import type { SeriesCard } from '../utils/groupSeries'
import { seriesCardLink } from '../utils/groupSeries'
import { isRemoteEmbyID } from '../utils/remoteEmby'
import { getLibraryArtworks } from './librariesPageModel'
import { formatEpisodeNumber } from '../utils/episodeNumber'
const TYPE_ICONS: Record<string, ReactNode> = {
movie: <Film size={18} />,
@@ -773,7 +774,7 @@ function ContinueCard({ media, progress }: { media: Media; progress: number }) {
{media.year > 0 && <span>{media.year}</span>}
{media.season_num !== undefined && media.episode_num !== undefined && (
<span>
S{media.season_num}E{media.episode_num}
S{media.season_num}E{formatEpisodeNumber(media)}
</span>
)}
</div>
+2 -1
View File
@@ -7,6 +7,7 @@ import { ExternalPlayerButton } from '../components/ExternalPlayerButton'
import { MediaFavouriteButton } from '../components/MediaFavouriteButton'
import type { Media } from '../types'
import { isTheatricalFeature, seriesTitle, type SeriesCard } from '../utils/groupSeries'
import { episodeNumberValue } from '../utils/episodeNumber'
type LibrarySeriesDetailHeaderProps = {
series: SeriesCard
@@ -152,7 +153,7 @@ function firstPlayableEpisode(episodes: Media[]): Media | null {
const sorted = [...episodes]
sorted.sort((a, b) =>
(a.season_num || 0) - (b.season_num || 0)
|| (a.episode_num || 0) - (b.episode_num || 0),
|| episodeNumberValue(a) - episodeNumberValue(b),
)
return sorted[0] ?? null
}
+10 -6
View File
@@ -11,6 +11,7 @@ import {
THEATRICAL_SEASON,
} from '../utils/groupSeries'
import { formatSize } from './libraryPageModel'
import { formatEpisodeLabel, formatEpisodeNumber } from '../utils/episodeNumber'
type SeasonGroup = {
season: number
@@ -95,14 +96,14 @@ export function LibrarySeriesEpisodes({
referrerPolicy="no-referrer"
/>
) : (
<span className="text-brand-600 font-bold text-sm">{ep.episode_num || '—'}</span>
<span className="text-brand-600 font-bold text-sm">{formatEpisodeNumber(ep) || '—'}</span>
)}
<div className="absolute inset-0 flex items-center justify-center bg-black/25 opacity-0 transition-opacity duration-200 group-hover:opacity-100">
<Play size={15} className="fill-white text-white drop-shadow-sm" />
</div>
{ep.episode_num > 0 && (
<span className="absolute bottom-0 right-0 rounded-tl bg-black/75 px-1 py-0.5 text-[9px] font-bold leading-none text-white backdrop-blur-[2px]">
{ep.episode_num}
{formatEpisodeNumber(ep)}
</span>
)}
</div>
@@ -153,15 +154,18 @@ function episodeDisplayTitle(ep: Media, siblings: Media[]): string {
}
}
if (ep.episode_num > 0) {
const episodeLabel = formatEpisodeLabel(ep)
if (episodeLabel) {
if (!mainTitle) {
return `第 ${ep.episode_num} 集`
return episodeLabel
}
const prefixRegex = new RegExp(`^(第\\s*0*${ep.episode_num}\\s*集|ep?\\.?\\s*0*${ep.episode_num}\\b)`, 'i')
// 标题里已经以「第 11 集 / 第 11.5 集 / ep 11」开头时直接用它,避免重复前缀。
const episodeNumber = formatEpisodeNumber(ep).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
const prefixRegex = new RegExp(`^(第\\s*0*${episodeNumber}\\s*集|ep?\\.?\\s*0*${episodeNumber}\\b)`, 'i')
if (prefixRegex.test(mainTitle)) {
return mainTitle
}
return `第 ${ep.episode_num} 集 · ${mainTitle}`
return `${episodeLabel} · ${mainTitle}`
}
return mainTitle || '未命名'
+30 -24
View File
@@ -1,4 +1,4 @@
import { Database, FileText, FolderInput, Pencil, Search, Sparkles, Trash2 } from 'lucide-react'
import { Database, FileText, FolderInput, Pencil, Search, ShieldAlert, Sparkles, Trash2 } from 'lucide-react'
import { EpisodeArtworkToggle } from '../components/EpisodeArtworkToggle'
import type { Media } from '../types'
@@ -34,50 +34,56 @@ export function MediaDetailAdminPanel({
}
return (
<div className="rounded-2xl border border-gray-200 bg-gray-50/50 p-5 space-y-3">
<p className="text-[10px] font-bold uppercase tracking-[0.2em] text-[#c9954a]">系统后台高级控制面板</p>
{isEpisodeArtworkTarget(media) && (
<EpisodeArtworkToggle
checked={scrapeEpisodeArtwork}
onChange={onScrapeEpisodeArtworkChange}
title="关闭后仍会获取每集简介、评分和时长,只跳过单集图片"
className="h-10"
/>
)}
<div className="flex flex-wrap gap-2">
<button onClick={onSmartScrape} className="btn-outline py-2 px-3.5 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<section className="overflow-hidden rounded-2xl border border-gray-200 bg-gray-50/50">
<div className="flex flex-wrap items-center justify-between gap-3 border-b border-gray-200/60 px-4 py-3 sm:px-5">
<p className="inline-flex items-center gap-2 text-2xs font-bold uppercase tracking-[0.18em] text-gray-500">
<ShieldAlert size={14} className="text-brand-500" />
系统后台高级控制面板
</p>
{isEpisodeArtworkTarget(media) && (
<EpisodeArtworkToggle
checked={scrapeEpisodeArtwork}
onChange={onScrapeEpisodeArtworkChange}
title="关闭后仍会获取每集简介、评分和时长,只跳过单集图片"
className="h-9 text-xs"
/>
)}
</div>
<div className="flex flex-wrap items-center gap-2 px-4 py-4 sm:px-5">
<button onClick={onSmartScrape} className="btn-outline h-9 rounded-lg px-3 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<Sparkles size={13} className="text-[#c9954a]" />
<span>智能刮削 (TMDB)</span>
</button>
<button onClick={onManualScrape} className="btn-outline py-2 px-3.5 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<button onClick={onManualScrape} className="btn-outline h-9 rounded-lg px-3 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<Search size={13} className="text-[#c9954a]" />
<span>手动匹配刮削</span>
</button>
<button onClick={onMetadataEdit} className="btn-outline py-2 px-3.5 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<Pencil size={13} className="text-gray-600" />
<button onClick={onMetadataEdit} className="btn-outline h-9 rounded-lg px-3 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<Pencil size={13} className="text-brand-500" />
<span>编辑元数据</span>
</button>
<button onClick={onOrganize} className="btn-outline py-2 px-3.5 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<button onClick={onOrganize} className="btn-outline h-9 rounded-lg px-3 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<FolderInput size={13} className="text-[#c9954a]" />
<span>整理入库</span>
</button>
<button onClick={onProbe} className="btn-outline py-2 px-3.5 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<Database size={13} className="text-gray-600" />
<button onClick={onProbe} className="btn-outline h-9 rounded-lg px-3 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<Database size={13} className="text-brand-500" />
<span>探测媒体轨 (ffprobe)</span>
</button>
<button onClick={onExportNFO} className="btn-outline py-2 px-3.5 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<FileText size={13} />
<button onClick={onExportNFO} className="btn-outline h-9 rounded-lg px-3 text-xs gap-1.5 border-gray-200 hover:border-brand-500/50 hover:bg-brand-50">
<FileText size={13} className="text-brand-500" />
<span>写出本地 NFO 属性</span>
</button>
<button
onClick={onDelete}
className="btn-outline py-2 px-3.5 text-xs gap-1.5 !border-red-100 !text-red-500 hover:!bg-red-50 hover:!border-red-200"
className="btn-outline ml-auto h-9 rounded-lg px-3 text-xs gap-1.5 !border-red-100 !text-red-500 hover:!bg-red-50 hover:!border-red-200"
>
<Trash2 size={13} />
<Trash2 size={13} className="text-red-500" />
<span>删除</span>
</button>
</div>
</div>
</section>
)
}
+47 -23
View File
@@ -9,6 +9,10 @@ type MediaDetailMetadataProps = {
export function MediaDetailMetadata({ media }: MediaDetailMetadataProps) {
const heading = media.episode_title?.trim() || media.title
const showTitleContext = Boolean(media.episode_title?.trim() && media.title && media.title !== heading)
const genres = parseCSV(media.genres)
const languages = parseCSV(media.languages)
const countries = parseCSV(media.countries)
const hasTaxonomy = genres.length > 0 || languages.length > 0 || countries.length > 0
return (
<>
@@ -21,26 +25,26 @@ export function MediaDetailMetadata({ media }: MediaDetailMetadataProps) {
{media.title}
</p>
)}
<div className="flex flex-wrap items-center gap-2.5 text-xs text-gray-500 font-bold tracking-wide uppercase">
<div className="flex flex-wrap items-center gap-2">
{media.year > 0 && (
<span className="inline-flex items-center gap-1 bg-gray-100 border border-gray-200/50 px-2.5 py-1 rounded-xl text-gray-700">
<span className="inline-flex h-7 items-center rounded-lg border border-gray-200/60 bg-gray-100 px-2.5 text-2xs font-bold uppercase tracking-wide gap-1.5 text-gray-700">
<Calendar size={13} className="text-brand-500" />
<span>{media.year} 年</span>
</span>
)}
{media.width > 0 && (
<span className="inline-flex items-center gap-1 bg-brand-50 text-brand-700 border border-brand-100/50 px-2.5 py-1 rounded-xl">
{media.width > 0 && media.height > 0 && (
<span className="inline-flex h-7 items-center rounded-lg border border-brand-100/50 bg-brand-50 px-2.5 text-2xs font-bold uppercase tracking-wide text-brand-700">
<span>{media.width} × {media.height}</span>
</span>
)}
<span className="bg-gray-100 border border-gray-200/50 px-2.5 py-1 rounded-xl text-gray-700">
{fmtSize(media.size_bytes)}
</span>
<span className="bg-gray-100 border border-gray-200/50 px-2.5 py-1 rounded-xl text-gray-700">
{fmtDuration(media.duration_sec)}
</span>
{media.size_bytes > 0 && (
<span className="inline-flex h-7 items-center rounded-lg border border-gray-200/60 bg-gray-100 px-2.5 text-2xs font-bold uppercase tracking-wide text-gray-700">{fmtSize(media.size_bytes)}</span>
)}
{media.duration_sec > 0 && (
<span className="inline-flex h-7 items-center rounded-lg border border-gray-200/60 bg-gray-100 px-2.5 text-2xs font-bold uppercase tracking-wide text-gray-700">{fmtDuration(media.duration_sec)}</span>
)}
{media.container && (
<span className="bg-gray-100 border border-gray-200/50 px-2.5 py-1 rounded-xl text-gray-700 font-mono">
<span className="inline-flex h-7 items-center rounded-lg border border-gray-200/60 bg-gray-100 px-2.5 text-2xs font-bold uppercase tracking-wide text-gray-700 font-mono">
{media.container}
</span>
)}
@@ -56,24 +60,44 @@ export function MediaDetailMetadata({ media }: MediaDetailMetadataProps) {
</div>
)}
<div className="space-y-4">
<MetadataTags label="类型流派" values={parseCSV(media.genres)} primary />
<MetadataTags label="语言" values={parseCSV(media.languages)} />
<MetadataTags label="国家/地区" values={parseCSV(media.countries)} />
</div>
{hasTaxonomy && (
<div className="space-y-3">
<MetadataRow label="类型流派" values={parseCSV(media.genres)} tone="brand" />
<MetadataRow label="语言" values={parseCSV(media.languages)} />
<MetadataRow label="国家/地区" values={parseCSV(media.countries)} />
</div>
)}
</>
)
}
function MetadataTags({ label, values, primary = false }: { label: string; values: string[]; primary?: boolean }) {
/**
* One "label - values" row of the taxonomy block.
*
* All rows share the same label gutter, so every value column starts on the
* same vertical line. Pills keep one geometry and differ by tone only.
*/
function MetadataRow({
label,
values,
tone = 'neutral',
}: {
label: string
values: string[]
tone?: 'neutral' | 'brand'
}) {
if (values.length === 0) return null
const tagClass = primary
? 'rounded-full bg-brand-50 text-brand-700 border border-brand-100/30 px-3 py-1 text-2xs font-bold uppercase tracking-wider'
: 'rounded-xl bg-gray-100 text-gray-600 border border-gray-200/40 px-2.5 py-1 text-2xs font-semibold'
const tagClass = [
'inline-flex items-center rounded-lg border px-2.5 py-1 text-2xs font-bold uppercase tracking-wide',
tone === 'brand'
? 'border-brand-100/50 bg-brand-50 text-brand-700'
: 'border-gray-200/60 bg-gray-100 text-gray-600',
].join(' ')
return (
<div className="flex flex-wrap items-center gap-3">
<span className="text-xs font-bold text-gray-500 w-16 uppercase tracking-wider">{label}</span>
<div className="flex flex-wrap gap-2">
<div className="grid grid-cols-[4.5rem_1fr] items-start gap-2 sm:grid-cols-[5.5rem_1fr]">
<span className="pt-1 text-xs font-semibold text-gray-500">{label}</span>
<div className="flex flex-wrap gap-1.5">
{values.map((value) => (
<span key={value} className={tagClass}>
{value}
+29 -25
View File
@@ -99,19 +99,23 @@ export function MediaDetailPlaybackActions({
}: MediaDetailPlaybackActionsProps) {
const targetId = media.id || playTargetId || ''
return (
<div className="flex flex-wrap gap-3">
<Link to={`/play/${targetId}`} state={{ from: `/media/${media.id}` }} className="btn-primary px-6 py-3.5 shadow-sm">
<div className="flex flex-wrap items-center gap-2.5">
<Link
to={`/play/${targetId}`}
state={{ from: `/media/${media.id}` }}
className="btn-primary h-11 px-6 shadow-sm"
>
<Play size={16} fill="currentColor" />
<span>立即播放</span>
</Link>
{!isDirectStreamMedia(media) && (
<Link
to={`/play/${targetId}?mode=hls`}
state={{ from: `/media/${media.id}` }}
className="btn-outline border-brand-500/30 hover:border-brand-500 text-[#c9954a] hover:bg-brand-50 px-5"
>
<RefreshCw size={14} className="animate-spin-slow" />
to={`/play/${targetId}?mode=hls`}
state={{ from: `/media/${media.id}` }}
className="btn-outline h-11 border-brand-500/30 px-5 text-[#c9954a] hover:border-brand-500 hover:bg-brand-50"
>
<RefreshCw size={14} />
<span>HLS 兼容转码播放</span>
</Link>
)}
@@ -121,7 +125,7 @@ export function MediaDetailPlaybackActions({
<button
onClick={onToggleFavourite}
className={
'btn-outline gap-2 ' +
'btn-outline h-11 gap-2 ' +
(favourite
? '!border-red-200 !bg-red-50 !text-red-600 hover:!bg-red-100/50'
: 'hover:border-red-200 hover:text-red-600 hover:bg-red-50/50')
@@ -154,10 +158,10 @@ export function MediaDetailMainContent({
<div className="relative z-10 p-6 sm:p-10 flex flex-col md:flex-row gap-8 lg:gap-12">
<MediaDetailPoster media={media} />
<div className="flex-1 space-y-6">
<div className="min-w-0 flex-1 space-y-5">
<MediaDetailMetadata media={media} />
<div className="divider border-gray-200/60" />
<div className="flex flex-col gap-5">
<div className="divider" />
<div className="space-y-4">
<MediaDetailPlaybackActions
media={media}
favourite={favourite}
@@ -165,21 +169,21 @@ export function MediaDetailMainContent({
playTargetId={playTargetId}
/>
<MediaVersionSwitcher media={media} />
{isAdmin && (
<MediaDetailAdminPanel
media={media}
scrapeEpisodeArtwork={scrapeEpisodeArtwork}
onScrapeEpisodeArtworkChange={onScrapeEpisodeArtworkChange}
onSmartScrape={onSmartScrape}
onManualScrape={onManualScrape}
onMetadataEdit={onMetadataEdit}
onOrganize={onOrganize}
onProbe={onProbe}
onExportNFO={onExportNFO}
onDelete={onDelete}
/>
)}
</div>
{isAdmin && (
<MediaDetailAdminPanel
media={media}
scrapeEpisodeArtwork={scrapeEpisodeArtwork}
onScrapeEpisodeArtworkChange={onScrapeEpisodeArtworkChange}
onSmartScrape={onSmartScrape}
onManualScrape={onManualScrape}
onMetadataEdit={onMetadataEdit}
onOrganize={onOrganize}
onProbe={onProbe}
onExportNFO={onExportNFO}
onDelete={onDelete}
/>
)}
</div>
</div>
)
+395 -94
View File
@@ -1,5 +1,5 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useLocation, useNavigate, useParams, useSearchParams } from 'react-router-dom'
import { useLocation, useNavigate, useParams } from 'react-router-dom'
import type Hls from 'hls.js'
import toast from 'react-hot-toast'
@@ -19,8 +19,9 @@ import { profileAPI } from '../api/profile'
import { useAuthStore } from '../stores/auth'
import type { Media, PlaybackInfo, PlaybackQuality, PlaybackSegment, PlaybackSegmentKind } from '../types'
import { getSeriesKey, seriesTitleFromPath } from '../utils/groupSeries'
import { formatEpisodeLabel } from '../utils/episodeNumber'
import { mediaVersionMatches, mediaVersionsOf } from '../utils/mediaVersion'
import { normalizePlaybackRate } from '../utils/playbackRate'
import { DEFAULT_PLAYBACK_RATE, normalizePlaybackRate } from '../utils/playbackRate'
import { resolveActiveSkip, skippedNoticeText, toSkipSegments, type SkipPrompt } from '../utils/skipSegments'
import { isRemoteEmbyID } from '../utils/remoteEmby'
import {
@@ -36,7 +37,8 @@ import {
type SubtitleStylePreset,
} from '../utils/subtitleDisplay'
import { pickPlayerMode, needsTranscodeForBrowser, isDirectStreamMedia, isStrmMedia, type PlayerMode } from './playerPageModel'
import { classifyDirectPlayError } from './directPlayError'
import { classifyDirectPlayError, DIRECT_SEEK_GRACE_MS } from './directPlayError'
import { canSeekDirectNow, DIRECT_SEEK_WAIT_MS } from './directPlaySeek'
import { apiErrorMessage } from './StrmManagePage'
import { PlayerTopBar } from './PlayerTopBar'
import { PlayerVideoStage } from './PlayerVideoStage'
@@ -58,6 +60,10 @@ import {
// ?mode=hls force HLS even when direct play would work
// ?mode=direct force direct play (default for browser-friendly codecs)
//
// ?mode= 只在进入某个媒体时读取一次(媒体详情页的「HLS 兼容转码播放」入口依赖它);
// 播放模式不再写回 URL——写 URL 会让 search 变化触发依赖 setSearchParams 的 effect
// 重跑,把刚切好的 HLS 打回直连(表现为「切了 HLS 还是直连播放」)。
//
// We pick a sensible default based on the source codec: H.264 + AAC in
// MP4 / WebM containers play directly; everything else (HEVC, MKV, AV1,
// AC3 audio, …) gets routed through ffmpeg → HLS. STRM / 云盘直链默认直连,
@@ -96,7 +102,6 @@ function newPlaybackProgressSession(mediaId: string): PlaybackProgressSession {
export function PlayerPage() {
const { id = '' } = useParams()
const [params, setParams] = useSearchParams()
const navigate = useNavigate()
const location = useLocation()
@@ -106,6 +111,17 @@ export function PlayerPage() {
const progressSessionRef = useRef<PlaybackProgressSession | null>(null)
const directRetryRef = useRef(false)
const retryingDirectRef = useRef(false)
// 程序化续播 seek 后的宽限截止时间;期内直连误报不升到 HLS。
const directSeekGraceUntilRef = useRef(0)
// STRM 直连跳转目标;seekable 未覆盖时会等到可跳再写 currentTime。
const pendingDirectSeekRef = useRef<number | null>(null)
const directSeekWaitCleanupRef = useRef<(() => void) | null>(null)
// 每个媒体加载时只读一次 URL 上的 ?mode=(媒体详情页「HLS 兼容转码播放」入口依赖它)。
// 播放模式本身不再写回 URL:写 URL 会让 search 变化触发依赖 setSearchParams 的 effect
// 重跑,把刚切好的 HLS 打回直连(表现为「切了 HLS 还是直连播放」)。
const requestedModeRef = useRef<PlayerMode | null>(null)
// URL 明确要求 HLS 时用它把「选线路」推迟到播放能力返回之后(云 HLS 优先,其次本地)。
const initialHlsRequestRef = useRef(false)
const fallbackTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const [media, setMedia] = useState<Media | null>(null)
@@ -139,16 +155,13 @@ export function PlayerPage() {
const [playerVolume, setPlayerVolume] = useState(() =>
normalizePlayerVolume(authUser?.player_volume),
)
const [playerPlaybackRate, setPlayerPlaybackRate] = useState(() =>
normalizePlaybackRate(authUser?.player_playback_rate),
)
// 播放倍速只作用于当前视频:不入库、不跨视频沿用(换集/换片会重置回 1x)。
const [playerPlaybackRate, setPlayerPlaybackRate] = useState(() => DEFAULT_PLAYBACK_RATE)
const persistedSubtitleChineseModeRef = useRef(subtitleChineseMode)
// VR 全景播放的首次操作说明是否已经看过:null = 服务端配置还没读回来,
// 这时不弹说明,避免给老用户闪一下。
const [vr360GuideSeen, setVr360GuideSeen] = useState<boolean | null>(null)
const playerVolumeTouchedRef = useRef(false)
const playerPlaybackRateTouchedRef = useRef(false)
const playerPlaybackRateSaveSeqRef = useRef(0)
const subtitlePreferenceTouchedRef = useRef(false)
const subtitlePreferenceSaveQueueRef = useRef<Promise<void>>(Promise.resolve())
const [hlsUnavailable, setHlsUnavailable] = useState(false)
@@ -285,10 +298,6 @@ export function PlayerPage() {
if (!playerVolumeTouchedRef.current) {
setPlayerVolume(volume)
}
const playbackRate = normalizePlaybackRate(cfg.playback_rate)
if (!playerPlaybackRateTouchedRef.current) {
setPlayerPlaybackRate(playbackRate)
}
setDanmakuEnabled(cfg.enabled)
setDanmakuOpacity(Number(cfg.opacity) || 1)
setDanmakuFontSize(Number(cfg.font_size) || 24)
@@ -300,10 +309,6 @@ export function PlayerPage() {
const current = useAuthStore.getState().user
if (current) setAuthUser({ ...current, player_volume: volume })
}
if (!playerPlaybackRateTouchedRef.current) {
const current = useAuthStore.getState().user
if (current) setAuthUser({ ...current, player_playback_rate: playbackRate })
}
})
.catch(() => {
// 配置读取失败时保持本地默认值,不影响播放。
@@ -338,34 +343,11 @@ export function PlayerPage() {
[setAuthUser],
)
// 倍速只改本机播放状态:不写回服务端,也不跨视频沿用。
const changePlayerPlaybackRate = useCallback((next: number) => {
playerPlaybackRateTouchedRef.current = true
setPlayerPlaybackRate(normalizePlaybackRate(next))
}, [])
const commitPlayerPlaybackRate = useCallback(
(next: number) => {
playerPlaybackRateTouchedRef.current = true
const playbackRate = normalizePlaybackRate(next)
setPlayerPlaybackRate(playbackRate)
const saveSeq = ++playerPlaybackRateSaveSeqRef.current
void danmakuAPI
.updateSettings({ playback_rate: playbackRate })
.then((cfg) => {
if (saveSeq !== playerPlaybackRateSaveSeqRef.current) return
const saved = normalizePlaybackRate(cfg.playback_rate)
setPlayerPlaybackRate(saved)
const current = useAuthStore.getState().user
if (current) setAuthUser({ ...current, player_playback_rate: saved })
})
.catch(() => {
if (saveSeq !== playerPlaybackRateSaveSeqRef.current) return
toast.error('倍速保存失败,请重试')
})
},
[setAuthUser],
)
const saveDanmakuAdvanced = useCallback(
async (values: { source: string; appId: string; appKey: string; clearAppKey: boolean }) => {
const patch: DanmakuSettingsPatch = {
@@ -519,6 +501,16 @@ export function PlayerPage() {
setDanmakuSearching(true)
directRetryRef.current = false
retryingDirectRef.current = false
directSeekGraceUntilRef.current = 0
pendingDirectSeekRef.current = null
directSeekWaitCleanupRef.current?.()
directSeekWaitCleanupRef.current = null
// 换片子时重新读一次 URL 上的强制模式;没有就按片源自动判定。
const urlMode = new URLSearchParams(window.location.search).get('mode')
requestedModeRef.current = urlMode === 'hls' || urlMode === 'direct' ? urlMode : null
// URL 明确要求 HLS 时,等播放能力拿到后还要选一次线路(云 HLS 优先)。
initialHlsRequestRef.current = urlMode === 'hls'
setMode('direct')
if (fallbackTimerRef.current) {
clearTimeout(fallbackTimerRef.current)
fallbackTimerRef.current = null
@@ -531,18 +523,11 @@ export function PlayerPage() {
}
}, [id])
// 依赖收敛为 mode 参数的字符串值:避免 params 对象引用每次变化都重复拉取元数据
const modeParam = params.get('mode') as PlayerMode | null
const setPlaybackMode = useCallback(
(next: PlayerMode) => {
setMode(next)
const nextParams = new URLSearchParams(window.location.search)
nextParams.set('mode', next)
setParams(nextParams, { replace: true })
},
[setParams],
)
const setPlaybackMode = useCallback((next: PlayerMode) => {
// 一旦显式决定过播放方式,URL 上的 ?mode= 就不再是权威(避免它把状态打回去)。
requestedModeRef.current = null
setMode(next)
}, [])
const clearFallbackTimer = useCallback(() => {
if (fallbackTimerRef.current) {
@@ -551,6 +536,185 @@ export function PlayerPage() {
}
}, [])
const clearDirectSeekWait = useCallback(() => {
directSeekWaitCleanupRef.current?.()
directSeekWaitCleanupRef.current = null
}, [])
/**
* STRM/115 直连跳转:等 seekable 覆盖目标再写 currentTime,避免被钳回 0;
* 同时打开误报宽限期,防止 seek 触发 video.load() 从头播。
*
* 超过 timeoutMs 仍未落点就直接放弃并回调 onStalled,绝不把播放器挂在
* 「正在定位」状态里(之前的表现就是「跳转等半天没反应」)。
*/
const seekStrmDirectTo = useCallback(
(
absoluteSec: number,
options?: {
onSettled?: () => void
onStalled?: (target: number) => void
timeoutMs?: number
},
) => {
const video = ref.current
if (!video) return false
const target = Math.max(0, absoluteSec)
const timeoutMs = options?.timeoutMs ?? DIRECT_SEEK_WAIT_MS
const wasPlaying = !video.paused
pendingDirectSeekRef.current = target
directSeekGraceUntilRef.current = Date.now() + DIRECT_SEEK_GRACE_MS
directRetryRef.current = false
clearFallbackTimer()
clearDirectSeekWait()
let intervalId: ReturnType<typeof setInterval> | null = null
let applied = false
let attempts = 0
let toastShown = false
const startedAt = Date.now()
const cleanup = () => {
video.removeEventListener('progress', tick)
video.removeEventListener('durationchange', tick)
video.removeEventListener('loadedmetadata', tick)
video.removeEventListener('seeked', onSeeked)
if (intervalId) {
clearInterval(intervalId)
intervalId = null
}
if (directSeekWaitCleanupRef.current === cleanup) {
directSeekWaitCleanupRef.current = null
}
}
const finish = (ok: boolean) => {
cleanup()
pendingDirectSeekRef.current = null
if (!ok) {
options?.onStalled?.(target)
return
}
// 定位完成后继续播:用户拖动时视频通常还在播,别让它停在暂停态。
if (wasPlaying) void video.play().catch(() => undefined)
options?.onSettled?.()
}
const settleIfMatched = () => {
const el = ref.current
if (!el || pendingDirectSeekRef.current === null) return false
if (Math.abs(el.currentTime - target) <= 2.5) {
finish(true)
return true
}
return false
}
const writeCurrentTime = (want: number) => {
const el = ref.current
if (!el) return false
selfSkipSeekRef.current = true
try {
el.currentTime = want
} catch {
return false
}
applied = true
attempts += 1
// 续期宽限:Range 拉取关键帧期间仍可能误报 error。
directSeekGraceUntilRef.current = Date.now() + DIRECT_SEEK_GRACE_MS
return true
}
const onSeeked = () => {
if (settleIfMatched()) return
const el = ref.current
if (!el || pendingDirectSeekRef.current === null) return
// 被钳回开头:允许 seekable 就绪后再写一次。
if (el.currentTime < 1.5 && target > 3) {
applied = false
directSeekGraceUntilRef.current = Date.now() + DIRECT_SEEK_GRACE_MS
}
}
const tick = () => {
const el = ref.current
if (!el || pendingDirectSeekRef.current === null) {
cleanup()
return
}
if (settleIfMatched()) return
const elapsed = Date.now() - startedAt
if (elapsed >= timeoutMs + 2500) {
// 给过一次强制写入的机会仍然没落点:交给 HLS,别继续等。
finish(false)
return
}
const canSeek = canSeekDirectNow(el.seekable, target)
const overCap = elapsed >= timeoutMs
if (!applied && (canSeek || overCap)) {
writeCurrentTime(target)
return
}
if (applied && attempts < 3 && canSeek && el.currentTime < 1.5 && target > 3) {
writeCurrentTime(target)
return
}
if (!toastShown && elapsed > 700 && !applied) {
toastShown = true
toast('正在定位播放位置…', { duration: 2200 })
}
}
video.addEventListener('progress', tick)
video.addEventListener('durationchange', tick)
video.addEventListener('loadedmetadata', tick)
video.addEventListener('seeked', onSeeked)
intervalId = setInterval(tick, 250)
directSeekWaitCleanupRef.current = cleanup
tick()
return true
},
[clearDirectSeekWait, clearFallbackTimer],
)
/**
* 直连定位确实做不到时(例如 115 直链上 Chrome 无法跳到目标关键帧),
* 用 HLS 接管并跳到同一个位置,而不是把用户晾在原地。
*/
const handleDirectSeekStalled = useCallback(
(target: number) => {
const cloudQuality =
findPlaybackQualityById(playbackInfo, selectedQuality) ??
findPlaybackQualityById(playbackInfo, playbackInfo?.default_quality ?? '')
if (cloudQuality?.source === 'cloud' && cloudQuality.available) {
setSelectedQuality(cloudQuality.id)
setHlsSource('cloud')
setCloudWaiting(false)
setCloudWaitMessage('')
pendingSeekRef.current = target > 2 ? target : null
setPlaybackMode('hls')
toast('直连定位失败,已切到 115 云 HLS 跳转')
return
}
const localQuality =
findPlaybackQualityById(playbackInfo, selectedQuality) ??
findPlaybackQualityById(playbackInfo, defaultLocalQualityId(playbackInfo))
if (localQuality?.available) {
setSelectedQuality(localQuality.id)
setHlsSource('local')
setCloudWaiting(false)
setCloudWaitMessage('')
setHlsStartSec(target)
setPlaybackMode('hls')
toast('直连定位失败,已切到本地 HLS 跳转')
return
}
toast.error('直连定位失败,且当前没有可用的 HLS 转码档位')
},
[playbackInfo, selectedQuality, setPlaybackMode],
)
// Load metadata and pick a default mode.
useEffect(() => {
if (!id) return
@@ -560,12 +724,23 @@ export function PlayerPage() {
.then((m) => {
if (cancelled) return
setMedia(m)
const isDirect = isDirectStreamMedia(m)
const auto = pickPlayerMode(m)
// 直连解码模式以及远程 Emby 挂载忽略 ?mode=hls。STRM 默认直连,但允许手动/失败后切 HLS。
setMode(directOnly || isDirect ? 'direct' : (modeParam ?? auto))
setPlayerError('')
setLoadError('')
const isDirect = isDirectStreamMedia(m)
const auto = pickPlayerMode(m)
// 直连解码 / 远程 Emby:强制直连。
if (directOnly || isDirect) {
setMode('direct')
return
}
// URL 上带 ?mode= 时以它为准(详情页「HLS 兼容转码播放」入口);
// 否则按片源自动判定,但不要用滞后的 get 回调把用户手动切的 HLS 盖回直连。
const requested = requestedModeRef.current
if (requested) {
setMode(requested)
return
}
setMode((prev) => (prev === 'hls' ? prev : auto))
})
.catch((err: unknown) => {
if (cancelled) return
@@ -575,7 +750,7 @@ export function PlayerPage() {
return () => {
cancelled = true
}
}, [id, modeParam, directOnly])
}, [id, directOnly])
// VR 全景素材识别:只在用户没手动改过开关时自动进入 VR 模式。
// 识别完全基于文件名/路径关键词与画幅比例,误判时点一下 VR 按钮即可退出。
@@ -674,6 +849,12 @@ export function PlayerPage() {
const mediaId = media?.id
const playbackProvider = playbackInfo?.provider
// 倍速只影响当前视频:换到另一集/另一部片时回到 1x,避免上一个视频的
// 倍速被顺手带到下一集。
useEffect(() => {
setPlayerPlaybackRate(DEFAULT_PLAYBACK_RATE)
}, [mediaId])
// 加载统一播放能力:115 云端清晰度 + 本地 HLS 清晰度。
useEffect(() => {
if (!mediaId) return
@@ -799,24 +980,29 @@ export function PlayerPage() {
// 切到 HLS 播放(优先 115 云端,其次本地转码)。toggleMode 与「VR 需要同源帧」
// 的自愈逻辑共用,避免两处各写一遍云端/本地选择。
const enterHlsPlayback = useCallback(
(startSec: number) => {
setHlsStartSec(startSec)
(startSec: number, infoOverride?: PlaybackInfo | null) => {
const position = Math.max(0, startSec)
// 允许传入刚拿到的能力信息:从 URL 强制 HLS 时不想等到 state 更新后才选线路。
const info = infoOverride ?? playbackInfo
setHlsStartSec(position)
const preferred =
playbackInfo?.provider === 'cloud115'
? findPlaybackQualityById(playbackInfo, selectedQuality) ??
findPlaybackQualityById(playbackInfo, playbackInfo.default_quality)
info?.provider === 'cloud115'
? findPlaybackQualityById(info, selectedQuality) ??
findPlaybackQualityById(info, info.default_quality)
: undefined
if (preferred?.source === 'cloud') {
setHlsSource('cloud')
if (preferred.available) {
setCloudWaiting(false)
// 云 HLS 是整段时间轴:切过去后按当前位置跳一下,别让用户从头看。
pendingSeekRef.current = position > 2 ? position : null
} else {
setCloudWaiting(true)
setCloudWaitMessage(preferred.note || `正在等待 115 转码 ${preferred.label}…`)
void startCloudTranscode(Number(preferred.id) || 4)
}
} else {
switchToLocalHLS(startSec)
switchToLocalHLS(position)
}
setPlaybackMode('hls')
},
@@ -868,6 +1054,15 @@ export function PlayerPage() {
}
}, [cloudWaiting, cloudWaitStartedAt, mediaId, selectedQuality, setPlaybackMode, switchToLocalHLS])
// 从 URL 强制 HLS(媒体详情页「HLS 兼容转码播放」)时,等播放能力返回后再选线路:
// 115 有可用的云转码档位就走云 HLS,否则退回本地转码。只做一次。
useEffect(() => {
if (!playbackInfo || !initialHlsRequestRef.current) return
if (directOnly || !mediaId) return
initialHlsRequestRef.current = false
enterHlsPlayback(0, playbackInfo)
}, [directOnly, enterHlsPlayback, mediaId, playbackInfo])
// 直连播放只发现外挂字幕;只有 HLS 模式需要探测可烧录的内嵌字幕。
useEffect(() => {
if (!mediaId) return
@@ -1111,32 +1306,92 @@ export function PlayerPage() {
}
const video = ref.current
if (!video) return
let onSeeked: (() => void) | undefined
const applyResume = () => {
if (resumePosition > 0 && Math.abs(video.currentTime - resumePosition) > 2) {
onSeeked = () => {
setInitialSeekDone(true)
const m = Math.floor(resumePosition / 60)
const s = Math.floor(resumePosition % 60)
const timeStr = `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`
toast.success(`已恢复上次播放进度至 ${timeStr}`, { duration: 2500 })
}
video.addEventListener('seeked', onSeeked, { once: true })
video.currentTime = resumePosition
let cancelled = false
let seekStarted = false
let onPlaying: (() => void) | undefined
let onCanPlay: (() => void) | undefined
const notifyResume = () => {
const m = Math.floor(resumePosition / 60)
const s = Math.floor(resumePosition % 60)
const timeStr = `${m.toString().padStart(2, '0')}:${s.toString().padStart(2, '0')}`
toast.success(`已恢复上次播放进度至 ${timeStr}`, { duration: 2500 })
}
const performResumeSeek = () => {
if (cancelled || seekStarted) return
if (Math.abs(video.currentTime - resumePosition) <= 2) {
seekStarted = true
setInitialSeekDone(true)
return
}
setInitialSeekDone(true)
seekStarted = true
if (isStrmMedia(mediaRef.current)) {
seekStrmDirectTo(resumePosition, {
timeoutMs: 6000,
onSettled: () => {
if (!cancelled) {
setInitialSeekDone(true)
notifyResume()
}
},
onStalled: () => {
// 定位不成就从头播,别把用户挂在「正在定位」上。
if (cancelled) return
setInitialSeekDone(true)
try {
video.currentTime = 0
} catch {
// ignore
}
void video.play().catch(() => undefined)
toast('续播定位超时,已从头播放', { duration: 2600 })
},
})
return
}
directSeekGraceUntilRef.current = Date.now() + DIRECT_SEEK_GRACE_MS
directRetryRef.current = false
clearFallbackTimer()
const onSeeked = () => {
setInitialSeekDone(true)
notifyResume()
}
video.addEventListener('seeked', onSeeked, { once: true })
try {
video.currentTime = resumePosition
} catch {
setInitialSeekDone(true)
}
}
if (video.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA) {
applyResume()
// STRM/云盘:等真正起播后再续播跳转,避免 302 未完成就 seek。
const deferForStrm = isStrmMedia(mediaRef.current)
if (deferForStrm) {
onPlaying = () => {
performResumeSeek()
}
if (!video.paused && video.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA) {
performResumeSeek()
} else {
video.addEventListener('playing', onPlaying, { once: true })
}
} else if (video.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA) {
performResumeSeek()
} else {
video.addEventListener('canplay', applyResume, { once: true })
onCanPlay = () => {
performResumeSeek()
}
video.addEventListener('canplay', onCanPlay, { once: true })
}
return () => {
video.removeEventListener('canplay', applyResume)
if (onSeeked) video.removeEventListener('seeked', onSeeked)
cancelled = true
if (onPlaying) video.removeEventListener('playing', onPlaying)
if (onCanPlay) video.removeEventListener('canplay', onCanPlay)
}
}, [resumePosition, initialSeekDone, mode, hlsStartSec])
}, [resumePosition, initialSeekDone, mode, hlsStartSec, clearFallbackTimer, seekStrmDirectTo])
// 使用 ref 实时同步进度计算所需的状态,避免每次 hlsStartSec 改变都触发 cleanup 并误上报旧进度
const hlsStartSecRef = useRef(hlsStartSec)
@@ -1462,7 +1717,8 @@ export function PlayerPage() {
const selectedQualityLabel = findPlaybackQualityById(playbackInfo, selectedQuality)?.label || ''
const qualityLabel =
mode === 'direct' ? originalQualityLabel : selectedQualityLabel || '清晰度'
// 播放方式只描述状态,切换入口在控制栏的「设置」面板里(见 onTogglePlaybackMode)。
// 播放方式标签:直接说清「这条片子现在是怎么在播」,避免用户猜线路
//(115 直链 / 115 云 HLS / 本地转码 / 客户端解码)。
const playbackModeLabel = isDirectStream
? isRemoteEmbyID(media?.id)
? 'Emby 直连播放'
@@ -1470,8 +1726,12 @@ export function PlayerPage() {
: directOnly
? '客户端直连解码'
: mode === 'hls'
? 'HLS 转码'
: '直接播放'
? hlsSource === 'cloud'
? '115 云 HLS 转码'
: '本地 HLS 转码'
: playbackInfo?.provider === 'cloud115'
? '直连播放 · 115 直链'
: '直接播放'
const canTogglePlaybackMode = !isDirectStream && !directOnly
// 顶栏标题下的次要信息:集数进度与版本数量,让用户一眼知道「在看什么、在哪」。
@@ -1524,7 +1784,8 @@ export function PlayerPage() {
}
const next = mode === 'hls' ? 'direct' : 'hls'
if (next === 'hls') {
enterHlsPlayback(0)
// 带着当前位置切过去:云 HLS 跳一下、本地 HLS 从该点重开转码,都不从头播。
enterHlsPlayback(ref.current?.currentTime || 0)
} else {
setCloudWaiting(false)
}
@@ -1616,10 +1877,30 @@ export function PlayerPage() {
video.currentTime = Math.max(0, target - hlsStartSec)
return
}
if (isStrmMedia(mediaRef.current)) {
seekStrmDirectTo(target, { onStalled: handleDirectSeekStalled })
return
}
selfSkipSeekRef.current = true
video.currentTime = target
},
[handleSeekAbsolute, hlsSource, hlsStartSec, mode],
[handleDirectSeekStalled, handleSeekAbsolute, hlsSource, hlsStartSec, mode, seekStrmDirectTo],
)
const handlePlayerSeekAbsolute = useCallback(
(absoluteSec: number) => {
if (mode === 'hls' && hlsSource === 'local') {
return handleSeekAbsolute(absoluteSec)
}
if (mode === 'direct' && isStrmMedia(mediaRef.current)) {
return seekStrmDirectTo(absoluteSec, {
timeoutMs: 4500,
onStalled: handleDirectSeekStalled,
})
}
return false
},
[handleDirectSeekStalled, handleSeekAbsolute, hlsSource, mode, seekStrmDirectTo],
)
const showSkipNotice = useCallback(
@@ -1790,6 +2071,7 @@ export function PlayerPage() {
elementSrc: video?.src ?? '',
expectedSrc,
alreadyRetried: directRetryRef.current,
seekGraceActive: Date.now() < directSeekGraceUntilRef.current,
})
if (action === 'ignore') return
@@ -1797,8 +2079,17 @@ export function PlayerPage() {
directRetryRef.current = true
retryingDirectRef.current = true
try {
video.load()
void video.play().catch(() => undefined)
// STRM/115 直连:load() 会清空进度从头播。软恢复即可。
if (isStrmMedia(mediaRef.current) || pendingDirectSeekRef.current !== null) {
const pending = pendingDirectSeekRef.current
void video.play().catch(() => undefined)
if (pending !== null && pending > 1) {
seekStrmDirectTo(pending)
}
} else {
video.load()
void video.play().catch(() => undefined)
}
} finally {
retryingDirectRef.current = false
}
@@ -1807,6 +2098,8 @@ export function PlayerPage() {
const fallbackDirectPlay = () => {
if (modeRef.current !== 'direct') return
// 宽限期内又触发的迟到 fallback 定时器:直接丢掉。
if (Date.now() < directSeekGraceUntilRef.current) return
const current = ref.current
if (current && current.readyState >= HTMLMediaElement.HAVE_CURRENT_DATA) return
if (isRemoteEmbyID(mediaRef.current?.id) || isDirectStreamMedia(mediaRef.current)) {
@@ -1841,7 +2134,9 @@ export function PlayerPage() {
}
clearFallbackTimer()
fallbackTimerRef.current = setTimeout(fallbackDirectPlay, 1500)
// STRM/115 直链冷启动比本地文件慢,给足缓冲时间再判定失败。
const fallbackDelayMs = isStrmMedia(mediaRef.current) ? 4000 : 1500
fallbackTimerRef.current = setTimeout(fallbackDirectPlay, fallbackDelayMs)
}, [
clearFallbackTimer,
directOnly,
@@ -1849,6 +2144,7 @@ export function PlayerPage() {
mediaId,
mode,
playbackInfo,
seekStrmDirectTo,
selectedQuality,
setPlaybackMode,
startCloudTranscode,
@@ -1883,6 +2179,7 @@ export function PlayerPage() {
directOnly={directOnly}
isDirectStream={isDirectStream}
directStreamLabel={isRemoteEmbyID(media?.id) ? 'Emby 直连播放' : undefined}
modeLabel={playbackModeLabel}
mode={mode}
onBack={goBack}
/>
@@ -1907,7 +2204,6 @@ export function PlayerPage() {
onPlayerVolumeCommit={commitPlayerVolume}
playerPlaybackRate={playerPlaybackRate}
onPlayerPlaybackRateChange={changePlayerPlaybackRate}
onPlayerPlaybackRateCommit={commitPlayerPlaybackRate}
danmakuEnabled={danmakuEnabled}
danmakuOpacity={danmakuOpacity}
danmakuFontSize={danmakuFontSize}
@@ -1932,7 +2228,11 @@ export function PlayerPage() {
onTogglePlaylist={isMobileTheater ? openPlaylistEntry : togglePlaylistOpen}
knownDuration={media?.duration_sec || 0}
streamOffset={mode === 'hls' && hlsSource === 'local' ? hlsStartSec : 0}
onSeekAbsolute={mode === 'hls' && hlsSource === 'local' ? handleSeekAbsolute : undefined}
onSeekAbsolute={
(mode === 'hls' && hlsSource === 'local') || (mode === 'direct' && isStrmMedia(media))
? handlePlayerSeekAbsolute
: undefined
}
qualities={qualityOptions}
qualityLabel={qualityLabel}
selectedQuality={selectedQuality}
@@ -2048,7 +2348,8 @@ function formatEpisodeDisplay(ep: Media, siblings: Media[]): string {
return mediaTitle
}
return ep.episode_num > 0 ? `第 ${ep.episode_num} 集` : mediaTitle || title || '未命名'
const episodeLabel = formatEpisodeLabel(ep)
return episodeLabel || mediaTitle || title || '未命名'
}
function findPlaybackQualityById(info: PlaybackInfo | null, id: string): PlaybackQuality | undefined {
+5 -1
View File
@@ -18,6 +18,8 @@ type PlayerTopBarProps = {
directOnly: boolean
isDirectStream?: boolean
directStreamLabel?: string
/** 已算好的播放方式文字(如「直连播放 · 115 直链」「115 云 HLS 转码」);传入时优先展示。 */
modeLabel?: string
mode: PlayerMode
onBack: () => void
}
@@ -28,10 +30,11 @@ export function PlayerTopBar({
directOnly,
isDirectStream,
directStreamLabel,
modeLabel,
mode,
onBack,
}: PlayerTopBarProps) {
const status = isDirectStream
const derived = isDirectStream
? {
icon: <Sparkles size={12} />,
label: directStreamLabel || '直连播放',
@@ -56,6 +59,7 @@ export function PlayerTopBar({
label: '直接播放',
title: '当前直接播放原始文件,可在控制栏「设置」里切到 HLS 转码',
}
const status = modeLabel ? { ...derived, label: modeLabel } : derived
return (
<div className="pointer-events-none absolute inset-x-0 top-0 z-20 flex items-start justify-between gap-3 bg-gradient-to-b from-black/60 via-black/20 to-transparent px-2.5 pb-10 pt-2.5 sm:px-3.5 sm:pt-3.5">
-3
View File
@@ -184,7 +184,6 @@ type PlayerVideoStageProps = {
onPlayerVolumeCommit: (volume: number) => void
playerPlaybackRate: number
onPlayerPlaybackRateChange: (rate: number) => void
onPlayerPlaybackRateCommit: (rate: number) => void
danmakuEnabled: boolean
danmakuOpacity: number
danmakuFontSize: number
@@ -268,7 +267,6 @@ export function PlayerVideoStage({
onPlayerVolumeCommit,
playerPlaybackRate,
onPlayerPlaybackRateChange,
onPlayerPlaybackRateCommit,
danmakuEnabled,
danmakuOpacity,
danmakuFontSize,
@@ -892,7 +890,6 @@ export function PlayerVideoStage({
onVolumeCommit={onPlayerVolumeCommit}
playbackRate={playerPlaybackRate}
onPlaybackRateChange={onPlayerPlaybackRateChange}
onPlaybackRateCommit={onPlayerPlaybackRateCommit}
uiVisible={controlsVisible && !locked}
onUiVisibleChange={setControlsVisible}
uiHold={vrUiHovered}
+9
View File
@@ -3,9 +3,15 @@ export type DirectPlayErrorAction = 'ignore' | 'retry' | 'fallback'
const MEDIA_ERR_ABORTED = 1
const HAVE_CURRENT_DATA = 2
/** 程序化续播/跳转后忽略直连误报的宽限期(毫秒)。 */
export const DIRECT_SEEK_GRACE_MS = 12_000
/**
* Chromium 常把被中断的直连(换 src、插入 track、302 未完成就 seek)
* 报成 error。真正不兼容应 fallback;瞬时中断应忽略或静默重试一次。
*
* seekGraceActive:程序化续播/跳转后的宽限期。115 STRM 在 302 直链上
* seek 时 Chromium 经常误报 decode/network error,此时绝不能升到 HLS。
*/
export function classifyDirectPlayError(input: {
errorCode: number | undefined | null
@@ -13,12 +19,15 @@ export function classifyDirectPlayError(input: {
elementSrc: string
expectedSrc: string
alreadyRetried: boolean
seekGraceActive?: boolean
}): DirectPlayErrorAction {
const code = input.errorCode ?? 0
if (code === MEDIA_ERR_ABORTED) return 'ignore'
if (input.readyState >= HAVE_CURRENT_DATA) return 'ignore'
if (!input.elementSrc) return 'ignore'
if (input.expectedSrc && input.elementSrc !== input.expectedSrc) return 'ignore'
// 续播 seek 宽限期内:误报很常见,且 retry 会 video.load() 把进度清零,直接忽略。
if (input.seekGraceActive) return 'ignore'
if (!input.alreadyRetried) return 'retry'
return 'fallback'
}
+52
View File
@@ -0,0 +1,52 @@
import {
canSeekDirectNow,
seekableEndSec,
timeInSeekableRanges,
} from './directPlaySeek.ts'
function check(name: string, cond: boolean) {
if (!cond) throw new Error(`directPlaySeek: ${name}`)
}
function ranges(parts: Array<[number, number]>) {
return {
length: parts.length,
start: (i: number) => parts[i][0],
end: (i: number) => parts[i][1],
}
}
check('empty seekable rejects mid seek', !timeInSeekableRanges(null, 120))
check('empty seekable end is 0', seekableEndSec(undefined) === 0)
check(
'covered range accepts target',
timeInSeekableRanges(ranges([[0, 600]]), 120),
)
check(
'target beyond end is rejected',
!timeInSeekableRanges(ranges([[0, 30]]), 120),
)
check(
'multi-range uses any segment',
timeInSeekableRanges(
ranges([
[0, 10],
[100, 200],
]),
150,
),
)
check('seekableEndSec takes max end', seekableEndSec(ranges([[0, 10], [5, 80]])) === 80)
check('canSeekDirectNow allows near-zero without seekable', canSeekDirectNow(null, 0.2))
check('canSeekDirectNow blocks mid when not seekable', !canSeekDirectNow(null, 90))
check(
'canSeekDirectNow allows mid when covered',
canSeekDirectNow(ranges([[0, 900]]), 90),
)
console.log('directPlaySeek.test.ts ok')
+60
View File
@@ -0,0 +1,60 @@
/** STRM/115 直连跳转:等 seekable 覆盖目标后再写 currentTime,避免被钳回 0。 */
export const DIRECT_SEEK_WAIT_MS = 10_000
export const DIRECT_SEEK_MARGIN_SEC = 0.35
type SeekableLike = {
length: number
start: (index: number) => number
end: (index: number) => number
}
/** 目标时间是否落在任一 seekable 区间内(末端留一点边距)。 */
export function timeInSeekableRanges(
seekable: SeekableLike | null | undefined,
time: number,
margin = DIRECT_SEEK_MARGIN_SEC,
): boolean {
if (!seekable || seekable.length <= 0) return false
if (!Number.isFinite(time) || time < 0) return false
for (let i = 0; i < seekable.length; i += 1) {
try {
const start = seekable.start(i)
const end = seekable.end(i)
if (!Number.isFinite(start) || !Number.isFinite(end)) continue
if (time + margin >= start && time <= end + margin) return true
} catch {
// TimeRanges 在部分浏览器上可能抛 InvalidStateError,跳过该段。
}
}
return false
}
/** seekable 已覆盖的最右端(秒);没有可用区间时返回 0。 */
export function seekableEndSec(seekable: SeekableLike | null | undefined): number {
if (!seekable || seekable.length <= 0) return 0
let max = 0
for (let i = 0; i < seekable.length; i += 1) {
try {
const end = seekable.end(i)
if (Number.isFinite(end) && end > max) max = end
} catch {
// ignore
}
}
return max
}
/**
* 直连是否可以立刻跳到 target。
* - seekable 已覆盖 → 可以
* - 目标接近 0 → 可以(不需要等)
* - 否则应等待 progress / durationchange
*/
export function canSeekDirectNow(
seekable: SeekableLike | null | undefined,
time: number,
): boolean {
if (!Number.isFinite(time) || time <= 1) return true
return timeInSeekableRanges(seekable, time)
}
+24
View File
@@ -72,4 +72,28 @@ check(
}) === 'fallback',
)
check(
'seek grace ignores decode miss without retrying',
classifyDirectPlayError({
errorCode: 4,
readyState: 0,
elementSrc: src,
expectedSrc: src,
alreadyRetried: false,
seekGraceActive: true,
}) === 'ignore',
)
check(
'seek grace never falls back to HLS',
classifyDirectPlayError({
errorCode: 4,
readyState: 0,
elementSrc: src,
expectedSrc: src,
alreadyRetried: true,
seekGraceActive: true,
}) === 'ignore',
)
console.log('playerPageModel.test.ts ok')
+7
View File
@@ -23,6 +23,13 @@ export const generalSettingsGroup: SettingGroup = {
hint: '默认关闭。开启后宿主机不再进行任何 FFmpeg 转码,所有播放交给第三方客户端(Infuse / VLC / Emby 客户端等)或浏览器本地解码直连(direct play / 302 直链),大幅降低宿主机 CPU 占用。若客户端不支持源编码可能无法播放。',
defaultValue: 'false',
},
{
key: 'segment.prewarm_enabled',
label: '后台预热 IntroDB 片头片段',
type: 'toggle',
hint: '默认关闭。开启后每 6 小时在后台向 TheIntroDB 拉取可识别媒体的片头/片尾区间并写入本地;关闭时仅在播放时按需请求。',
defaultValue: 'false',
},
{
key: 'transcode.enabled',
label: '启用转码',
+2 -1
View File
@@ -10,6 +10,7 @@ import {
type SeriesCard,
} from '../utils/groupSeries'
import { resolveSeriesCardByKey } from '../utils/seriesCardResolve'
import { episodeNumberValue } from '../utils/episodeNumber'
type SeasonEpisodes = {
season: number
@@ -72,7 +73,7 @@ export function useLibrarySeriesSelection({
seasons.get(s)!.push(ep)
}
for (const [, list] of seasons) {
list.sort((a, b) => (a.episode_num || 0) - (b.episode_num || 0))
list.sort((a, b) => episodeNumberValue(a) - episodeNumberValue(b))
}
return Array.from(seasons.entries())
.sort(([a], [b]) => seasonSortOrder(a) - seasonSortOrder(b))
+2 -1
View File
@@ -9,6 +9,7 @@ import { confirmActionResult } from '../components/confirmAction'
import { useEpisodeArtworkPreference } from '../hooks/useEpisodeArtworkPreference'
import type { Media } from '../types'
import { seasonSortOrder } from '../utils/groupSeries'
import { episodeNumberValue } from '../utils/episodeNumber'
import { mediaLibraryBackTarget } from './MediaDetailPageModel'
type MediaDetailLocationState = { from?: string } | null
@@ -81,7 +82,7 @@ export function useMediaDetailPageState({ id, navigate }: MediaDetailPageStatePa
seasons.get(s)!.push(ep)
}
for (const [, list] of seasons) {
list.sort((a, b) => (a.episode_num || 0) - (b.episode_num || 0))
list.sort((a, b) => episodeNumberValue(a) - episodeNumberValue(b))
}
return Array.from(seasons.entries())
.sort(([a], [b]) => seasonSortOrder(a) - seasonSortOrder(b))
-2
View File
@@ -12,8 +12,6 @@ export interface User {
subtitle_chinese_mode?: 'original' | 'simplified' | 'traditional'
/** 本地缓存的播放器音量,服务端配置接口会刷新。 */
player_volume?: number
/** 本地缓存的播放倍速,服务端配置接口会刷新。 */
player_playback_rate?: number
force_password_reset: boolean
is_active: boolean
allowed_library_ids?: string[]
+2
View File
@@ -28,6 +28,8 @@ export interface Media {
release_date?: string
season_num: number
episode_num: number
/** 集号的小数部分(S01E11.5 → 0.5);没有小数时缺省。 */
episode_fraction?: number
scrape_status: string
tmdb_id: number
bangumi_id: number
+36
View File
@@ -0,0 +1,36 @@
import type { Media } from '../types'
type EpisodeNumberSource = Pick<Media, 'episode_num' | 'episode_fraction'> | null | undefined
/**
* 含小数的集号:S01E11.5 → 11.5。没有小数(绝大多数集)时就是 episode_num。
*
* 后端把整数集号和它的小数部分分开存(Emby 协议的 IndexNumber 只能是整数),
* 网页端所有「显示集号 / 按集排序」的地方都应该用这个函数,而不是直接读
* episode_num,否则 11 与 11.5 会显示、排序成同一集。
*/
export function episodeNumberValue(media: EpisodeNumberSource): number {
const base = media?.episode_num ?? 0
const fraction = media?.episode_fraction ?? 0
if (!(fraction > 0 && fraction < 1)) return base
return base + fraction
}
/** 显示用集号文本:11 → "11",11.5 → "11.5";没有集号时返回空串。 */
export function formatEpisodeNumber(media: EpisodeNumberSource): string {
const value = episodeNumberValue(media)
return value > 0 ? String(value) : ''
}
/** "第 11 集" / "第 11.5 集";没有集号时返回空串。 */
export function formatEpisodeLabel(media: EpisodeNumberSource): string {
const value = formatEpisodeNumber(media)
return value ? `第 ${value} 集` : ''
}
/** 按 (季, 集, 小数部分) 升序比较,供剧集列表排序复用。 */
export function compareEpisodeOrder(a: Media, b: Media): number {
const seasonDiff = (a.season_num ?? 0) - (b.season_num ?? 0)
if (seasonDiff !== 0) return seasonDiff
return episodeNumberValue(a) - episodeNumberValue(b)
}
+3
View File
@@ -3,6 +3,9 @@ export const PLAYBACK_RATE_OPTIONS = [0.5, 0.75, 1, 1.25, 1.5, 1.75, 2, 2.5, 3]
export const MIN_PLAYBACK_RATE = PLAYBACK_RATE_OPTIONS[0]
export const MAX_PLAYBACK_RATE = PLAYBACK_RATE_OPTIONS[PLAYBACK_RATE_OPTIONS.length - 1]
/** 默认倍速(1x)。倍速不落库,每个视频都从这个值开始。 */
export const DEFAULT_PLAYBACK_RATE = 1
export function normalizePlaybackRate(value: unknown): number {
const parsed = Number(value)
if (!Number.isFinite(parsed)) return 1