我如何像在 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 获取:不支持的协议(protocol)方案“”
最佳答案
这是使用标准 http 处理函数的可能答案。我没有直接测试它的程序,但它确实可以作为代理使用一些简单的 shell 命令。
func encodeAudio(w http.ResponseWriter, req *http.Request) {
streamLink := exec.Command("youtube-dl", "-f", "140", "-g", "https://www.youtube.com/watch?v=VIDEOID")
out, err := streamLink.Output()
if err != nil {
log.Fatal(err)
}
cmdFF := exec.Command("ffmpeg", "-i", "pipe:0", "-acodec", "libmp3lame", "-f", "mp3", "-")
resp, err := http.Get(string(out))
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
cmdFF.Stdin = resp.Body
cmdFF.Stdout = w
if err := cmdFF.Run(); err != nil {
log.Fatal(err)
}
}
关于ffmpeg - 管道 HTTP 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25023502/