Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

audio_convert.py 3.1KB

1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
1 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 mp3_to_wav(mp3_path, wav_path):
  60. """
  61. 把mp3格式转成pcm文件
  62. """
  63. audio = AudioSegment.from_mp3(mp3_path)
  64. audio.export(wav_path, format="wav")
  65. def pcm_to_sil(pcm_path, silk_path):
  66. """
  67. wav 文件转成 silk
  68. return 声音长度,毫秒
  69. """
  70. audio = AudioSegment.from_wav(pcm_path)
  71. rate = find_closest_sil_supports(audio.frame_rate)
  72. # Convert to PCM_s16
  73. pcm_s16 = audio.set_sample_width(2)
  74. pcm_s16 = pcm_s16.set_frame_rate(rate)
  75. wav_data = pcm_s16.raw_data
  76. silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate)
  77. with open(silk_path, "wb") as f:
  78. f.write(silk_data)
  79. return audio.duration_seconds * 1000
  80. def mp3_to_sil(mp3_path, silk_path):
  81. """
  82. mp3 文件转成 silk
  83. return 声音长度,毫秒
  84. """
  85. audio = AudioSegment.from_mp3(mp3_path)
  86. rate = find_closest_sil_supports(audio.frame_rate)
  87. # Convert to PCM_s16
  88. pcm_s16 = audio.set_sample_width(2)
  89. pcm_s16 = pcm_s16.set_frame_rate(rate)
  90. wav_data = pcm_s16.raw_data
  91. silk_data = pysilk.encode(wav_data, data_rate=rate, sample_rate=rate)
  92. # Save the silk file
  93. with open(silk_path, "wb") as f:
  94. f.write(silk_data)
  95. return audio.duration_seconds * 1000
  96. def sil_to_wav(silk_path, wav_path, rate: int = 24000):
  97. """
  98. silk 文件转 wav
  99. """
  100. wav_data = pysilk.decode_file(silk_path, to_wav=True, sample_rate=rate)
  101. with open(wav_path, "wb") as f:
  102. f.write(wav_data)