drus40229 2013-03-05 18:03
浏览 60
已采纳

HTML5 <audio>标签的流容器

I have a Go application in which I would like to stream live uncompressed audio to the browser. I was hoping to stream over HTTP by just having the browser open a URL corresponding to the stream, and then feeding that connection with the audio data.

I was planning on using WAV to send the data uncompressed, However the WAV file format requires the file size be predefined in the header. Is there a better container format for doing this streaming that I can work with easily in Go?

I know that one approach would be to use a proper streaming server and transcode my audio through that, but if I'm implementing this myself, is there a fairly straightforward way to get off the ground? Perhaps a Go library to make this easier?

Thanks

EDIT I've solved this via ffmpeg as outlined in my answer below

  • 写回答

2条回答 默认 最新

  • doutui839638 2013-03-07 23:00
    关注

    Ok, here's what I ended up doing, my Go code pipes the raw PCM data through an ffmpeg process which converts it to mp3. I assume packets is a channel that my audio data is appearing over, and response is an http.ResponseWriter

    cmd := 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
    ", amount)
    }()
    err = cmd.Wait()
    if err != nil {
        log.Println("ffmpeg command terminated incorrectly", err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”