slice_audio.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os,sys,numpy as np
  2. import traceback
  3. from scipy.io import wavfile
  4. # parent_directory = os.path.dirname(os.path.abspath(__file__))
  5. # sys.path.append(parent_directory)
  6. from my_utils import load_audio
  7. from slicer2 import Slicer
  8. def slice(inp,opt_root,threshold,min_length,min_interval,hop_size,max_sil_kept,_max,alpha,i_part,all_part):
  9. os.makedirs(opt_root,exist_ok=True)
  10. if os.path.isfile(inp):
  11. input=[inp]
  12. elif os.path.isdir(inp):
  13. input=[os.path.join(inp, name) for name in sorted(list(os.listdir(inp)))]
  14. else:
  15. return "输入路径存在但既不是文件也不是文件夹"
  16. slicer = Slicer(
  17. sr=32000, # 长音频采样率
  18. threshold= int(threshold), # 音量小于这个值视作静音的备选切割点
  19. min_length= int(min_length), # 每段最小多长,如果第一段太短一直和后面段连起来直到超过这个值
  20. min_interval= int(min_interval), # 最短切割间隔
  21. hop_size= int(hop_size), # 怎么算音量曲线,越小精度越大计算量越高(不是精度越大效果越好)
  22. max_sil_kept= int(max_sil_kept), # 切完后静音最多留多长
  23. )
  24. _max=float(_max)
  25. alpha=float(alpha)
  26. for inp_path in input[int(i_part)::int(all_part)]:
  27. # print(inp_path)
  28. try:
  29. name = os.path.basename(inp_path)
  30. audio = load_audio(inp_path, 32000)
  31. # print(audio.shape)
  32. for chunk, start, end in slicer.slice(audio): # start和end是帧数
  33. tmp_max = np.abs(chunk).max()
  34. if(tmp_max>1):chunk/=tmp_max
  35. chunk = (chunk / tmp_max * (_max * alpha)) + (1 - alpha) * chunk
  36. wavfile.write(
  37. "%s/%s_%010d_%010d.wav" % (opt_root, name, start, end),
  38. 32000,
  39. # chunk.astype(np.float32),
  40. (chunk * 32767).astype(np.int16),
  41. )
  42. except:
  43. print(inp_path,"->fail->",traceback.format_exc())
  44. return "执行完毕,请检查输出文件"
  45. print(slice(*sys.argv[1:]))