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