You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

audio_convert.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import wave
  2. import pysilk
  3. from pydub import AudioSegment
  4. def get_pcm_from_wav(wav_path):
  5. """
  6. 从 wav 文件中读取 pcm
  7. :param wav_path: wav 文件路径
  8. :returns: pcm 数据
  9. """
  10. wav = wave.open(wav_path, "rb")
  11. return wav.readframes(wav.getnframes())
  12. def mp3_to_wav(mp3_path, wav_path):
  13. """
  14. 把mp3格式转成pcm文件
  15. """
  16. audio = AudioSegment.from_mp3(mp3_path)
  17. audio.export(wav_path, format="wav")
  18. def any_to_wav(any_path, wav_path):
  19. """
  20. 把任意格式转成wav文件
  21. """
  22. if any_path.endswith('.wav'):
  23. return
  24. if any_path.endswith('.sil') or any_path.endswith('.silk') or any_path.endswith('.slk'):
  25. return sil_to_wav(any_path, wav_path)
  26. audio = AudioSegment.from_file(any_path)
  27. audio.export(wav_path, format="wav")
  28. def pcm_to_silk(pcm_path, silk_path):
  29. """
  30. wav 文件转成 silk
  31. return 声音长度,毫秒
  32. """
  33. audio = AudioSegment.from_wav(pcm_path)
  34. wav_data = audio.raw_data
  35. silk_data = pysilk.encode(
  36. wav_data, data_rate=audio.frame_rate, sample_rate=audio.frame_rate)
  37. with open(silk_path, "wb") as f:
  38. f.write(silk_data)
  39. return audio.duration_seconds * 1000
  40. def mp3_to_sil(mp3_path, silk_path):
  41. """
  42. mp3 文件转成 silk
  43. return 声音长度,毫秒
  44. """
  45. audio = AudioSegment.from_mp3(mp3_path)
  46. wav_data = audio.raw_data
  47. silk_data = pysilk.encode(
  48. wav_data, data_rate=audio.frame_rate, sample_rate=audio.frame_rate)
  49. # Save the silk file
  50. with open(silk_path, "wb") as f:
  51. f.write(silk_data)
  52. return audio.duration_seconds * 1000
  53. def sil_to_wav(silk_path, wav_path, rate: int = 24000):
  54. """
  55. silk 文件转 wav
  56. """
  57. wav_data = pysilk.decode_file(silk_path, to_wav=True, sample_rate=rate)
  58. with open(wav_path, "wb") as f:
  59. f.write(wav_data)