Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

105 lignes
3.1KB

  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 any_path.endswith('.sil') or any_path.endswith('.silk') or any_path.endswith('.slk'):
  36. return sil_to_wav(any_path, wav_path)
  37. audio = AudioSegment.from_file(any_path)
  38. audio.export(wav_path, format="wav")
  39. def any_to_sil(any_path, sil_path):
  40. """
  41. 把任意格式转成sil文件
  42. """
  43. if any_path.endswith('.sil') or any_path.endswith('.silk') or any_path.endswith('.slk'):
  44. shutil.copy2(any_path, sil_path)
  45. return 10000
  46. if any_path.endswith('.wav'):
  47. return pcm_to_sil(any_path, sil_path)
  48. if any_path.endswith('.mp3'):
  49. return mp3_to_sil(any_path, sil_path)
  50. raise NotImplementedError("Not support file type: {}".format(any_path))
  51. def mp3_to_wav(mp3_path, wav_path):
  52. """
  53. 把mp3格式转成pcm文件
  54. """
  55. audio = AudioSegment.from_mp3(mp3_path)
  56. audio.export(wav_path, format="wav")
  57. def pcm_to_sil(pcm_path, silk_path):
  58. """
  59. wav 文件转成 silk
  60. return 声音长度,毫秒
  61. """
  62. audio = AudioSegment.from_wav(pcm_path)
  63. rate = find_closest_sil_supports(audio.frame_rate)
  64. # Convert to PCM_s16
  65. pcm_s16 = audio.set_sample_width(2)
  66. pcm_s16 = pcm_s16.set_frame_rate(rate)
  67. wav_data = pcm_s16.raw_data
  68. silk_data = pysilk.encode(
  69. wav_data, data_rate=rate, sample_rate=rate)
  70. with open(silk_path, "wb") as f:
  71. f.write(silk_data)
  72. return audio.duration_seconds * 1000
  73. def mp3_to_sil(mp3_path, silk_path):
  74. """
  75. mp3 文件转成 silk
  76. return 声音长度,毫秒
  77. """
  78. audio = AudioSegment.from_mp3(mp3_path)
  79. rate = find_closest_sil_supports(audio.frame_rate)
  80. # Convert to PCM_s16
  81. pcm_s16 = audio.set_sample_width(2)
  82. pcm_s16 = pcm_s16.set_frame_rate(rate)
  83. wav_data = pcm_s16.raw_data
  84. silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate)
  85. # Save the silk file
  86. with open(silk_path, "wb") as f:
  87. f.write(silk_data)
  88. return audio.duration_seconds * 1000
  89. def sil_to_wav(silk_path, wav_path, rate: int = 24000):
  90. """
  91. silk 文件转 wav
  92. """
  93. wav_data = pysilk.decode_file(silk_path, to_wav=True, sample_rate=rate)
  94. with open(wav_path, "wb") as f:
  95. f.write(wav_data)