Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

44 linhas
1.5KB

  1. import os
  2. import av
  3. import pilk
  4. def silk_to_pcm(silk_path: str, pcm_path: str) -> int:
  5. """将silk文件转为pcm文件"""
  6. pcm_rate = pilk.decode(silk_path, pcm_path)
  7. return pcm_rate
  8. def to_pcm(in_path: str) -> tuple[str, int]:
  9. """任意媒体文件转 pcm"""
  10. if in_path.endswith('.silk'):
  11. pcm_path = os.path.splitext(in_path)[0] + '.pcm'
  12. sample_rate = silk_to_pcm(in_path, pcm_path)
  13. else:
  14. out_path = os.path.splitext(in_path)[0] + '.pcm'
  15. with av.open(in_path) as in_container:
  16. in_stream = in_container.streams.audio[0]
  17. sample_rate = in_stream.codec_context.sample_rate
  18. with av.open(out_path, 'w', 's16le') as out_container:
  19. out_stream = out_container.add_stream(
  20. 'pcm_s16le',
  21. rate=sample_rate,
  22. layout='mono'
  23. )
  24. try:
  25. for frame in in_container.decode(in_stream):
  26. frame.pts = None
  27. for packet in out_stream.encode(frame):
  28. out_container.mux(packet)
  29. except:
  30. pass
  31. pcm_path = out_path
  32. return pcm_path, sample_rate
  33. def convert_to_silk(media_path: str) -> str:
  34. """任意媒体文件转 silk, 返回silk路径"""
  35. pcm_path, sample_rate = to_pcm(media_path)
  36. silk_path = os.path.splitext(pcm_path)[0] + '.silk'
  37. pilk.encode(pcm_path, silk_path, pcm_rate=sample_rate, tencent=True)
  38. os.remove(pcm_path)
  39. return silk_path