Commit 03a9725c25

03a9725c2520bb33fbc1c3f19360c070125fb493

parent: 539f1b8a97

Unsigned

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

Fix: Update player title after loadfile

Layout: unified · split

internal/player.go +156 −144
@@ -10,22 +10,23 @@ import (
10 "path/filepath" 10 "path/filepath"
11 "runtime" 11 "runtime"
12 "strings" 12 "strings"
13 "time"
13) 14)
14 15
15var logFile = "debug.log" 16var logFile = "debug.log"
16 17
17func getMPVPath() (string, error) { 18func getMPVPath() (string, error) {
18 exePath, err := os.Executable() 19 exePath, err := os.Executable()
19 if err != nil { 20 if err != nil {
20 return "", err 21 return "", err
21 } 22 }
22 exeDir := filepath.Dir(exePath) 23 exeDir := filepath.Dir(exePath)
23 mpvPath := filepath.Join(exeDir, "bin", "mpv.exe") // Adjust the relative path 24 mpvPath := filepath.Join(exeDir, "bin", "mpv.exe") // Adjust the relative path
24 return mpvPath, nil 25 return mpvPath, nil
25} 26}
26 27
27func StartVideo(link string, args []string, title string, anime *Anime) (string, error) { 28func StartVideo(link string, args []string, title string, anime *Anime) (string, error) {
28 var command *exec.Cmd 29 var command *exec.Cmd
29 var mpvSocketPath string 30 var mpvSocketPath string
30 var err error 31 var err error
31 32
@@ -41,188 +42,199 @@ func StartVideo(link string, args []string, title string, anime *Anime) (string,
41 return "", fmt.Errorf("failed to load file in existing MPV instance: %w", err) 42 return "", fmt.Errorf("failed to load file in existing MPV instance: %w", err)
42 } 43 }
43 44
44 // Set the new title 45 // Wait a brief moment for the file to load
45 _, err = MPVSendCommand(mpvSocketPath, []interface{}{"set_property", "force-media-title", title}) 46 time.Sleep(100 * time.Millisecond)
47
48 // Update the window title
49 titleCommand := []interface{}{"set_property", "force-media-title", title}
50 _, err = MPVSendCommand(mpvSocketPath, titleCommand)
46 if err != nil { 51 if err != nil {
47 Log(fmt.Sprintf("Failed to set title: %v", err), logFile) 52 Log(fmt.Sprintf("Failed to update title: %v", err), logFile)
53 }
54
55 // Also update the window title property
56 windowTitleCommand := []interface{}{"set_property", "title", title}
57 _, err = MPVSendCommand(mpvSocketPath, windowTitleCommand)
58 if err != nil {
59 Log(fmt.Sprintf("Failed to update window title: %v", err), logFile)
48 } 60 }
49 61
50 return mpvSocketPath, nil 62 return mpvSocketPath, nil
51 } 63 }
52 64
53 if anime.Ep.Player.SocketPath == "" { 65 if anime.Ep.Player.SocketPath == "" {
54 // Generate a random number for the socket path 66 // Generate a random number for the socket path
55 randomBytes := make([]byte, 4) 67 randomBytes := make([]byte, 4)
56 _, err = rand.Read(randomBytes) 68 _, err = rand.Read(randomBytes)
57 if err != nil { 69 if err != nil {
58 Log("Failed to generate random number", logFile) 70 Log("Failed to generate random number", logFile)
59 return "", fmt.Errorf("failed to generate random number: %w", err) 71 return "", fmt.Errorf("failed to generate random number: %w", err)
60 } 72 }
61 73
62 randomNumber := fmt.Sprintf("%x", randomBytes) 74 randomNumber := fmt.Sprintf("%x", randomBytes)
63 75
64 // Create the mpv socket path with the random number 76 // Create the mpv socket path with the random number
65 if runtime.GOOS == "windows" { 77 if runtime.GOOS == "windows" {
66 mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber) 78 mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber)
67 } else { 79 } else {
68 mpvSocketPath = fmt.Sprintf("/tmp/curd_mpvsocket_%s", randomNumber) 80 mpvSocketPath = fmt.Sprintf("/tmp/curd_mpvsocket_%s", randomNumber)
69 } 81 }
70 } else { 82 } else {
71 mpvSocketPath = anime.Ep.Player.SocketPath 83 mpvSocketPath = anime.Ep.Player.SocketPath
72 } 84 }
73 85
74 // Add the title to MPV arguments 86 // Add the title to MPV arguments
75 titleArgs := []string{fmt.Sprintf("--title=%s", title), fmt.Sprintf("--force-media-title=%s", title)} 87 titleArgs := []string{fmt.Sprintf("--title=%s", title), fmt.Sprintf("--force-media-title=%s", title)}
76 88
77 // Keep the window open after episode completes, new episode starts in the same mpv window 89 // Keep the window open after episode completes, new episode starts in the same mpv window
78 args = append(args, "--force-window=yes", "--idle=yes") 90 args = append(args, "--force-window=yes", "--idle=yes")
79 args = append(args, titleArgs...) 91 args = append(args, titleArgs...)
80 92
81 // Prepare arguments for mpv 93 // Prepare arguments for mpv
82 var mpvArgs []string 94 var mpvArgs []string
83 mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link) 95 mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link)
84 // Add any additional arguments passed 96 // Add any additional arguments passed
85 if len(args) > 0 { 97 if len(args) > 0 {
86 mpvArgs = append(mpvArgs, args...) 98 mpvArgs = append(mpvArgs, args...)
87 } 99 }
88 100
89 if runtime.GOOS == "windows" { 101 if runtime.GOOS == "windows" {
90 // Get the path to mpv.exe for Windows 102 // Get the path to mpv.exe for Windows
91 mpvPath, err := getMPVPath() 103 mpvPath, err := getMPVPath()
92 if err != nil { 104 if err != nil {
93 CurdOut("Error: Failed to get MPV path") 105 CurdOut("Error: Failed to get MPV path")
94 Log("Failed to get mpv path.", logFile) 106 Log("Failed to get mpv path.", logFile)
95 return "", err 107 return "", err
96 } 108 }
97 109
98 // Create command for Windows 110 // Create command for Windows
99 command = exec.Command(mpvPath, mpvArgs...) 111 command = exec.Command(mpvPath, mpvArgs...)
100 } else { 112 } else {
101 // Create command for Unix-like systems 113 // Create command for Unix-like systems
102 command = exec.Command("mpv", mpvArgs...) 114 command = exec.Command("mpv", mpvArgs...)
103 } 115 }
104 116
105 // Start the mpv process 117 // Start the mpv process
106 err = command.Start() 118 err = command.Start()
107 if err != nil { 119 if err != nil {
108 CurdOut("Error: Failed to start mpv process") 120 CurdOut("Error: Failed to start mpv process")
109 return "", fmt.Errorf("failed to start mpv: %w", err) 121 return "", fmt.Errorf("failed to start mpv: %w", err)
110 } 122 }
111 return mpvSocketPath, nil 123 return mpvSocketPath, nil
112} 124}
113 125
114// Helper function to join args with a space 126// Helper function to join args with a space
115func joinArgs(args []string) string { 127func joinArgs(args []string) string {
116 result := "" 128 result := ""
117 for i, arg := range args { 129 for i, arg := range args {
118 if i > 0 { 130 if i > 0 {
119 result += " " 131 result += " "
120 } 132 }
121 result += arg 133 result += arg
122 } 134 }
123 return result 135 return result
124} 136}
125 137
126func MPVSendCommand(ipcSocketPath string, command []interface{}) (interface{}, error) { 138func MPVSendCommand(ipcSocketPath string, command []interface{}) (interface{}, error) {
127 conn, err := connectToPipe(ipcSocketPath) 139 conn, err := connectToPipe(ipcSocketPath)
128 if err != nil { 140 if err != nil {
129 return nil, err 141 return nil, err
130 } 142 }
131 defer conn.Close() 143 defer conn.Close()
132 144
133 commandStr, err := json.Marshal(map[string]interface{}{ 145 commandStr, err := json.Marshal(map[string]interface{}{
134 "command": command, 146 "command": command,
135 }) 147 })
136 if err != nil { 148 if err != nil {
137 return nil, err 149 return nil, err
138 } 150 }
139 151
140 // Send the command 152 // Send the command
141 _, err = conn.Write(append(commandStr, '\n')) 153 _, err = conn.Write(append(commandStr, '\n'))
142 if err != nil { 154 if err != nil {
143 return nil, err 155 return nil, err
144 } 156 }
145 157
146 // Receive the response 158 // Receive the response
147 buf := make([]byte, 4096) 159 buf := make([]byte, 4096)
148 n, err := conn.Read(buf) 160 n, err := conn.Read(buf)
149 if err != nil { 161 if err != nil {
150 return nil, err 162 return nil, err
151 } 163 }
152 164
153 var response map[string]interface{} 165 var response map[string]interface{}
154 if err := json.Unmarshal(buf[:n], &response); err != nil { 166 if err := json.Unmarshal(buf[:n], &response); err != nil {
155 return nil, err 167 return nil, err
156 } 168 }
157 169
158 if data, exists := response["data"]; exists { 170 if data, exists := response["data"]; exists {
159 return data, nil 171 return data, nil
160 } 172 }
161 173
162 return nil, nil 174 return nil, nil
163} 175}
164 176
165func SeekMPV(ipcSocketPath string, time int) (interface{}, error) { 177func SeekMPV(ipcSocketPath string, time int) (interface{}, error) {
166 command := []interface{}{"seek", time, "absolute"} 178 command := []interface{}{"seek", time, "absolute"}
167 return MPVSendCommand(ipcSocketPath, command) 179 return MPVSendCommand(ipcSocketPath, command)
168} 180}
169 181
170func GetMPVPausedStatus(ipcSocketPath string) (bool, error) { 182func GetMPVPausedStatus(ipcSocketPath string) (bool, error) {
171 status, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "pause"}) 183 status, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "pause"})
172 if err != nil || status == nil { 184 if err != nil || status == nil {
173 return false, err 185 return false, err
174 } 186 }
175 187
176 paused, ok := status.(bool) 188 paused, ok := status.(bool)
177 if ok { 189 if ok {
178 return paused, nil 190 return paused, nil
179 } 191 }
180 return false, nil 192 return false, nil
181} 193}
182 194
183func GetMPVPlaybackSpeed(ipcSocketPath string) (float64, error) { 195func GetMPVPlaybackSpeed(ipcSocketPath string) (float64, error) {
184 speed, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "speed"}) 196 speed, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "speed"})
185 if err != nil || speed == nil { 197 if err != nil || speed == nil {
186 Log("Failed to get playback speed.", logFile) 198 Log("Failed to get playback speed.", logFile)
187 return 0, err 199 return 0, err
188 } 200 }
189 201
190 currentSpeed, ok := speed.(float64) 202 currentSpeed, ok := speed.(float64)
191 if ok { 203 if ok {
192 return currentSpeed, nil 204 return currentSpeed, nil
193 } 205 }
194 206
195 return 0, nil 207 return 0, nil
196} 208}
197 209
198func GetPercentageWatched(ipcSocketPath string) (float64, error) { 210func GetPercentageWatched(ipcSocketPath string) (float64, error) {
199 currentTime, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "time-pos"}) 211 currentTime, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "time-pos"})
200 if err != nil || currentTime == nil { 212 if err != nil || currentTime == nil {
201 return 0, err 213 return 0, err
202 } 214 }
203 215
204 duration, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "duration"}) 216 duration, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "duration"})
205 if err != nil || duration == nil { 217 if err != nil || duration == nil {
206 return 0, err 218 return 0, err
207 } 219 }
208 220
209 currTime, ok1 := currentTime.(float64) 221 currTime, ok1 := currentTime.(float64)
210 dur, ok2 := duration.(float64) 222 dur, ok2 := duration.(float64)
211 223
212 if ok1 && ok2 && dur > 0 { 224 if ok1 && ok2 && dur > 0 {
213 percentageWatched := (currTime / dur) * 100 225 percentageWatched := (currTime / dur) * 100
214 return percentageWatched, nil 226 return percentageWatched, nil
215 } 227 }
216 228
217 return 0, nil 229 return 0, nil
218} 230}
219 231
220func PercentageWatched(playbackTime int, duration int) float64 { 232func PercentageWatched(playbackTime int, duration int) float64 {
221 if duration > 0 { 233 if duration > 0 {
222 percentage := (float64(playbackTime) / float64(duration)) * 100 234 percentage := (float64(playbackTime) / float64(duration)) * 100
223 return percentage 235 return percentage
224 } 236 }
225 return float64(0) 237 return float64(0)
226} 238}
227 239
228func HasActivePlayback(ipcSocketPath string) (bool, error) { 240func HasActivePlayback(ipcSocketPath string) (bool, error) {