Streaming container for HTML5 <audio> tag

I have a Go app in which I would like to stream live uncompressed sound to a browser. I was hoping to transmit streams via HTTP only if the browser opened the URL corresponding to the stream and then connected this connection to the audio data.

I planned to use WAV to send uncompressed data. However, for the WAV file format, the file size must be predefined in the header. Is there a better container format for this streaming that I can easily work with in Go?

I know that one approach would be to use a proper streaming server and transcode my sound through it, but if I implement this myself, is there a pretty simple way to get out of the ground? Perhaps a Go library to make this easier?

thank

EDIT I solved this via ffmpeg as described in my answer below

+5
source share
2 answers

Ok, here's what I did in the end, my Go code passes the original PCM data through the ffmpeg process, which converts it to mp3. I assume that packetsis the channel through which my audio data appears, and response- 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\n", amount)
}()
err = cmd.Wait()
if err != nil {
    log.Println("ffmpeg command terminated incorrectly", err)
}
0
source

If you are still thinking of uncompressed WAV for maximum quality, I think you have two options

  • Lie in the title - let's say that WAV is very long (say 2 GB)
  • Add extra pieces to the RIFF container

, , 2 . , , HTML5, , , .

, , -, RIFF, WAV - . WAV . WAV-, , WAV , 44- , RIFF!

, , .

+2
source

All Articles