赞
踩
目录
依赖项:
pip install librosa
pip install transformers
- import librosa
- import numpy as np
- import torch
- from transformers import Wav2Vec2Processor
-
- processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
-
-
-
- wav_path = r'E:\迅雷下载\data\finedance\music_wav\001.wav'
- speech_array, sampling_rate = librosa.load(wav_path, sr=16000)
- input_values = np.squeeze(processor(speech_array, sampling_rate=16000).input_values)
-
- print(input_values.shape)
- import librosa as lr
- import numpy as np
- import soundfile as sf
- from tqdm import tqdm
-
-
- def slice_audio(audio_file, stride, length, out_dir):
- # stride, length in seconds
- audio, sr = lr.load(audio_file, sr=None)
- file_name = os.path.splitext(os.path.basename(audio_file))[0]
- start_idx = 0
- idx = 0
- window = int(length * sr)
- stride_step = int(stride * sr)
- while start_idx <= len(audio) - window:
- audio_slice = audio[start_idx : start_idx + window]
- sf.write(f"{out_dir}/{file_name}_slice{idx}.wav", audio_slice, sr)
- start_idx += stride_step
- idx += 1
- return idx
-
-
- def slice_audio_folder(wav_dir, stride=0.5, length=5):
- wavs = sorted(glob.glob(f"{wav_dir}/*.wav"))
- wav_out = wav_dir + "_sliced"
- os.makedirs(wav_out, exist_ok=True)
- for wav in tqdm(wavs):
- audio_slices = slice_audio(wav, stride, length, wav_out)
-
- if __name__ == '__main__':
- slice_audio_folder(r"E:\Projects\FineDance-main\data\code")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。