1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-10 09:02:49 +00:00

fix(audio clipping): save separated audio streams

This commit is contained in:
2018-10-07 20:09:03 -05:00
parent 4c287a55fc
commit ff0945b881

View File

@@ -356,37 +356,38 @@ func writePacketsToFile(username string, packets chan *discordgo.Packet) {
os.Mkdir(config.Config.ClipsPath, os.ModePerm) os.Mkdir(config.Config.ClipsPath, os.ModePerm)
} }
// construct filename
timestamp := time.Now().UTC().Format("2006-01-02") + "-" + strconv.Itoa(int(time.Now().Unix()))
filename := config.Config.ClipsPath + "/" + timestamp + "-" + username + ".wav"
// grab everything from the voice packet channel and dump it to the file // grab everything from the voice packet channel and dump it to the file
// close when there is nothing left // close when there is nothing left
pcmOut := make([]int16, 0) // split audio into specific voice streams
pcmOut := map[uint32][]int16{}
loop: loop:
for { for {
select { select {
case p := <-packets: case p := <-packets:
for _, pcm := range p.PCM { pcmOut[p.SSRC] = append(pcmOut[p.SSRC], p.PCM...)
pcmOut = append(pcmOut, pcm)
}
default: default:
break loop break loop
} }
} }
cmd := exec.Command("ffmpeg", "-f", "s16le", "-ar", strconv.Itoa(sampleRate), "-ac", strconv.Itoa(channels), "-i", "pipe:0", filename) for key, pcmData := range pcmOut {
// construct filename
timestamp := time.Now().UTC().Format("2006-01-02") + "-" + strconv.Itoa(int(time.Now().Unix()))
filename := config.Config.ClipsPath + "/" + timestamp + "-" + strconv.Itoa(int(key)) + "-" + username + ".wav"
output := new(bytes.Buffer) cmd := exec.Command("ffmpeg", "-f", "s16le", "-ar", strconv.Itoa(sampleRate), "-ac", strconv.Itoa(channels), "-i", "pipe:0", filename)
binary.Write(output, binary.LittleEndian, pcmOut) output := new(bytes.Buffer)
cmd.Stdin = bytes.NewReader(output.Bytes())
err := cmd.Run() binary.Write(output, binary.LittleEndian, pcmData)
cmd.Stdin = bytes.NewReader(output.Bytes())
if err != nil { err := cmd.Run()
log.Error(err)
if err != nil {
log.Error(err)
}
} }
} }