繁星coding
好的,这就是我最终要做的,我的 Go 代码通过 ffmpeg 过程将原始 PCM 数据通过管道传输,该过程将其转换为 mp3。我假设packets是我的音频数据出现的通道,并且response是一个 http.ResponseWritercmd := exec.Command("ffmpeg", "-v", "verbose", "-f", "u16le", "-ar", "44100", "-ac", "1", "-i", "-", "-f", "mp3", "-")audioIn, err := cmd.StdinPipe()if err != nil { log.Println("Failed to create stdin pipe")}audioOut, err := cmd.StdoutPipe()if err != nil { log.Println("Failed to create stdout pipe")}err = cmd.Start()if err != nil { log.Println("Failed to start ffmpeg command, error: ", err)}go func() { for { packet := <-packets audioIn.Write(packet.Payload) }}go func() { amount, err := io.Copy(response, audioOut) if err != nil { log.Println("io copy terminated with an error", err) } log.Printf("Done copying audio data: %d bytes\n", amount)}()err = cmd.Wait()if err != nil { log.Println("ffmpeg command terminated incorrectly", err)}