Created
July 18, 2018 09:28
-
-
Save coyove/27135f3304b617c5a6360b637762bbe7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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