문제
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 가져오기:지원되지 않는 프로토콜 구성표 ""
해결책
표준 HTTP 처리기 기능을 사용하여 가능한 답변이 있습니다.나는 이것을 직접 테스트 할 프로그램이 없지만 프록시로 서있는 간단한 쉘 명령으로 작동합니다.
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)
}
}
. 다른 팁
http.Request.Body
이다 io.ReadCloser
, 그래서 파이프로 연결할 수 있습니다 exec.Cmd
.Stdin:
func Handler(rw http.ResponseWriter, req *http.Request) {
cmd := exec.Command("ffmpeg", other, args, ...)
cmd.Stdin = req.Body
go func() {
defer req.Body.Close()
if err := cmd.Run(); err != nil {
// do something
}
}()
//redirect the user and check for progress?
}
//편집 질문을 오해했지만 대답은 여전히 유효합니다. http.Get
버전:
http.Response.Body
이다 io.ReadCloser
처럼 http.Request.Body
.
func EncodeUrl(url, fn string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
cmd := exec.Command("ffmpeg", ......, fn)
cmd.Stdin = resp.Body
return cmd.Run()
}
//편집2:
이것 ~해야 한다 마티니 문서에 따르면 작동하지만 다시 한 번 사용법을 배우는 것이 좋습니다. 서브먹스 아니면 적어도 사용 고릴라.
m := martini.Classic()
m.Get("/stream/:ytid", func(params martini.Params, rw http.ResponseWriter,
req *http.Request) string {
ytid := params["ytid"]
stream_link := exec.Command("youtube-dl","-f", "140", "-g", "https://www.youtube.com/watch?v=" + ytid)
var out bytes.Buffer
stream_link.Stdout = &out
errr := stream_link.Run()
if err != nil {
log.Fatal(err)
}
log.Println("Link", out.String())
cmd_ff := exec.Command("ffmpeg", "-i", "pipe:0", "-acodec", "libmp3lame", "-f", "mp3", "-")
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
cmd_ff.Stdin = resp.Body
go func() {
defer resp.Body.Close()
if err := cmd_ff.Run(); err != nil {
log.Fatal(err)
}
}()
return "Youtube ID: " + ytid
})
m.Run()
제휴하지 않습니다 StackOverflow