如何在Golang中使用字节保存Opus文件?

We are using pion/webrtc library and we have incoming audio track from RTCPeerConnection

We need to save opus audio file from RTCPacket.Raw (list of bytes). We tried to use their OpusWriter but it didn't work, audio is written but not playable.

So the question is how can we save Opus audio file from bytes?

The code:

        for {
          rtpPacket, err := remoteTrack.ReadRTP()
          b := rtpPacket.Raw

          if ivfFile != nil {
            if err := ivfFile.WriteRTP(rtpPacket); err != nil {
              log.Println(err)
            }
          }

          checkError(err)
          videoTrackLock.RLock()
          _, err = videoTrack.Write(b)
          videoTrackLock.RUnlock()

          if err != io.ErrClosedPipe {
            checkError(err)
          }
        }

Does the save-to-disk example not work for you? That demonstrates saving VP8/Opus to disk.

If you do want the audio directly from *RTP.Packet you should use the Payload field not Raw. Raw contains the RTP headers, while Payload will only contain the media.

Thanks for using Pion! I am happy to help either here or on Slack