選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

61 行
1.5KB

  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 pcm_to_silk(pcm_path, silk_path):
  19. """
  20. wav 文件转成 silk
  21. return 声音长度,毫秒
  22. """
  23. audio = AudioSegment.from_wav(pcm_path)
  24. wav_data = audio.raw_data
  25. silk_data = pysilk.encode(
  26. wav_data, data_rate=audio.frame_rate, sample_rate=audio.frame_rate)
  27. with open(silk_path, "wb") as f:
  28. f.write(silk_data)
  29. return audio.duration_seconds * 1000
  30. def mp3_to_sil(mp3_path, silk_path):
  31. """
  32. mp3 文件转成 silk
  33. return 声音长度,毫秒
  34. """
  35. audio = AudioSegment.from_mp3(mp3_path)
  36. wav_data = audio.raw_data
  37. silk_data = pysilk.encode(
  38. wav_data, data_rate=audio.frame_rate, sample_rate=audio.frame_rate)
  39. # Save the silk file
  40. with open(silk_path, "wb") as f:
  41. f.write(silk_data)
  42. return audio.duration_seconds * 1000
  43. def sil_to_wav(silk_path, wav_path, rate: int = 24000):
  44. """
  45. silk 文件转 wav
  46. """
  47. wav_data = pysilk.decode_file(silk_path, to_wav=True, sample_rate=rate)
  48. with open(wav_path, "wb") as f:
  49. f.write(wav_data)