Commit 539f1b8a97

539f1b8a9757c8d58bb7a16cc22c73e71241ec3c

parent: 820bdf8040

Unsigned

Wraient <wraient.911@gmail.com> · 2025-01-26 14:11 UTC

Feat: Start new episode in same window after playback ends

Layout: unified · split

cmd/curd/main.go +17 −2
@@ -185,7 +185,7 @@ func main() {
185 185
186 anime.Ep.Player.Speed = 1.0 186 anime.Ep.Player.Speed = 1.0
187 187
188 // Main loop 188 // Main loop (loop to keep starting new episodes)
189 for { 189 for {
190 190
191 internal.Log(anime, logFile) 191 internal.Log(anime, logFile)
@@ -211,7 +211,7 @@ func main() {
211 } 211 }
212 } 212 }
213 213
214 // Start curd 214 // Start curd (loop while episode is playing)
215 for { 215 for {
216 // Check if current episode is filler/recap 216 // Check if current episode is filler/recap
217 err = internal.GetEpisodeData(anime.MalId, anime.Ep.Number, &anime) 217 err = internal.GetEpisodeData(anime.MalId, anime.Ep.Number, &anime)
@@ -428,6 +428,21 @@ func main() {
428 // anime.AnilistId, anime.AllanimeId, anime.Ep.Number, anime.Ep.Player.PlaybackTime), logFile) 428 // anime.AnilistId, anime.AllanimeId, anime.Ep.Number, anime.Ep.Player.PlaybackTime), logFile)
429 } 429 }
430 } 430 }
431
432 // Check if anything is playing, if nothing is playing and episode was started, start next episode
433 hasPlayback, err := internal.HasActivePlayback(anime.Ep.Player.SocketPath)
434 if err != nil {
435 internal.Log("Error checking playback status: "+err.Error(), logFile)
436 } else if !hasPlayback && anime.Ep.Started {
437 // Nothing is playing, start next episode
438 internal.Log("Nothing playing in MPV, starting next episode", logFile)
439 anime.Ep.Number++
440 anime.Ep.Started = false
441 anime.Ep.IsCompleted = true
442 close(skipLoopDone)
443 return
444 }
445
431 } 446 }
432 } 447 }
433 }() 448 }()
internal/curd.go +1 −1
@@ -1051,7 +1051,7 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string
1051 } else { 1051 } else {
1052 CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(*anime), anime.Ep.Number)) 1052 CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(*anime), anime.Ep.Number))
1053 } 1053 }
1054 mpvSocketPath, err := StartVideo(PrioritizeLink(anime.Ep.Links), []string{}, fmt.Sprintf("%s - Episode %d", GetAnimeName(*anime), anime.Ep.Number)) 1054 mpvSocketPath, err := StartVideo(PrioritizeLink(anime.Ep.Links), []string{}, fmt.Sprintf("%s - Episode %d", GetAnimeName(*anime), anime.Ep.Number), anime)
1055 1055
1056 if err != nil { 1056 if err != nil {
1057 Log("Failed to start mpv", logFile) 1057 Log("Failed to start mpv", logFile)
internal/localTracking.go +1 −1
@@ -327,7 +327,7 @@ func WatchUntracked(userCurdConfig *CurdConfig, logFile string) {
327 CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(anime), anime.Ep.Number)) 327 CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(anime), anime.Ep.Number))
328 328
329 // Start video playback 329 // Start video playback
330 mpvSocketPath, err := StartVideo(PrioritizeLink(link), []string{}, fmt.Sprintf("%s - Episode %d", GetAnimeName(anime), anime.Ep.Number)) 330 mpvSocketPath, err := StartVideo(PrioritizeLink(link), []string{}, fmt.Sprintf("%s - Episode %d", GetAnimeName(anime), anime.Ep.Number), &anime)
331 if err != nil { 331 if err != nil {
332 Log("Failed to start mpv", logFile) 332 Log("Failed to start mpv", logFile)
333 os.Exit(1) 333 os.Exit(1)
internal/player.go +75 −5
@@ -9,7 +9,9 @@ import (
9 "os/exec" 9 "os/exec"
10 "path/filepath" 10 "path/filepath"
11 "runtime" 11 "runtime"
12 "strings"
12) 13)
14
13var logFile = "debug.log" 15var logFile = "debug.log"
14 16
15func getMPVPath() (string, error) { 17func getMPVPath() (string, error) {
@@ -22,12 +24,36 @@ func getMPVPath() (string, error) {
22 return mpvPath, nil 24 return mpvPath, nil
23} 25}
24 26
25func StartVideo(link string, args []string, title string) (string, error) { 27func StartVideo(link string, args []string, title string, anime *Anime) (string, error) {
26 var command *exec.Cmd 28 var command *exec.Cmd
29 var mpvSocketPath string
30 var err error
31
32 // Check if we have an existing socket and if MPV is still running
33 if anime.Ep.Player.SocketPath != "" && IsMPVRunning(anime.Ep.Player.SocketPath) {
34 // Reuse existing socket
35 mpvSocketPath = anime.Ep.Player.SocketPath
36
37 // Load the new file in the existing MPV instance
38 command := []interface{}{"loadfile", link}
39 _, err = MPVSendCommand(mpvSocketPath, command)
40 if err != nil {
41 return "", fmt.Errorf("failed to load file in existing MPV instance: %w", err)
42 }
43
44 // Set the new title
45 _, err = MPVSendCommand(mpvSocketPath, []interface{}{"set_property", "force-media-title", title})
46 if err != nil {
47 Log(fmt.Sprintf("Failed to set title: %v", err), logFile)
48 }
27 49
50 return mpvSocketPath, nil
51 }
52
53 if anime.Ep.Player.SocketPath == "" {
28 // Generate a random number for the socket path 54 // Generate a random number for the socket path
29 randomBytes := make([]byte, 4) 55 randomBytes := make([]byte, 4)
30 _, err := rand.Read(randomBytes) 56 _, err = rand.Read(randomBytes)
31 if err != nil { 57 if err != nil {
32 Log("Failed to generate random number", logFile) 58 Log("Failed to generate random number", logFile)
33 return "", fmt.Errorf("failed to generate random number: %w", err) 59 return "", fmt.Errorf("failed to generate random number: %w", err)
@@ -36,15 +62,20 @@ func StartVideo(link string, args []string, title string) (string, error) {
36 randomNumber := fmt.Sprintf("%x", randomBytes) 62 randomNumber := fmt.Sprintf("%x", randomBytes)
37 63
38 // Create the mpv socket path with the random number 64 // Create the mpv socket path with the random number
39 var mpvSocketPath string
40 if runtime.GOOS == "windows" { 65 if runtime.GOOS == "windows" {
41 mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber) 66 mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber)
42 } else { 67 } else {
43 mpvSocketPath = fmt.Sprintf("/tmp/curd_mpvsocket_%s", randomNumber) 68 mpvSocketPath = fmt.Sprintf("/tmp/curd_mpvsocket_%s", randomNumber)
69 }
70 } else {
71 mpvSocketPath = anime.Ep.Player.SocketPath
44 } 72 }
45 73
46 // Add the title to MPV arguments 74 // Add the title to MPV arguments
47 titleArgs := []string{fmt.Sprintf("--title=%s", title),fmt.Sprintf("--force-media-title=%s", title)} 75 titleArgs := []string{fmt.Sprintf("--title=%s", title), fmt.Sprintf("--force-media-title=%s", title)}
76
77 // Keep the window open after episode completes, new episode starts in the same mpv window
78 args = append(args, "--force-window=yes", "--idle=yes")
48 args = append(args, titleArgs...) 79 args = append(args, titleArgs...)
49 80
50 // Prepare arguments for mpv 81 // Prepare arguments for mpv
@@ -52,7 +83,8 @@ func StartVideo(link string, args []string, title string) (string, error) {
52 mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link) 83 mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link)
53 // Add any additional arguments passed 84 // Add any additional arguments passed
54 if len(args) > 0 { 85 if len(args) > 0 {
55 mpvArgs = append(mpvArgs, args...) } 86 mpvArgs = append(mpvArgs, args...)
87 }
56 88
57 if runtime.GOOS == "windows" { 89 if runtime.GOOS == "windows" {
58 // Get the path to mpv.exe for Windows 90 // Get the path to mpv.exe for Windows
@@ -192,3 +224,41 @@ func PercentageWatched(playbackTime int, duration int) float64 {
192 } 224 }
193 return float64(0) 225 return float64(0)
194} 226}
227
228func HasActivePlayback(ipcSocketPath string) (bool, error) {
229 // Get the time-pos property from MPV
230 timePos, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "time-pos"})
231 if err != nil {
232 // Check specifically for "property unavailable" error
233 if strings.Contains(err.Error(), "property unavailable") {
234 // This indicates nothing is playing
235 return false, nil
236 }
237 return false, fmt.Errorf("error getting time-pos: %w", err)
238 }
239
240 // If we got a valid response, something is playing
241 if timePos != nil {
242 return true, nil
243 }
244
245 // Default to false if we can't determine the status
246 return false, nil
247}
248
249func IsMPVRunning(socketPath string) bool {
250 if socketPath == "" {
251 return false
252 }
253
254 // Try to connect to the socket
255 conn, err := connectToPipe(socketPath)
256 if err != nil {
257 return false
258 }
259 defer conn.Close()
260
261 // Send a simple command to check if MPV responds
262 _, err = MPVSendCommand(socketPath, []interface{}{"get_property", "pid"})
263 return err == nil
264}