| @@ -3,7 +3,6 @@ package internal |
| 3 | 3 | import ( |
| 4 | 4 | "crypto/rand" |
| 5 | 5 | "encoding/json" |
| 6 | | "errors" |
| 7 | 6 | "fmt" |
| 8 | 7 | "os" |
| 9 | 8 | "os/exec" |
| @@ -16,16 +15,137 @@ import ( |
| 16 | 15 | |
| 17 | 16 | var logFile = "debug.log" |
| 18 | 17 | |
| 19 | | func getMPVPath() (string, error) { |
| 18 | func getBundledMPVPath() (string, error) { |
| 20 | 19 | exePath, err := os.Executable() |
| 21 | 20 | if err != nil { |
| 22 | 21 | return "", err |
| 23 | 22 | } |
| 24 | 23 | exeDir := filepath.Dir(exePath) |
| 25 | | mpvPath := filepath.Join(exeDir, "bin", "mpv.exe") // Adjust the relative path |
| 24 | mpvPath := filepath.Join(exeDir, "bin", "mpv.exe") |
| 26 | 25 | return mpvPath, nil |
| 27 | 26 | } |
| 28 | 27 | |
| 28 | func resolveExecutable(binary string) (string, error) { |
| 29 | binary = strings.TrimSpace(binary) |
| 30 | if binary == "" { |
| 31 | return "", fmt.Errorf("empty binary name") |
| 32 | } |
| 33 | |
| 34 | if filepath.IsAbs(binary) || strings.Contains(binary, "/") || strings.Contains(binary, "\\") { |
| 35 | if _, err := os.Stat(binary); err == nil { |
| 36 | return binary, nil |
| 37 | } |
| 38 | return "", fmt.Errorf("binary path not found: %s", binary) |
| 39 | } |
| 40 | |
| 41 | resolvedPath, err := exec.LookPath(binary) |
| 42 | if err != nil { |
| 43 | return "", err |
| 44 | } |
| 45 | |
| 46 | return resolvedPath, nil |
| 47 | } |
| 48 | |
| 49 | func candidatePlayerBinaries(configuredPlayer string) []string { |
| 50 | player := strings.TrimSpace(configuredPlayer) |
| 51 | if player == "" { |
| 52 | player = "mpv" |
| 53 | } |
| 54 | |
| 55 | var candidates []string |
| 56 | addUnique := func(value string) { |
| 57 | value = strings.TrimSpace(value) |
| 58 | if value == "" { |
| 59 | return |
| 60 | } |
| 61 | for _, existing := range candidates { |
| 62 | if existing == value { |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | candidates = append(candidates, value) |
| 67 | } |
| 68 | |
| 69 | addUnique(player) |
| 70 | |
| 71 | if strings.EqualFold(player, "iina") { |
| 72 | // iina is mpv-based and may be exposed either on PATH or via app bundle. |
| 73 | addUnique("iina") |
| 74 | if runtime.GOOS == "darwin" { |
| 75 | addUnique("/Applications/IINA.app/Contents/MacOS/IINA") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return candidates |
| 80 | } |
| 81 | |
| 82 | func resolveMPVBinary() (string, error) { |
| 83 | if runtime.GOOS == "windows" { |
| 84 | bundledMPVPath, err := getBundledMPVPath() |
| 85 | if err == nil { |
| 86 | if _, statErr := os.Stat(bundledMPVPath); statErr == nil { |
| 87 | return bundledMPVPath, nil |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return resolveExecutable("mpv") |
| 93 | } |
| 94 | |
| 95 | func resolveConfiguredPlayerBinary(configuredPlayer string) (string, string, error) { |
| 96 | configuredPlayer = strings.TrimSpace(configuredPlayer) |
| 97 | if configuredPlayer == "" { |
| 98 | configuredPlayer = "mpv" |
| 99 | } |
| 100 | |
| 101 | for _, candidate := range candidatePlayerBinaries(configuredPlayer) { |
| 102 | resolvedPath, err := resolveExecutable(candidate) |
| 103 | if err == nil { |
| 104 | return resolvedPath, configuredPlayer, nil |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | mpvPath, mpvErr := resolveMPVBinary() |
| 109 | if mpvErr != nil { |
| 110 | return "", "", fmt.Errorf("configured player %q was not found and fallback to mpv failed: %w", configuredPlayer, mpvErr) |
| 111 | } |
| 112 | |
| 113 | if !strings.EqualFold(configuredPlayer, "mpv") { |
| 114 | warning := fmt.Sprintf("Configured player '%s' was not found. Falling back to mpv.", configuredPlayer) |
| 115 | CurdOut(warning) |
| 116 | Log(warning) |
| 117 | } |
| 118 | |
| 119 | return mpvPath, "mpv", nil |
| 120 | } |
| 121 | |
| 122 | func isIINAPlayer(effectivePlayerName string, resolvedPlayerBinary string) bool { |
| 123 | if strings.EqualFold(strings.TrimSpace(effectivePlayerName), "iina") { |
| 124 | return true |
| 125 | } |
| 126 | |
| 127 | binaryName := strings.TrimSuffix(filepath.Base(resolvedPlayerBinary), filepath.Ext(resolvedPlayerBinary)) |
| 128 | return strings.EqualFold(binaryName, "iina") |
| 129 | } |
| 130 | |
| 131 | func translateMPVArgsForIINA(mpvArgs []string) []string { |
| 132 | translated := make([]string, 0, len(mpvArgs)) |
| 133 | for _, arg := range mpvArgs { |
| 134 | if strings.HasPrefix(arg, "--") { |
| 135 | if strings.HasPrefix(arg, "--mpv-") { |
| 136 | translated = append(translated, arg) |
| 137 | continue |
| 138 | } |
| 139 | translated = append(translated, "--mpv-"+strings.TrimPrefix(arg, "--")) |
| 140 | continue |
| 141 | } |
| 142 | |
| 143 | translated = append(translated, arg) |
| 144 | } |
| 145 | |
| 146 | return translated |
| 147 | } |
| 148 | |
| 29 | 149 | func StartVideo(link string, args []string, title string, anime *Anime) (string, error) { |
| 30 | 150 | var command *exec.Cmd |
| 31 | 151 | var mpvSocketPath string |
| @@ -98,13 +218,14 @@ func StartVideo(link string, args []string, title string, anime *Anime) (string, |
| 98 | 218 | args = append(args, "--force-window=yes", "--idle=yes") |
| 99 | 219 | args = append(args, titleArgs...) |
| 100 | 220 | |
| 101 | | // Prepare arguments for mpv |
| 221 | // Prepare arguments for mpv-compatible players. |
| 102 | 222 | var mpvArgs []string |
| 103 | | mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link) |
| 223 | mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath)) |
| 104 | 224 | // Add any additional arguments passed |
| 105 | 225 | if len(args) > 0 { |
| 106 | 226 | mpvArgs = append(mpvArgs, args...) |
| 107 | 227 | } |
| 228 | mpvArgs = append(mpvArgs, link) |
| 108 | 229 | |
| 109 | 230 | // Check for Android environment |
| 110 | 231 | amPath, err := exec.LookPath("am") |
| @@ -128,36 +249,26 @@ func StartVideo(link string, args []string, title string, anime *Anime) (string, |
| 128 | 249 | return "android-intent", nil |
| 129 | 250 | } |
| 130 | 251 | |
| 131 | | if runtime.GOOS == "windows" { |
| 132 | | // Get the path to mpv.exe for Windows |
| 133 | | mpvPath, err := getMPVPath() |
| 134 | | if err != nil { |
| 135 | | CurdOut("Error: Failed to get MPV path") |
| 136 | | Log("Failed to get mpv path.") |
| 137 | | return "", err |
| 138 | | } |
| 139 | | |
| 140 | | if _, err := os.Stat(mpvPath); err == nil { |
| 141 | | command = exec.Command(mpvPath, mpvArgs...) |
| 142 | | |
| 143 | | } else if errors.Is(err, os.ErrNotExist) { |
| 144 | | //for windows with mpv on PATH |
| 145 | | command = exec.Command("mpv", mpvArgs...) |
| 252 | resolvedPlayerBinary, effectivePlayerName, err := resolveConfiguredPlayerBinary(userConfig.Player) |
| 253 | if err != nil { |
| 254 | CurdOut("Error: Failed to resolve media player") |
| 255 | Log(fmt.Sprintf("Player resolution failed for '%s': %v", userConfig.Player, err)) |
| 256 | return "", err |
| 257 | } |
| 146 | 258 | |
| 147 | | } else { |
| 148 | | Log("Failed to get mpv path.") |
| 149 | | return "", err |
| 150 | | } |
| 151 | | } else { |
| 152 | | // Create command for Unix-like systems |
| 153 | | command = exec.Command("mpv", mpvArgs...) |
| 259 | playerArgs := mpvArgs |
| 260 | if isIINAPlayer(effectivePlayerName, resolvedPlayerBinary) { |
| 261 | playerArgs = translateMPVArgsForIINA(mpvArgs) |
| 262 | playerArgs = append(playerArgs, "--no-stdin") |
| 154 | 263 | } |
| 155 | 264 | |
| 156 | | // Start the mpv process |
| 265 | command = exec.Command(resolvedPlayerBinary, playerArgs...) |
| 266 | |
| 267 | // Start the selected mpv-compatible player process |
| 157 | 268 | err = command.Start() |
| 158 | 269 | if err != nil { |
| 159 | | CurdOut("Error: Failed to start mpv process") |
| 160 | | return "", fmt.Errorf("failed to start mpv: %w", err) |
| 270 | CurdOut(fmt.Sprintf("Error: Failed to start %s process", effectivePlayerName)) |
| 271 | return "", fmt.Errorf("failed to start %s: %w", effectivePlayerName, err) |
| 161 | 272 | } |
| 162 | 273 | |
| 163 | 274 | // Wait for the socket to become available with retries |