Commit 0f2cbb7a25
Unregistered key
Layout: unified · split
internal/player.go +63 −5
| @@ -285,6 +285,7 @@ func StartVideo(link string, args []string, title string, anime *Anime) (string, | ||
| 285 | 285 | if userConfig.MpvArgs != nil { |
| 286 | 286 | args = append(args, userConfig.MpvArgs...) |
| 287 | 287 | } |
| 288 | callerHasSubtitleArg := hasMPVSubtitleArg(args) | |
| 288 | 289 | |
| 289 | 290 | shouldSetDefaultReferrer := isHTTPStreamLink(link) && !hasMPVReferrerArg(args) |
| 290 | 291 | referrer := strings.TrimSpace(anime.Ep.StreamReferrer) |
| @@ -294,7 +295,8 @@ func StartVideo(link string, args []string, title string, anime *Anime) (string, | ||
| 294 | 295 | if referrer != "" && shouldSetDefaultReferrer { |
| 295 | 296 | args = append(args, fmt.Sprintf("--referrer=%s", referrer)) |
| 296 | 297 | } |
| 297 | if subtitleURL := strings.TrimSpace(anime.Ep.SubtitleURL); subtitleURL != "" && !hasMPVSubtitleArg(args) { | |
| 298 | subtitleURL := strings.TrimSpace(anime.Ep.SubtitleURL) | |
| 299 | if subtitleURL != "" && !callerHasSubtitleArg { | |
| 298 | 300 | args = append(args, fmt.Sprintf("--sub-file=%s", subtitleURL)) |
| 299 | 301 | } |
| 300 | 302 | args = normalizeReferrerArgs(args) |
| @@ -324,10 +326,10 @@ func StartVideo(link string, args []string, title string, anime *Anime) (string, | ||
| 324 | 326 | return "", fmt.Errorf("failed to load file in existing MPV instance: %w", err) |
| 325 | 327 | } |
| 326 | 328 | |
| 327 | // Wait a brief moment for the file to load | |
| 328 | time.Sleep(100 * time.Millisecond) | |
| 329 | ||
| 330 | if subtitleURL := strings.TrimSpace(anime.Ep.SubtitleURL); subtitleURL != "" && !hasMPVSubtitleArg(args) { | |
| 329 | if subtitleURL != "" && !callerHasSubtitleArg { | |
| 330 | if readyErr := waitForMPVFileReady(mpvSocketPath, link, 12*time.Second); readyErr != nil { | |
| 331 | Log(fmt.Sprintf("Timed out waiting to attach subtitle track: %v", readyErr)) | |
| 332 | } | |
| 331 | 333 | _, subErr := MPVSendCommand(mpvSocketPath, []interface{}{"sub-add", subtitleURL, "select"}) |
| 332 | 334 | if subErr != nil { |
| 333 | 335 | Log(fmt.Sprintf("Failed to load subtitle track: %v", subErr)) |
| @@ -572,6 +574,62 @@ func SeekMPV(ipcSocketPath string, time int) (interface{}, error) { | ||
| 572 | 574 | return MPVSendCommand(ipcSocketPath, command) |
| 573 | 575 | } |
| 574 | 576 | |
| 577 | type mpvCommandSender func(string, []interface{}) (interface{}, error) | |
| 578 | ||
| 579 | func waitForMPVFileReady(ipcSocketPath, expectedPath string, timeout time.Duration) error { | |
| 580 | return waitForMPVFileReadyWith(MPVSendCommand, ipcSocketPath, expectedPath, timeout, 100*time.Millisecond) | |
| 581 | } | |
| 582 | ||
| 583 | func waitForMPVFileReadyWith(send mpvCommandSender, ipcSocketPath, expectedPath string, timeout, pollInterval time.Duration) error { | |
| 584 | if timeout <= 0 { | |
| 585 | timeout = 10 * time.Second | |
| 586 | } | |
| 587 | if pollInterval <= 0 { | |
| 588 | pollInterval = 100 * time.Millisecond | |
| 589 | } | |
| 590 | deadline := time.Now().Add(timeout) | |
| 591 | var lastErr error | |
| 592 | for time.Now().Before(deadline) { | |
| 593 | pathValue, pathErr := send(ipcSocketPath, []interface{}{"get_property", "path"}) | |
| 594 | path, _ := pathValue.(string) | |
| 595 | pathReady := pathErr == nil && strings.TrimSpace(path) != "" | |
| 596 | if pathReady && expectedPath != "" { | |
| 597 | pathReady = path == expectedPath | |
| 598 | } | |
| 599 | if pathReady { | |
| 600 | if duration, durationErr := send(ipcSocketPath, []interface{}{"get_property", "duration"}); durationErr == nil && mpvNumber(duration) > 0 { | |
| 601 | return nil | |
| 602 | } | |
| 603 | if position, positionErr := send(ipcSocketPath, []interface{}{"get_property", "time-pos"}); positionErr == nil && mpvNumber(position) >= 0 { | |
| 604 | return nil | |
| 605 | } | |
| 606 | } | |
| 607 | if pathErr != nil { | |
| 608 | lastErr = pathErr | |
| 609 | } | |
| 610 | time.Sleep(pollInterval) | |
| 611 | } | |
| 612 | if lastErr != nil { | |
| 613 | return fmt.Errorf("MPV file did not become ready: %w", lastErr) | |
| 614 | } | |
| 615 | return fmt.Errorf("MPV file did not become ready before timeout") | |
| 616 | } | |
| 617 | ||
| 618 | func mpvNumber(value interface{}) float64 { | |
| 619 | switch number := value.(type) { | |
| 620 | case float64: | |
| 621 | return number | |
| 622 | case float32: | |
| 623 | return float64(number) | |
| 624 | case int: | |
| 625 | return float64(number) | |
| 626 | case int64: | |
| 627 | return float64(number) | |
| 628 | default: | |
| 629 | return -1 | |
| 630 | } | |
| 631 | } | |
| 632 | ||
| 575 | 633 | func GetMPVPausedStatus(ipcSocketPath string) (bool, error) { |
| 576 | 634 | status, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "pause"}) |
| 577 | 635 | if err != nil || status == nil { |
internal/player_subtitle_test.go added +31
| @@ -0,0 +1,31 @@ | ||
| 1 | package internal | |
| 2 | ||
| 3 | import ( | |
| 4 | "errors" | |
| 5 | "testing" | |
| 6 | "time" | |
| 7 | ) | |
| 8 | ||
| 9 | func TestWaitForMPVFileReadyRetriesUntilPropertiesExist(t *testing.T) { | |
| 10 | pathCalls := 0 | |
| 11 | sender := func(_ string, command []interface{}) (interface{}, error) { | |
| 12 | switch command[1] { | |
| 13 | case "path": | |
| 14 | pathCalls++ | |
| 15 | if pathCalls < 3 { | |
| 16 | return nil, errors.New("property unavailable") | |
| 17 | } | |
| 18 | return "https://cdn.example/episode.m3u8", nil | |
| 19 | case "duration": | |
| 20 | return float64(1440), nil | |
| 21 | default: | |
| 22 | return nil, errors.New("unexpected command") | |
| 23 | } | |
| 24 | } | |
| 25 | if err := waitForMPVFileReadyWith(sender, "socket", "https://cdn.example/episode.m3u8", 100*time.Millisecond, time.Millisecond); err != nil { | |
| 26 | t.Fatal(err) | |
| 27 | } | |
| 28 | if pathCalls != 3 { | |
| 29 | t.Fatalf("path queried %d times, want 3", pathCalls) | |
| 30 | } | |
| 31 | } | |
internal/providers/senshi/provider_test.go +4 −6
| @@ -145,7 +145,6 @@ func TestGetEpisodeStreamsForModeSelectsHardSub(t *testing.T) { | ||
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | func TestGetEpisodeStreamsForModeLoadsSoftSubtitles(t *testing.T) { |
| 148 | const subtitleURL = "https://cdn.example/sub_2_eng.vtt" | |
| 149 | 148 | const manifestPath = "/sub_filemoon.json" |
| 150 | 149 | |
| 151 | 150 | var server *httptest.Server |
| @@ -162,7 +161,7 @@ func TestGetEpisodeStreamsForModeLoadsSoftSubtitles(t *testing.T) { | ||
| 162 | 161 | }}) |
| 163 | 162 | case r.URL.Path == manifestPath: |
| 164 | 163 | _ = json.NewEncoder(w).Encode([]senshiSubtitleTrack{{ |
| 165 | Src: subtitleURL, | |
| 164 | Src: server.URL + "/sub_2_eng.vtt", | |
| 166 | 165 | Label: "ENG", |
| 167 | 166 | Default: true, |
| 168 | 167 | }}) |
| @@ -184,13 +183,12 @@ func TestGetEpisodeStreamsForModeLoadsSoftSubtitles(t *testing.T) { | ||
| 184 | 183 | if len(links) != 1 || links[0] != "https://cdn.example/soft.m3u8" { |
| 185 | 184 | t.Fatalf("unexpected links %#v", links) |
| 186 | 185 | } |
| 187 | if hints[links[0]].Subtitle != subtitleURL { | |
| 186 | if hints[links[0]].Subtitle != server.URL+"/sub_2_eng.vtt" { | |
| 188 | 187 | t.Fatalf("unexpected subtitle %q", hints[links[0]].Subtitle) |
| 189 | 188 | } |
| 190 | 189 | } |
| 191 | 190 | |
| 192 | 191 | func TestGetEpisodeStreamsForModeHardStillInjectsSenshiSubs(t *testing.T) { |
| 193 | const subtitleURL = "https://cdn.example/sub_2_eng.vtt" | |
| 194 | 192 | const manifestPath = "/sub_filemoon.json" |
| 195 | 193 | |
| 196 | 194 | var server *httptest.Server |
| @@ -207,7 +205,7 @@ func TestGetEpisodeStreamsForModeHardStillInjectsSenshiSubs(t *testing.T) { | ||
| 207 | 205 | }}) |
| 208 | 206 | case r.URL.Path == manifestPath: |
| 209 | 207 | _ = json.NewEncoder(w).Encode([]senshiSubtitleTrack{{ |
| 210 | Src: subtitleURL, | |
| 208 | Src: server.URL + "/sub_2_eng.vtt", | |
| 211 | 209 | Label: "ENG", |
| 212 | 210 | Default: true, |
| 213 | 211 | }}) |
| @@ -229,7 +227,7 @@ func TestGetEpisodeStreamsForModeHardStillInjectsSenshiSubs(t *testing.T) { | ||
| 229 | 227 | if len(links) != 1 { |
| 230 | 228 | t.Fatalf("unexpected links %#v", links) |
| 231 | 229 | } |
| 232 | if hints[links[0]].Subtitle != subtitleURL { | |
| 230 | if hints[links[0]].Subtitle != server.URL+"/sub_2_eng.vtt" { | |
| 233 | 231 | t.Fatalf("expected senshi external subs even with hard preference, got %q", hints[links[0]].Subtitle) |
| 234 | 232 | } |
| 235 | 233 | } |
internal/providers/senshi/subtitles.go +210 −8
| @@ -1,11 +1,24 @@ | ||
| 1 | 1 | package senshi |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | "crypto/sha256" | |
| 5 | "fmt" | |
| 6 | "io" | |
| 4 | 7 | "net/http" |
| 5 | 8 | "net/url" |
| 9 | "os" | |
| 10 | "path/filepath" | |
| 11 | "regexp" | |
| 6 | 12 | "strings" |
| 13 | "time" | |
| 14 | ||
| 15 | "github.com/wraient/curd/internal/curdhost" | |
| 7 | 16 | ) |
| 8 | 17 | |
| 18 | const maxSenshiSubtitleSize = 8 << 20 | |
| 19 | ||
| 20 | var senshiVTTTimingLine = regexp.MustCompile(`^\s*(?:(?:\d{2}:)?\d{2}:\d{2}\.\d{3})\s+-->\s+(?:(?:\d{2}:)?\d{2}:\d{2}\.\d{3})(?:\s|$)`) | |
| 21 | ||
| 9 | 22 | type senshiSubtitleTrack struct { |
| 10 | 23 | Src string `json:"src"` |
| 11 | 24 | Label string `json:"label"` |
| @@ -50,21 +63,47 @@ func fetchSenshiSubtitle(manifestURL string) (string, error) { | ||
| 50 | 63 | } |
| 51 | 64 | |
| 52 | 65 | func pickSenshiSubtitleTrack(tracks []senshiSubtitleTrack) string { |
| 53 | var fallback string | |
| 66 | // Senshi can put a forced/signs-only English track before the full dialogue | |
| 67 | // track. Prefer a non-forced default before considering label fallbacks. | |
| 54 | 68 | for _, track := range tracks { |
| 55 | 69 | file := strings.TrimSpace(track.Src) |
| 56 | if file == "" { | |
| 57 | continue | |
| 70 | if file != "" && track.Default && !isSenshiForcedSubtitleLabel(track.Label) { | |
| 71 | return file | |
| 72 | } | |
| 73 | } | |
| 74 | for _, track := range tracks { | |
| 75 | file := strings.TrimSpace(track.Src) | |
| 76 | label := strings.ToLower(strings.TrimSpace(track.Label)) | |
| 77 | if file != "" && strings.Contains(label, "eng") && !isSenshiForcedSubtitleLabel(label) { | |
| 78 | return file | |
| 79 | } | |
| 80 | } | |
| 81 | for _, track := range tracks { | |
| 82 | file := strings.TrimSpace(track.Src) | |
| 83 | if file != "" && track.Default { | |
| 84 | return file | |
| 58 | 85 | } |
| 86 | } | |
| 87 | for _, track := range tracks { | |
| 88 | file := strings.TrimSpace(track.Src) | |
| 59 | 89 | label := strings.ToLower(strings.TrimSpace(track.Label)) |
| 60 | if track.Default || strings.Contains(label, "eng") { | |
| 90 | if file != "" && strings.Contains(label, "eng") { | |
| 61 | 91 | return file |
| 62 | 92 | } |
| 63 | if fallback == "" { | |
| 64 | fallback = file | |
| 93 | } | |
| 94 | for _, track := range tracks { | |
| 95 | if file := strings.TrimSpace(track.Src); file != "" { | |
| 96 | return file | |
| 65 | 97 | } |
| 66 | 98 | } |
| 67 | return fallback | |
| 99 | return "" | |
| 100 | } | |
| 101 | ||
| 102 | func isSenshiForcedSubtitleLabel(label string) bool { | |
| 103 | label = strings.ToLower(strings.TrimSpace(label)) | |
| 104 | return strings.Contains(label, "forced") || | |
| 105 | strings.Contains(label, "sign") || | |
| 106 | strings.Contains(label, "song") | |
| 68 | 107 | } |
| 69 | 108 | |
| 70 | 109 | func resolveSenshiSubtitle(item embedItem) string { |
| @@ -76,7 +115,170 @@ func resolveSenshiSubtitle(item embedItem) string { | ||
| 76 | 115 | if err != nil { |
| 77 | 116 | return "" |
| 78 | 117 | } |
| 79 | return subtitle | |
| 118 | return prepareSenshiSubtitle(subtitle) | |
| 119 | } | |
| 120 | ||
| 121 | func prepareSenshiSubtitle(subtitleURL string) string { | |
| 122 | subtitleURL = strings.TrimSpace(subtitleURL) | |
| 123 | if subtitleURL == "" { | |
| 124 | return "" | |
| 125 | } | |
| 126 | if styled := validatedSenshiASSURL(subtitleURL); styled != "" { | |
| 127 | return styled | |
| 128 | } | |
| 129 | if local, err := cacheSanitizedSenshiVTT(subtitleURL, senshiSubtitleCacheDir()); err == nil { | |
| 130 | return local | |
| 131 | } | |
| 132 | return subtitleURL | |
| 133 | } | |
| 134 | ||
| 135 | func validatedSenshiASSURL(subtitleURL string) string { | |
| 136 | parsed, err := url.Parse(strings.TrimSpace(subtitleURL)) | |
| 137 | if err != nil || parsed.Scheme == "" || parsed.Host == "" || !strings.EqualFold(filepath.Ext(parsed.Path), ".vtt") { | |
| 138 | return "" | |
| 139 | } | |
| 140 | parsed.Path = strings.TrimSuffix(parsed.Path, filepath.Ext(parsed.Path)) + ".ass" | |
| 141 | candidate := parsed.String() | |
| 142 | ||
| 143 | req, err := newRequest(http.MethodGet, candidate) | |
| 144 | if err != nil { | |
| 145 | return "" | |
| 146 | } | |
| 147 | req.Header.Set("Range", "bytes=0-8191") | |
| 148 | resp, err := curdhost.HTTPClient().Do(req) | |
| 149 | if err != nil { | |
| 150 | return "" | |
| 151 | } | |
| 152 | defer resp.Body.Close() | |
| 153 | if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { | |
| 154 | return "" | |
| 155 | } | |
| 156 | prefix, err := io.ReadAll(io.LimitReader(resp.Body, 8192)) | |
| 157 | if err != nil { | |
| 158 | return "" | |
| 159 | } | |
| 160 | text := strings.TrimSpace(strings.TrimPrefix(string(prefix), "\ufeff")) | |
| 161 | if !strings.HasPrefix(text, "[Script Info]") || !strings.Contains(text, "[V4+ Styles]") { | |
| 162 | return "" | |
| 163 | } | |
| 164 | return candidate | |
| 165 | } | |
| 166 | ||
| 167 | func senshiSubtitleCacheDir() string { | |
| 168 | return filepath.Join(os.TempDir(), "curd", "subtitles") | |
| 169 | } | |
| 170 | ||
| 171 | func cacheSanitizedSenshiVTT(subtitleURL, cacheDir string) (string, error) { | |
| 172 | parsed, err := url.Parse(strings.TrimSpace(subtitleURL)) | |
| 173 | if err != nil || parsed.Scheme == "" || parsed.Host == "" || !strings.EqualFold(filepath.Ext(parsed.Path), ".vtt") { | |
| 174 | return "", fmt.Errorf("not a remote WebVTT subtitle") | |
| 175 | } | |
| 176 | if err := os.MkdirAll(cacheDir, 0o700); err != nil { | |
| 177 | return "", err | |
| 178 | } | |
| 179 | cleanupOldSenshiSubtitles(cacheDir) | |
| 180 | ||
| 181 | cacheKey := fmt.Sprintf("%x", sha256.Sum256([]byte(subtitleURL))) | |
| 182 | cachePath := filepath.Join(cacheDir, cacheKey+".vtt") | |
| 183 | if info, statErr := os.Stat(cachePath); statErr == nil && info.Size() > 0 && time.Since(info.ModTime()) < 12*time.Hour { | |
| 184 | return cachePath, nil | |
| 185 | } | |
| 186 | ||
| 187 | req, err := newRequest(http.MethodGet, subtitleURL) | |
| 188 | if err != nil { | |
| 189 | return "", err | |
| 190 | } | |
| 191 | resp, err := curdhost.HTTPClient().Do(req) | |
| 192 | if err != nil { | |
| 193 | return "", err | |
| 194 | } | |
| 195 | defer resp.Body.Close() | |
| 196 | if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices { | |
| 197 | return "", fmt.Errorf("subtitle request failed with status %d", resp.StatusCode) | |
| 198 | } | |
| 199 | raw, err := io.ReadAll(io.LimitReader(resp.Body, maxSenshiSubtitleSize+1)) | |
| 200 | if err != nil { | |
| 201 | return "", err | |
| 202 | } | |
| 203 | if len(raw) > maxSenshiSubtitleSize { | |
| 204 | return "", fmt.Errorf("subtitle is too large") | |
| 205 | } | |
| 206 | sanitized, changed := sanitizeSenshiWebVTT(raw) | |
| 207 | if !changed { | |
| 208 | return subtitleURL, nil | |
| 209 | } | |
| 210 | tempPath := cachePath + ".tmp" | |
| 211 | if err := os.WriteFile(tempPath, sanitized, 0o600); err != nil { | |
| 212 | return "", err | |
| 213 | } | |
| 214 | if err := os.Rename(tempPath, cachePath); err != nil { | |
| 215 | _ = os.Remove(tempPath) | |
| 216 | return "", err | |
| 217 | } | |
| 218 | return cachePath, nil | |
| 219 | } | |
| 220 | ||
| 221 | func sanitizeSenshiWebVTT(raw []byte) ([]byte, bool) { | |
| 222 | text := strings.TrimPrefix(strings.ReplaceAll(string(raw), "\r\n", "\n"), "\ufeff") | |
| 223 | lines := strings.Split(text, "\n") | |
| 224 | timingIndexes := make([]int, 0) | |
| 225 | for index, line := range lines { | |
| 226 | if senshiVTTTimingLine.MatchString(strings.TrimSpace(line)) { | |
| 227 | timingIndexes = append(timingIndexes, index) | |
| 228 | } | |
| 229 | } | |
| 230 | if len(timingIndexes) == 0 { | |
| 231 | return raw, false | |
| 232 | } | |
| 233 | ||
| 234 | changed := strings.Contains(text, "\\h") | |
| 235 | out := []string{"WEBVTT", ""} | |
| 236 | for cueIndex, timingIndex := range timingIndexes { | |
| 237 | end := len(lines) | |
| 238 | if cueIndex+1 < len(timingIndexes) { | |
| 239 | end = timingIndexes[cueIndex+1] | |
| 240 | } | |
| 241 | content := append([]string(nil), lines[timingIndex+1:end]...) | |
| 242 | for len(content) > 0 && strings.TrimSpace(content[len(content)-1]) == "" { | |
| 243 | content = content[:len(content)-1] | |
| 244 | } | |
| 245 | for len(content) > 0 && strings.TrimSpace(content[0]) == "" { | |
| 246 | content = content[1:] | |
| 247 | } | |
| 248 | if len(content) == 0 { | |
| 249 | continue | |
| 250 | } | |
| 251 | out = append(out, strings.TrimSpace(lines[timingIndex])) | |
| 252 | for _, line := range content { | |
| 253 | line = strings.ReplaceAll(strings.TrimSuffix(line, "\r"), "\\h", "\u00a0") | |
| 254 | if strings.TrimSpace(line) == "" { | |
| 255 | line = "\u00a0" | |
| 256 | changed = true | |
| 257 | } | |
| 258 | out = append(out, line) | |
| 259 | } | |
| 260 | out = append(out, "") | |
| 261 | } | |
| 262 | if !changed { | |
| 263 | return raw, false | |
| 264 | } | |
| 265 | return []byte(strings.Join(out, "\n")), true | |
| 266 | } | |
| 267 | ||
| 268 | func cleanupOldSenshiSubtitles(cacheDir string) { | |
| 269 | entries, err := os.ReadDir(cacheDir) | |
| 270 | if err != nil { | |
| 271 | return | |
| 272 | } | |
| 273 | for _, entry := range entries { | |
| 274 | if entry.IsDir() || !strings.HasSuffix(strings.ToLower(entry.Name()), ".vtt") { | |
| 275 | continue | |
| 276 | } | |
| 277 | info, err := entry.Info() | |
| 278 | if err == nil && time.Since(info.ModTime()) > 48*time.Hour { | |
| 279 | _ = os.Remove(filepath.Join(cacheDir, entry.Name())) | |
| 280 | } | |
| 281 | } | |
| 80 | 282 | } |
| 81 | 283 | |
| 82 | 284 | func isSubEmbedStatus(status string) bool { |
internal/providers/senshi/subtitles_test.go +67 −1
| @@ -1,6 +1,11 @@ | ||
| 1 | 1 | package senshi |
| 2 | 2 | |
| 3 | import "testing" | |
| 3 | import ( | |
| 4 | "net/http" | |
| 5 | "net/http/httptest" | |
| 6 | "strings" | |
| 7 | "testing" | |
| 8 | ) | |
| 4 | 9 | |
| 5 | 10 | func TestSubtitleInfoFromURL(t *testing.T) { |
| 6 | 11 | rawURL := "https://embed.example/e/abc/?sub.info=https%3A%2F%2Fninstream.com%2Fmanifest.json" |
| @@ -19,6 +24,27 @@ func TestPickSenshiSubtitleTrackPrefersEnglishDefault(t *testing.T) { | ||
| 19 | 24 | } |
| 20 | 25 | } |
| 21 | 26 | |
| 27 | func TestPickSenshiSubtitleTrackSkipsForcedTrackBeforeDefaultDialogue(t *testing.T) { | |
| 28 | tracks := []senshiSubtitleTrack{ | |
| 29 | {Src: "https://cdn.example/sub_3_eng.vtt", Label: "Forced (ENG)"}, | |
| 30 | {Src: "https://cdn.example/sub_4_eng.vtt", Label: "ENG", Default: true}, | |
| 31 | {Src: "https://cdn.example/sub_5_eng.vtt", Label: "SDH (ENG)"}, | |
| 32 | } | |
| 33 | if got := pickSenshiSubtitleTrack(tracks); got != "https://cdn.example/sub_4_eng.vtt" { | |
| 34 | t.Fatalf("picked %q instead of the full dialogue track", got) | |
| 35 | } | |
| 36 | } | |
| 37 | ||
| 38 | func TestPickSenshiSubtitleTrackAvoidsForcedEnglishFallback(t *testing.T) { | |
| 39 | tracks := []senshiSubtitleTrack{ | |
| 40 | {Src: "https://cdn.example/forced.vtt", Label: "Signs & Songs (ENG)"}, | |
| 41 | {Src: "https://cdn.example/dialogue.vtt", Label: "English"}, | |
| 42 | } | |
| 43 | if got := pickSenshiSubtitleTrack(tracks); got != "https://cdn.example/dialogue.vtt" { | |
| 44 | t.Fatalf("picked %q instead of the full dialogue track", got) | |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 22 | 48 | func TestSenshiSubtitleManifestURLUsesServerFM(t *testing.T) { |
| 23 | 49 | serverFM := "https://embed.example/e/abc/?sub.info=https://ninstream.com/manifest.json" |
| 24 | 50 | item := embedItem{ServerFM: &serverFM} |
| @@ -33,3 +59,43 @@ func TestSenshiSubtitleManifestURLFallsBackToMaskedBase(t *testing.T) { | ||
| 33 | 59 | t.Fatalf("unexpected manifest url %q", got) |
| 34 | 60 | } |
| 35 | 61 | } |
| 62 | ||
| 63 | func TestValidatedSenshiASSURLPrefersOriginalStyledTrack(t *testing.T) { | |
| 64 | server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| 65 | if r.URL.Path != "/sub_2_eng.ass" { | |
| 66 | http.NotFound(w, r) | |
| 67 | return | |
| 68 | } | |
| 69 | _, _ = w.Write([]byte("[Script Info]\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name\n")) | |
| 70 | })) | |
| 71 | defer server.Close() | |
| 72 | withSenshiTestClient(t, server.Client()) | |
| 73 | ||
| 74 | got := validatedSenshiASSURL(server.URL + "/sub_2_eng.vtt") | |
| 75 | if got != server.URL+"/sub_2_eng.ass" { | |
| 76 | t.Fatalf("validatedSenshiASSURL() = %q", got) | |
| 77 | } | |
| 78 | } | |
| 79 | ||
| 80 | func TestSanitizeSenshiWebVTTKeepsMultilineSignInsideCue(t *testing.T) { | |
| 81 | raw := []byte("WEBVTT\n\n05:14.750 --> 05:20.800\n<b>Attack\n\n\nDefense\n \nMagic\n\n\nSpeed</b>\n\n05:21.530 --> 05:22.800\nElymas.\n") | |
| 82 | sanitized, changed := sanitizeSenshiWebVTT(raw) | |
| 83 | if !changed { | |
| 84 | t.Fatal("expected malformed VTT to be changed") | |
| 85 | } | |
| 86 | text := string(sanitized) | |
| 87 | if strings.Contains(text, "\n\nDefense") { | |
| 88 | t.Fatalf("blank line still terminates the cue:\n%s", text) | |
| 89 | } | |
| 90 | if !strings.Contains(text, "Defense") || !strings.Contains(text, "05:21.530 --> 05:22.800\nElymas.") { | |
| 91 | t.Fatalf("sanitized cues were lost:\n%s", text) | |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | func TestSanitizeSenshiWebVTTReplacesASSEscapedSpaces(t *testing.T) { | |
| 96 | raw := []byte("WEBVTT\n\n04:59.000 --> 05:01.000\nEpisode 2\\h\\hTitle\n") | |
| 97 | sanitized, changed := sanitizeSenshiWebVTT(raw) | |
| 98 | if !changed || strings.Contains(string(sanitized), "\\h") { | |
| 99 | t.Fatalf("ASS spacing was not normalized: %q", sanitized) | |
| 100 | } | |
| 101 | } | |