• 对音频切分成小音频(机器学习用)


    我是把so-vits中小工具,分析源码然后提取出来了。以后可以写在自己的程序里。

    -------流程(这是我做的流程,你可以不用看)

    从开源代码中快速获取自己需要的东西

    如果有界面f12看他里面的接口,然后在源码中全局搜索,没有接口比如socket,看他的消息字段,然后推测。然后提取补齐代码就行了

    -------

    你需要看的

    提取出来有3个类

    run.py是我自己写的

    其他是我提取的源码,首先你得install一些包

    numpy,librosa,soundfile

    slicer2.py

    1. import numpy as np
    2. # This function is obtained from librosa.
    3. def get_rms(
    4. y,
    5. *,
    6. frame_length=2048,
    7. hop_length=512,
    8. pad_mode="constant",
    9. ):
    10. padding = (int(frame_length // 2), int(frame_length // 2))
    11. y = np.pad(y, padding, mode=pad_mode)
    12. axis = -1
    13. # put our new within-frame axis at the end for now
    14. out_strides = y.strides + tuple([y.strides[axis]])
    15. # Reduce the shape on the framing axis
    16. x_shape_trimmed = list(y.shape)
    17. x_shape_trimmed[axis] -= frame_length - 1
    18. out_shape = tuple(x_shape_trimmed) + tuple([frame_length])
    19. xw = np.lib.stride_tricks.as_strided(
    20. y, shape=out_shape, strides=out_strides
    21. )
    22. if axis < 0:
    23. target_axis = axis - 1
    24. else:
    25. target_axis = axis + 1
    26. xw = np.moveaxis(xw, -1, target_axis)
    27. # Downsample along the target axis
    28. slices = [slice(None)] * xw.ndim
    29. slices[axis] = slice(0, None, hop_length)
    30. x = xw[tuple(slices)]
    31. # Calculate power
    32. power = np.mean(np.abs(x) ** 2, axis=-2, keepdims=True)
    33. return np.sqrt(power)
    34. class Slicer:
    35. def __init__(self,
    36. sr: int,
    37. threshold: float = -40.,
    38. min_length: int = 5000,
    39. min_interval: int = 300,
    40. hop_size: int = 20,
    41. max_sil_kept: int = 5000):
    42. if not min_length >= min_interval >= hop_size:
    43. raise ValueError('The following condition must be satisfied: min_length >= min_interval >= hop_size')
    44. if not max_sil_kept >= hop_size:
    45. raise ValueError('The following condition must be satisfied: max_sil_kept >= hop_size')
    46. min_interval = sr * min_interval / 1000
    47. self.threshold = 10 ** (threshold / 20.)
    48. self.hop_size = round(sr * hop_size / 1000)
    49. self.win_size = min(round(min_interval), 4 * self.hop_size)
    50. self.min_length = round(sr * min_length / 1000 / self.hop_size)
    51. self.min_interval = round(min_interval / self.hop_size)
    52. self.max_sil_kept = round(sr * max_sil_kept / 1000 / self.hop_size)
    53. def _apply_slice(self, waveform, begin, end):
    54. if len(waveform.shape) > 1:
    55. return waveform[:, begin * self.hop_size: min(waveform.shape[1], end * self.hop_size)]
    56. else:
    57. return waveform[begin * self.hop_size: min(waveform.shape[0], end * self.hop_size)]
    58. # @timeit
    59. def slice(self, waveform):
    60. if len(waveform.shape) > 1:
    61. samples = waveform.mean(axis=0)
    62. else:
    63. samples = waveform
    64. if samples.shape[0] <= self.min_length:
    65. return [waveform]
    66. rms_list = get_rms(y=samples, frame_length=self.win_size, hop_length=self.hop_size).squeeze(0)
    67. sil_tags = []
    68. silence_start = None
    69. clip_start = 0
    70. for i, rms in enumerate(rms_list):
    71. # Keep looping while frame is silent.
    72. if rms < self.threshold:
    73. # Record start of silent frames.
    74. if silence_start is None:
    75. silence_start = i
    76. continue
    77. # Keep looping while frame is not silent and silence start has not been recorded.
    78. if silence_start is None:
    79. continue
    80. # Clear recorded silence start if interval is not enough or clip is too short
    81. is_leading_silence = silence_start == 0 and i > self.max_sil_kept
    82. need_slice_middle = i - silence_start >= self.min_interval and i - clip_start >= self.min_length
    83. if not is_leading_silence and not need_slice_middle:
    84. silence_start = None
    85. continue
    86. # Need slicing. Record the range of silent frames to be removed.
    87. if i - silence_start <= self.max_sil_kept:
    88. pos = rms_list[silence_start: i + 1].argmin() + silence_start
    89. if silence_start == 0:
    90. sil_tags.append((0, pos))
    91. else:
    92. sil_tags.append((pos, pos))
    93. clip_start = pos
    94. elif i - silence_start <= self.max_sil_kept * 2:
    95. pos = rms_list[i - self.max_sil_kept: silence_start + self.max_sil_kept + 1].argmin()
    96. pos += i - self.max_sil_kept
    97. pos_l = rms_list[silence_start: silence_start + self.max_sil_kept + 1].argmin() + silence_start
    98. pos_r = rms_list[i - self.max_sil_kept: i + 1].argmin() + i - self.max_sil_kept
    99. if silence_start == 0:
    100. sil_tags.append((0, pos_r))
    101. clip_start = pos_r
    102. else:
    103. sil_tags.append((min(pos_l, pos), max(pos_r, pos)))
    104. clip_start = max(pos_r, pos)
    105. else:
    106. pos_l = rms_list[silence_start: silence_start + self.max_sil_kept + 1].argmin() + silence_start
    107. pos_r = rms_list[i - self.max_sil_kept: i + 1].argmin() + i - self.max_sil_kept
    108. if silence_start == 0:
    109. sil_tags.append((0, pos_r))
    110. else:
    111. sil_tags.append((pos_l, pos_r))
    112. clip_start = pos_r
    113. silence_start = None
    114. # Deal with trailing silence.
    115. total_frames = rms_list.shape[0]
    116. if silence_start is not None and total_frames - silence_start >= self.min_interval:
    117. silence_end = min(total_frames, silence_start + self.max_sil_kept)
    118. pos = rms_list[silence_start: silence_end + 1].argmin() + silence_start
    119. sil_tags.append((pos, total_frames + 1))
    120. # Apply and return slices.
    121. if len(sil_tags) == 0:
    122. return [waveform]
    123. else:
    124. chunks = []
    125. if sil_tags[0][0] > 0:
    126. chunks.append(self._apply_slice(waveform, 0, sil_tags[0][0]))
    127. for i in range(len(sil_tags) - 1):
    128. chunks.append(self._apply_slice(waveform, sil_tags[i][1], sil_tags[i + 1][0]))
    129. if sil_tags[-1][1] < total_frames:
    130. chunks.append(self._apply_slice(waveform, sil_tags[-1][1], total_frames))
    131. return chunks
    132. def main():
    133. import os.path
    134. from argparse import ArgumentParser
    135. import librosa
    136. import soundfile
    137. parser = ArgumentParser()
    138. parser.add_argument('audio', type=str, help='The audio to be sliced')
    139. parser.add_argument('--out', type=str, help='Output directory of the sliced audio clips')
    140. parser.add_argument('--db_thresh', type=float, required=False, default=-40,
    141. help='The dB threshold for silence detection')
    142. parser.add_argument('--min_length', type=int, required=False, default=5000,
    143. help='The minimum milliseconds required for each sliced audio clip')
    144. parser.add_argument('--min_interval', type=int, required=False, default=300,
    145. help='The minimum milliseconds for a silence part to be sliced')
    146. parser.add_argument('--hop_size', type=int, required=False, default=10,
    147. help='Frame length in milliseconds')
    148. parser.add_argument('--max_sil_kept', type=int, required=False, default=500,
    149. help='The maximum silence length kept around the sliced clip, presented in milliseconds')
    150. args = parser.parse_args()
    151. out = args.out
    152. if out is None:
    153. out = os.path.dirname(os.path.abspath(args.audio))
    154. audio, sr = librosa.load(args.audio, sr=None, mono=False)
    155. slicer = Slicer(
    156. sr=sr,
    157. threshold=args.db_thresh,
    158. min_length=args.min_length,
    159. min_interval=args.min_interval,
    160. hop_size=args.hop_size,
    161. max_sil_kept=args.max_sil_kept
    162. )
    163. chunks = slicer.slice(audio)
    164. if not os.path.exists(out):
    165. os.makedirs(out)
    166. for i, chunk in enumerate(chunks):
    167. if len(chunk.shape) > 1:
    168. chunk = chunk.T
    169. soundfile.write(os.path.join(out, f'%s_%d.wav' % (os.path.basename(args.audio).rsplit('.', maxsplit=1)[0], i)), chunk, sr)
    170. if __name__ == '__main__':
    171. main()

    auto_slicer.py

    1. import os
    2. import numpy as np
    3. import librosa
    4. import soundfile as sf
    5. from slicer2 import Slicer
    6. class AutoSlicer:
    7. def __init__(self):
    8. self.slicer_params = {
    9. "threshold": -40,
    10. "min_length": 5000,
    11. "min_interval": 300,
    12. "hop_size": 10,
    13. "max_sil_kept": 500,
    14. }
    15. self.original_min_interval = self.slicer_params["min_interval"]
    16. def auto_slice(self, filename, input_dir, output_dir, max_sec):
    17. audio, sr = librosa.load(os.path.join(input_dir, filename), sr=None, mono=False)
    18. slicer = Slicer(sr=sr, **self.slicer_params)
    19. chunks = slicer.slice(audio)
    20. files_to_delete = []
    21. for i, chunk in enumerate(chunks):
    22. if len(chunk.shape) > 1:
    23. chunk = chunk.T
    24. output_filename = f"{os.path.splitext(filename)[0]}_{i}"
    25. output_filename = "".join(c for c in output_filename if c.isascii() or c == "_") + ".wav"
    26. output_filepath = os.path.join(output_dir, output_filename)
    27. sf.write(output_filepath, chunk, sr)
    28. #Check and re-slice audio that more than max_sec.
    29. while True:
    30. new_audio, sr = librosa.load(output_filepath, sr=None, mono=False)
    31. if librosa.get_duration(y=new_audio, sr=sr) <= max_sec:
    32. break
    33. self.slicer_params["min_interval"] = self.slicer_params["min_interval"] // 2
    34. if self.slicer_params["min_interval"] >= self.slicer_params["hop_size"]:
    35. new_chunks = Slicer(sr=sr, **self.slicer_params).slice(new_audio)
    36. for j, new_chunk in enumerate(new_chunks):
    37. if len(new_chunk.shape) > 1:
    38. new_chunk = new_chunk.T
    39. new_output_filename = f"{os.path.splitext(output_filename)[0]}_{j}.wav"
    40. sf.write(os.path.join(output_dir, new_output_filename), new_chunk, sr)
    41. files_to_delete.append(output_filepath)
    42. else:
    43. break
    44. self.slicer_params["min_interval"] = self.original_min_interval
    45. for file_path in files_to_delete:
    46. if os.path.exists(file_path):
    47. os.remove(file_path)
    48. def merge_short(self, output_dir, max_sec, min_sec):
    49. short_files = []
    50. for filename in os.listdir(output_dir):
    51. filepath = os.path.join(output_dir, filename)
    52. if filename.endswith(".wav"):
    53. audio, sr = librosa.load(filepath, sr=None, mono=False)
    54. duration = librosa.get_duration(y=audio, sr=sr)
    55. if duration < min_sec:
    56. short_files.append((filepath, audio, duration))
    57. short_files.sort(key=lambda x: x[2], reverse=True)
    58. merged_audio = []
    59. current_duration = 0
    60. for filepath, audio, duration in short_files:
    61. if current_duration + duration <= max_sec:
    62. merged_audio.append(audio)
    63. current_duration += duration
    64. os.remove(filepath)
    65. else:
    66. if merged_audio:
    67. output_audio = np.concatenate(merged_audio, axis=-1)
    68. if len(output_audio.shape) > 1:
    69. output_audio = output_audio.T
    70. output_filename = f"merged_{len(os.listdir(output_dir))}.wav"
    71. sf.write(os.path.join(output_dir, output_filename), output_audio, sr)
    72. merged_audio = [audio]
    73. current_duration = duration
    74. os.remove(filepath)
    75. if merged_audio and current_duration >= min_sec:
    76. output_audio = np.concatenate(merged_audio, axis=-1)
    77. if len(output_audio.shape) > 1:
    78. output_audio = output_audio.T
    79. output_filename = f"merged_{len(os.listdir(output_dir))}.wav"
    80. sf.write(os.path.join(output_dir, output_filename), output_audio, sr)
    81. def slice_count(self, input_dir, output_dir):
    82. orig_duration = final_duration = 0
    83. for file in os.listdir(input_dir):
    84. if file.endswith(".wav"):
    85. _audio, _sr = librosa.load(os.path.join(input_dir, file), sr=None, mono=False)
    86. orig_duration += librosa.get_duration(y=_audio, sr=_sr)
    87. wav_files = [file for file in os.listdir(output_dir) if file.endswith(".wav")]
    88. num_files = len(wav_files)
    89. max_duration = -1
    90. min_duration = float("inf")
    91. for file in wav_files:
    92. file_path = os.path.join(output_dir, file)
    93. audio, sr = librosa.load(file_path, sr=None, mono=False)
    94. duration = librosa.get_duration(y=audio, sr=sr)
    95. final_duration += float(duration)
    96. if duration > max_duration:
    97. max_duration = float(duration)
    98. if duration < min_duration:
    99. min_duration = float(duration)
    100. return num_files, max_duration, min_duration, orig_duration, final_duration

    run.py

    1. import os
    2. from auto_slicer import AutoSlicer
    3. import librosa
    4. def slicer_fn(input_dir, output_dir, process_method, max_sec, min_sec):
    5. if output_dir == "":
    6. return "请先选择输出的文件夹"
    7. if output_dir == input_dir:
    8. return "输出目录不能和输入目录相同"
    9. slicer = AutoSlicer()
    10. if os.path.exists(output_dir) is not True:
    11. os.makedirs(output_dir)
    12. for filename in os.listdir(input_dir):
    13. if filename.lower().endswith(".wav"):
    14. slicer.auto_slice(filename, input_dir, output_dir, max_sec)
    15. if process_method == "丢弃":
    16. for filename in os.listdir(output_dir):
    17. if filename.endswith(".wav"):
    18. filepath = os.path.join(output_dir, filename)
    19. audio, sr = librosa.load(filepath, sr=None, mono=False)
    20. if librosa.get_duration(y=audio, sr=sr) < min_sec:
    21. os.remove(filepath)
    22. elif process_method == "将过短音频整合为长音频":
    23. slicer.merge_short(output_dir, max_sec, min_sec)
    24. file_count, max_duration, min_duration, orig_duration, final_duration = slicer.slice_count(input_dir, output_dir)
    25. hrs = int(final_duration / 3600)
    26. mins = int((final_duration % 3600) / 60)
    27. sec = format(float(final_duration % 60), '.2f')
    28. rate = format(100 * (final_duration / orig_duration), '.2f') if orig_duration != 0 else 0
    29. rate_msg = f"为原始音频时长的{rate}%" if rate != 0 else "因未知问题,无法计算切片时长的占比"
    30. return f"成功将音频切分为{file_count}条片段,其中最长{max_duration}秒,最短{min_duration}秒,切片后的音频总时长{hrs:02d}小时{mins:02d}{sec}秒,{rate_msg}"
    31. input_dir="F:\sliper\input"#输入文件夹(这里面可以放多个wav)
    32. output_dir="F:\sliper\output"#输出文件夹
    33. process_method="丢弃"#如果音频小于3就丢弃
    34. max_sec=15#音频最长为15
    35. min_sec=3#音频最小为3
    36. slicer_fn(input_dir,output_dir,process_method,max_sec,min_sec)

    测试输入

    得到

  • 相关阅读:
    leetcode 剑指 Offer 63. 股票的最大利润
    【矩阵论】4. 矩阵运算——广义逆——广义逆的计算
    docker 清理磁盘
    饿了么三面:让你怀疑人生的Spring Boot夺命连环40问
    配置高级 --------打包与运行---配置高级---多环境开发---日志
    spring boot + springcloud教程
    【Python】如果修改了第三方库(包)的源代码,该怎么做才能还原呢
    插帧中grid_sample函数详解
    《制造企业高质量发展成长指南》全新首发,3大亮点邀您品鉴!
    SpringBean的装配与注入
  • 原文地址:https://blog.csdn.net/qq_38403590/article/details/133700129