package silk /* #cgo LDFLAGS: -L . -lSKP_SILK_SDK #include #include #include #include "SKP_Silk_SDK_API.h" #include "Decoder.h" static void print_usage() { printf("********** Silk Decoder (Fixed Point) v %s ********************\n", SKP_Silk_SDK_get_version()); } */ import "C" import ( "fmt" "os" "os/exec" "silk2audio/transcoder/ffmpeg" "strings" "unsafe" ) func TransSilkToWav(inputPath string) (string, string) { var outputPath = strings.Replace(inputPath, ".silk", ".pcm", -1) var wavPath = strings.Replace(inputPath, ".silk", ".wav", -1) inputPathC := C.CString(inputPath) outPathC := C.CString(outputPath) var _ = C.Decoder(inputPathC, outPathC) //调用C函数 C.free(unsafe.Pointer(inputPathC)) C.free(unsafe.Pointer(outPathC)) //ffmpeg pcm转码音频 transPcmToAudio(outputPath, wavPath) //_ = FileRemove(outputPath) return wavPath, outputPath } func TransMp3ToWav(inputPath string) (string, string) { var outputPath = strings.Replace(inputPath, ".mp3", ".pcm", -1) var wavPath = strings.Replace(inputPath, ".mp3", ".wav", -1) //将mp3转成pcm transMp3ToAudio(inputPath, outputPath) //ffmpeg pcm转码音频 transPcmToAudio(outputPath, wavPath) //_ = FileRemove(outputPath) return wavPath, outputPath } func transPcmToAudio(inputPath, OutputPath string) { format := "s16le" overwrite := true audioCodec := "pcm_s16le" audioChannels := 2 audioRate := 12000 opts := ffmpeg.Options{ Overwrite: &overwrite, OutputFormat: &format, AudioChannels: &audioChannels, AudioRate: &audioRate, AudioCodec: &audioCodec, } ffmpegConf := &ffmpeg.Config{ FfmpegBinPath: "ffmpeg", } _, err := ffmpeg. New(ffmpegConf). Input(inputPath). Output(OutputPath). WithOptions(opts). Start(opts) if err != nil { fmt.Println(err.Error()) } else { fmt.Println(OutputPath) } } func transMp3ToAudio(inputPath, OutputPath string) { cmdArgs := []string{"-i", inputPath, "-f", "s16le", "-acodec", "pcm_s16le", "-ar", "12000", "-ac", "2", OutputPath} err := exec.Command("ffmpeg", cmdArgs...).Run() if err != nil { fmt.Println(err.Error()) } else { fmt.Println(OutputPath) } } func FileRemove(logFile string) error { _, err := os.Stat(logFile) if err == nil { return os.Remove(logFile) } return nil }