Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

140 lines
3.7KB

  1. import shutil
  2. import wave
  3. import pysilk
  4. from pydub import AudioSegment
  5. sil_supports = [8000, 12000, 16000, 24000, 32000, 44100, 48000] # slk转wav时,支持的采样率
  6. def find_closest_sil_supports(sample_rate):
  7. """
  8. 找到最接近的支持的采样率
  9. """
  10. if sample_rate in sil_supports:
  11. return sample_rate
  12. closest = 0
  13. mindiff = 9999999
  14. for rate in sil_supports:
  15. diff = abs(rate - sample_rate)
  16. if diff < mindiff:
  17. closest = rate
  18. mindiff = diff
  19. return closest
  20. def get_pcm_from_wav(wav_path):
  21. """
  22. 从 wav 文件中读取 pcm
  23. :param wav_path: wav 文件路径
  24. :returns: pcm 数据
  25. """
  26. wav = wave.open(wav_path, "rb")
  27. return wav.readframes(wav.getnframes())
  28. def any_to_wav(any_path, wav_path):
  29. """
  30. 把任意格式转成wav文件
  31. """
  32. if any_path.endswith(".wav"):
  33. shutil.copy2(any_path, wav_path)
  34. return
  35. if (
  36. any_path.endswith(".sil")
  37. or any_path.endswith(".silk")
  38. or any_path.endswith(".slk")
  39. ):
  40. return sil_to_wav(any_path, wav_path)
  41. audio = AudioSegment.from_file(any_path)
  42. audio.export(wav_path, format="wav")
  43. def any_to_sil(any_path, sil_path):
  44. """
  45. 把任意格式转成sil文件
  46. """
  47. if (
  48. any_path.endswith(".sil")
  49. or any_path.endswith(".silk")
  50. or any_path.endswith(".slk")
  51. ):
  52. shutil.copy2(any_path, sil_path)
  53. return 10000
  54. if any_path.endswith(".wav"):
  55. return pcm_to_sil(any_path, sil_path)
  56. if any_path.endswith(".mp3"):
  57. return mp3_to_sil(any_path, sil_path)
  58. raise NotImplementedError("Not support file type: {}".format(any_path))
  59. def any_to_amr(any_path, amr_path):
  60. """
  61. 把任意格式转成amr文件
  62. """
  63. if any_path.endswith(".amr"):
  64. shutil.copy2(any_path, amr_path)
  65. return
  66. if (
  67. any_path.endswith(".sil")
  68. or any_path.endswith(".silk")
  69. or any_path.endswith(".slk")
  70. ):
  71. raise NotImplementedError("Not support file type: {}".format(any_path))
  72. audio = AudioSegment.from_file(any_path)
  73. audio = audio.set_frame_rate(8000) # only support 8000
  74. audio.export(amr_path, format="amr")
  75. def mp3_to_wav(mp3_path, wav_path):
  76. """
  77. 把mp3格式转成pcm文件
  78. """
  79. audio = AudioSegment.from_mp3(mp3_path)
  80. audio.export(wav_path, format="wav")
  81. def pcm_to_sil(pcm_path, silk_path):
  82. """
  83. wav 文件转成 silk
  84. return 声音长度,毫秒
  85. """
  86. audio = AudioSegment.from_wav(pcm_path)
  87. rate = find_closest_sil_supports(audio.frame_rate)
  88. # Convert to PCM_s16
  89. pcm_s16 = audio.set_sample_width(2)
  90. pcm_s16 = pcm_s16.set_frame_rate(rate)
  91. wav_data = pcm_s16.raw_data
  92. silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate)
  93. with open(silk_path, "wb") as f:
  94. f.write(silk_data)
  95. return audio.duration_seconds * 1000
  96. def mp3_to_sil(mp3_path, silk_path):
  97. """
  98. mp3 文件转成 silk
  99. return 声音长度,毫秒
  100. """
  101. audio = AudioSegment.from_mp3(mp3_path)
  102. rate = find_closest_sil_supports(audio.frame_rate)
  103. # Convert to PCM_s16
  104. pcm_s16 = audio.set_sample_width(2)
  105. pcm_s16 = pcm_s16.set_frame_rate(rate)
  106. wav_data = pcm_s16.raw_data
  107. silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate)
  108. # Save the silk file
  109. with open(silk_path, "wb") as f:
  110. f.write(silk_data)
  111. return audio.duration_seconds * 1000
  112. def sil_to_wav(silk_path, wav_path, rate: int = 24000):
  113. """
  114. silk 文件转 wav
  115. """
  116. wav_data = pysilk.decode_file(silk_path, to_wav=True, sample_rate=rate)
  117. with open(wav_path, "wb") as f:
  118. f.write(wav_data)