silk.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package silk
  2. /*
  3. #cgo LDFLAGS: -L . -lSKP_SILK_SDK
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include "SKP_Silk_SDK_API.h"
  8. #include "Decoder.h"
  9. static void print_usage() {
  10. printf("********** Silk Decoder (Fixed Point) v %s ********************\n", SKP_Silk_SDK_get_version());
  11. }
  12. */
  13. import "C"
  14. import (
  15. "fmt"
  16. "os"
  17. "silk2audio/transcoder/ffmpeg"
  18. "strings"
  19. "unsafe"
  20. )
  21. func TransSilkToWav(inputPath string) (string, string) {
  22. var outputPath = strings.Replace(inputPath, ".silk", ".pcm", -1)
  23. var wavPath = strings.Replace(inputPath, ".silk", ".wav", -1)
  24. inputPathC := C.CString(inputPath)
  25. outPathC := C.CString(outputPath)
  26. var _ = C.Decoder(inputPathC, outPathC) //调用C函数
  27. C.free(unsafe.Pointer(inputPathC))
  28. C.free(unsafe.Pointer(outPathC))
  29. //ffmpeg pcm转码音频
  30. transPcmToAudio(outputPath, wavPath)
  31. //_ = FileRemove(outputPath)
  32. return wavPath, outputPath
  33. }
  34. func transPcmToAudio(inputPath, OutputPath string) {
  35. format := "s16le"
  36. overwrite := true
  37. audioCodec := "pcm_s16le"
  38. audioChannels := 2
  39. audioRate := 12000
  40. opts := ffmpeg.Options{
  41. Overwrite: &overwrite,
  42. OutputFormat: &format,
  43. AudioChannels: &audioChannels,
  44. AudioRate: &audioRate,
  45. AudioCodec: &audioCodec,
  46. }
  47. ffmpegConf := &ffmpeg.Config{
  48. FfmpegBinPath: "ffmpeg",
  49. }
  50. _, err := ffmpeg.
  51. New(ffmpegConf).
  52. Input(inputPath).
  53. Output(OutputPath).
  54. WithOptions(opts).
  55. Start(opts)
  56. if err != nil {
  57. fmt.Println(err.Error())
  58. } else {
  59. fmt.Println(OutputPath)
  60. }
  61. }
  62. func FileRemove(logFile string) error {
  63. _, err := os.Stat(logFile)
  64. if err == nil {
  65. return os.Remove(logFile)
  66. }
  67. return nil
  68. }