|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "os/signal" |
| 12 | + "path/filepath" |
| 13 | + "runtime" |
| 14 | + "syscall" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + url = flag.String("url", "", "Stream URL to play") |
| 20 | + volume = flag.Int("volume", 100, "Volume level (0-100)") |
| 21 | + device = flag.String("device", "", "Audio device to use") |
| 22 | + format = flag.String("format", "mp3", "Stream format (mp3, aac, etc.)") |
| 23 | + player *exec.Cmd |
| 24 | + stopChan = make(chan struct{}) |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + flag.Parse() |
| 29 | + |
| 30 | + if *url == "" { |
| 31 | + fmt.Println("Please provide a stream URL using -url flag") |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + |
| 35 | + // Handle interrupt signals |
| 36 | + sigChan := make(chan os.Signal, 1) |
| 37 | + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 38 | + |
| 39 | + // Start the stream |
| 40 | + go playStream() |
| 41 | + |
| 42 | + // Wait for interrupt signal |
| 43 | + <-sigChan |
| 44 | + stopStream() |
| 45 | +} |
| 46 | + |
| 47 | +func playStream() { |
| 48 | + // Determine the appropriate player command based on the OS |
| 49 | + var cmd *exec.Cmd |
| 50 | + switch runtime.GOOS { |
| 51 | + case "windows": |
| 52 | + cmd = exec.Command("ffplay", "-nodisp", "-autoexit", "-volume", fmt.Sprintf("%d", *volume), *url) |
| 53 | + case "darwin": |
| 54 | + cmd = exec.Command("ffplay", "-nodisp", "-autoexit", "-volume", fmt.Sprintf("%d", *volume), *url) |
| 55 | + case "linux": |
| 56 | + cmd = exec.Command("ffplay", "-nodisp", "-autoexit", "-volume", fmt.Sprintf("%d", *volume), *url) |
| 57 | + default: |
| 58 | + fmt.Printf("Unsupported operating system: %s\n", runtime.GOOS) |
| 59 | + os.Exit(1) |
| 60 | + } |
| 61 | + |
| 62 | + // Set up pipes for communication |
| 63 | + stdout, err := cmd.StdoutPipe() |
| 64 | + if err != nil { |
| 65 | + fmt.Printf("Error setting up stdout pipe: %v\n", err) |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | + |
| 69 | + stderr, err := cmd.StderrPipe() |
| 70 | + if err != nil { |
| 71 | + fmt.Printf("Error setting up stderr pipe: %v\n", err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + |
| 75 | + // Start the player |
| 76 | + if err := cmd.Start(); err != nil { |
| 77 | + fmt.Printf("Error starting player: %v\n", err) |
| 78 | + os.Exit(1) |
| 79 | + } |
| 80 | + |
| 81 | + player = cmd |
| 82 | + |
| 83 | + // Monitor the stream |
| 84 | + go monitorStream(stdout, stderr) |
| 85 | + |
| 86 | + // Wait for the player to finish |
| 87 | + if err := cmd.Wait(); err != nil { |
| 88 | + fmt.Printf("Player finished with error: %v\n", err) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func monitorStream(stdout, stderr io.ReadCloser) { |
| 93 | + // Monitor stdout |
| 94 | + go func() { |
| 95 | + scanner := bufio.NewScanner(stdout) |
| 96 | + for scanner.Scan() { |
| 97 | + line := scanner.Text() |
| 98 | + fmt.Printf("Player output: %s\n", line) |
| 99 | + } |
| 100 | + }() |
| 101 | + |
| 102 | + // Monitor stderr |
| 103 | + go func() { |
| 104 | + scanner := bufio.NewScanner(stderr) |
| 105 | + for scanner.Scan() { |
| 106 | + line := scanner.Text() |
| 107 | + fmt.Printf("Player error: %s\n", line) |
| 108 | + } |
| 109 | + }() |
| 110 | + |
| 111 | + // Monitor the stream URL |
| 112 | + go func() { |
| 113 | + for { |
| 114 | + select { |
| 115 | + case <-stopChan: |
| 116 | + return |
| 117 | + default: |
| 118 | + resp, err := http.Get(*url) |
| 119 | + if err != nil { |
| 120 | + fmt.Printf("Error checking stream: %v\n", err) |
| 121 | + time.Sleep(5 * time.Second) |
| 122 | + continue |
| 123 | + } |
| 124 | + resp.Body.Close() |
| 125 | + time.Sleep(30 * time.Second) |
| 126 | + } |
| 127 | + } |
| 128 | + }() |
| 129 | +} |
| 130 | + |
| 131 | +func stopStream() { |
| 132 | + if player != nil { |
| 133 | + close(stopChan) |
| 134 | + player.Process.Kill() |
| 135 | + player.Wait() |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// Helper function to get the executable path |
| 140 | +func getExecutablePath() string { |
| 141 | + ex, err := os.Executable() |
| 142 | + if err != nil { |
| 143 | + return "" |
| 144 | + } |
| 145 | + return filepath.Dir(ex) |
| 146 | +} |
| 147 | + |
| 148 | +// Helper function to check if a URL is valid |
| 149 | +func isValidURL(url string) bool { |
| 150 | + resp, err := http.Get(url) |
| 151 | + if err != nil { |
| 152 | + return false |
| 153 | + } |
| 154 | + defer resp.Body.Close() |
| 155 | + return resp.StatusCode == http.StatusOK |
| 156 | +} |
0 commit comments