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() {
185185
186186 anime.Ep.Player.Speed = 1.0
187187
188 // Main loop
188 // Main loop (loop to keep starting new episodes)
189189 for {
190190
191191 internal.Log(anime, logFile)
@@ -211,7 +211,7 @@ func main() {
211211 }
212212 }
213213
214 // Start curd
214 // Start curd (loop while episode is playing)
215215 for {
216216 // Check if current episode is filler/recap
217217 err = internal.GetEpisodeData(anime.MalId, anime.Ep.Number, &anime)
@@ -428,6 +428,21 @@ func main() {
428428 // anime.AnilistId, anime.AllanimeId, anime.Ep.Number, anime.Ep.Player.PlaybackTime), logFile)
429429 }
430430 }
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
431446 }
432447 }
433448 }()
internal/curd.go +1 −1
@@ -1051,7 +1051,7 @@ func StartCurd(userCurdConfig *CurdConfig, anime *Anime, logFile string) string
10511051 } else {
10521052 CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(*anime), anime.Ep.Number))
10531053 }
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)
10551055
10561056 if err != nil {
10571057 Log("Failed to start mpv", logFile)
internal/localTracking.go +1 −1
@@ -327,7 +327,7 @@ func WatchUntracked(userCurdConfig *CurdConfig, logFile string) {
327327 CurdOut(fmt.Sprintf("%s - Episode %d", GetAnimeName(anime), anime.Ep.Number))
328328
329329 // 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)
331331 if err != nil {
332332 Log("Failed to start mpv", logFile)
333333 os.Exit(1)
internal/player.go +75 −5
@@ -9,7 +9,9 @@ import (
99 "os/exec"
1010 "path/filepath"
1111 "runtime"
12 "strings"
1213)
14
1315var logFile = "debug.log"
1416
1517func getMPVPath() (string, error) {
@@ -22,12 +24,36 @@ func getMPVPath() (string, error) {
2224 return mpvPath, nil
2325}
2426
25func StartVideo(link string, args []string, title string) (string, error) {
27func StartVideo(link string, args []string, title string, anime *Anime) (string, error) {
2628 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 }
2749
50 return mpvSocketPath, nil
51 }
52
53 if anime.Ep.Player.SocketPath == "" {
2854 // Generate a random number for the socket path
2955 randomBytes := make([]byte, 4)
30 _, err := rand.Read(randomBytes)
56 _, err = rand.Read(randomBytes)
3157 if err != nil {
3258 Log("Failed to generate random number", logFile)
3359 return "", fmt.Errorf("failed to generate random number: %w", err)
@@ -36,15 +62,20 @@ func StartVideo(link string, args []string, title string) (string, error) {
3662 randomNumber := fmt.Sprintf("%x", randomBytes)
3763
3864 // Create the mpv socket path with the random number
39 var mpvSocketPath string
4065 if runtime.GOOS == "windows" {
4166 mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber)
4267 } else {
4368 mpvSocketPath = fmt.Sprintf("/tmp/curd_mpvsocket_%s", randomNumber)
69 }
70 } else {
71 mpvSocketPath = anime.Ep.Player.SocketPath
4472 }
4573
4674 // 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")
4879 args = append(args, titleArgs...)
4980
5081 // Prepare arguments for mpv
@@ -52,7 +83,8 @@ func StartVideo(link string, args []string, title string) (string, error) {
5283 mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link)
5384 // Add any additional arguments passed
5485 if len(args) > 0 {
55 mpvArgs = append(mpvArgs, args...) }
86 mpvArgs = append(mpvArgs, args...)
87 }
5688
5789 if runtime.GOOS == "windows" {
5890 // Get the path to mpv.exe for Windows
@@ -192,3 +224,41 @@ func PercentageWatched(playbackTime int, duration int) float64 {
192224 }
193225 return float64(0)
194226}
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}