silk.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "os/exec"
  18. "silk2audio/transcoder/ffmpeg"
  19. "strings"
  20. "unsafe"
  21. )
  22. func TransSilkToWav(inputPath string) (string, string) {
  23. var outputPath = strings.Replace(inputPath, ".silk", ".pcm", -1)
  24. var wavPath = strings.Replace(inputPath, ".silk", ".wav", -1)
  25. inputPathC := C.CString(inputPath)
  26. outPathC := C.CString(outputPath)
  27. var _ = C.Decoder(inputPathC, outPathC) //调用C函数
  28. C.free(unsafe.Pointer(inputPathC))
  29. C.free(unsafe.Pointer(outPathC))
  30. //ffmpeg pcm转码音频
  31. transPcmToAudio(outputPath, wavPath)
  32. //_ = FileRemove(outputPath)
  33. return wavPath, outputPath
  34. }
  35. func TransMp3ToWav(inputPath string) (string, string) {
  36. var outputPath = strings.Replace(inputPath, ".mp3", ".pcm", -1)
  37. var wavPath = strings.Replace(inputPath, ".mp3", ".wav", -1)
  38. //将mp3转成pcm
  39. transMp3ToAudio(inputPath, outputPath)
  40. //ffmpeg pcm转码音频
  41. transPcmToAudio(outputPath, wavPath)
  42. //_ = FileRemove(outputPath)
  43. return wavPath, outputPath
  44. }
  45. func transPcmToAudio(inputPath, OutputPath string) {
  46. format := "s16le"
  47. overwrite := true
  48. audioCodec := "pcm_s16le"
  49. audioChannels := 2
  50. audioRate := 12000
  51. opts := ffmpeg.Options{
  52. Overwrite: &overwrite,
  53. OutputFormat: &format,
  54. AudioChannels: &audioChannels,
  55. AudioRate: &audioRate,
  56. AudioCodec: &audioCodec,
  57. }
  58. ffmpegConf := &ffmpeg.Config{
  59. FfmpegBinPath: "ffmpeg",
  60. }
  61. _, err := ffmpeg.
  62. New(ffmpegConf).
  63. Input(inputPath).
  64. Output(OutputPath).
  65. WithOptions(opts).
  66. Start(opts)
  67. if err != nil {
  68. fmt.Println(err.Error())
  69. } else {
  70. fmt.Println(OutputPath)
  71. }
  72. }
  73. func transMp3ToAudio(inputPath, OutputPath string) {
  74. cmdArgs := []string{"-i", inputPath, "-f", "s16le", "-acodec", "pcm_s16le", "-ar", "12000", "-ac", "2", OutputPath}
  75. err := exec.Command("ffmpeg", cmdArgs...).Run()
  76. if err != nil {
  77. fmt.Println(err.Error())
  78. } else {
  79. fmt.Println(OutputPath)
  80. }
  81. }
  82. func FileRemove(logFile string) error {
  83. _, err := os.Stat(logFile)
  84. if err == nil {
  85. return os.Remove(logFile)
  86. }
  87. return nil
  88. }