Skip to content

Instantly share code, notes, and snippets.

@coyove
Created July 18, 2018 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coyove/27135f3304b617c5a6360b637762bbe7 to your computer and use it in GitHub Desktop.
Save coyove/27135f3304b617c5a6360b637762bbe7 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"encoding/binary"
"io"
"io/ioutil"
"log"
"os/exec"
)
func main() {
cmd := exec.Command("ffmpeg", "-i", "test.mp3",
"-f", "s16le", "-ar", "48000", "-ac", "2", "pipe:1")
stdout, _ := cmd.StdoutPipe()
wait := make(chan bool)
go func() {
cmd.Run()
wait <- true
}()
stdin := bufio.NewReaderSize(stdout, 16384)
encodePCM := func(in []byte) {
err := ioutil.WriteFile("1.data", in, 0777)
if err != nil {
panic(err)
}
cmd := exec.Command("opusenc", "--discard-comments", "--raw", "1.data", "1.opus")
cmd.Run()
buf, _ := ioutil.ReadFile("1.opus")
log.Println(len(buf))
}
for {
buf := make([]byte, 960*2*2)
err := binary.Read(stdin, binary.LittleEndian, &buf)
if err == io.EOF {
break
}
if err == io.ErrUnexpectedEOF {
encodePCM(buf)
break
}
if err != nil {
panic(err)
break
}
encodePCM(buf)
}
select {
case <-wait:
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment