| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package silk
- /*
- #cgo LDFLAGS: -L . -lSKP_SILK_SDK
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #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
- }
|