我该如何像在NodeJS中那样将HTTP响应进行管道传输呢?以下是我在NodeJS中使用的代码片段:
request({
url: audio_file_url,
}).pipe(ffmpeg_process.stdin);
我该如何在Go中实现相同的结果?
我正在尝试将来自HTTP的音频流传输到FFmpeg进程中,以便它可以即时转换并将转换后的文件返回给客户端。
以下是我的源代码,以便大家清楚明白:
func encodeAudio(w http.ResponseWriter, req *http.Request) {
path, err := exec.LookPath("youtube-dl")
if err != nil {
log.Fatal("LookPath: ", err)
}
path_ff, err_ff := exec.LookPath("ffmpeg")
if err != nil {
log.Fatal("LookPath: ", err_ff)
}
streamLink := exec.Command(path,"-f", "140", "-g", "https://www.youtube.com/watch?v=VIDEOID")
var out bytes.Buffer
streamLink.Stdout = &out
cmdFF := exec.Command(path_ff, "-i", "pipe:0", "-acodec", "libmp3lame", "-f", "mp3", "-")
resp, err := http.Get(out.String())
if err != nil {
log.Fatal(err)
}
// pr, pw := io.Pipe()
defer resp.Body.Close()
cmdFF.Stdin = resp.Body
cmdFF.Stdout = w
streamLink.Run()
//get ffmpeg running in another goroutine to receive data
errCh := make(chan error, 1)
go func() {
errCh <- cmdFF.Run()
}()
// close the pipeline to signal the end of the stream
// pw.Close()
// pr.Close()
// check for an error from ffmpeg
if err := <-errCh; err != nil {
// ff error
}
}
错误: 2014/07/29 23:04:02 获取:不支持的协议方案“”
exec.LookPath
可以找到可执行文件,那么您不需要使用它,exec.Command
也能找到它。 - OneOfOneout
。这里也有很多多余的东西。阅读文档可能会帮助澄清一些混淆。 - JimB