栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

MUSDB数据集的预处理(方法二)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MUSDB数据集的预处理(方法二)

        除了用musdb依赖包自带的musdbconvert方法的命令行处理musdb官网下载的mp4格式数据集外,还可以用ffmpeg方法直接用代码处理mp4格式的数据集。

当然,如果不嫌麻烦的话,也可以直接选择在官网直接下载 musdb-hq 格式的数据集,下载下来直接就是文件夹格式的wav文件。

话不多说,直接上链接和代码。

        下载mp4数据集:MUSDB18 - a corpus for music separation | Zenodo

        转换为五通道的wav文件:

"""五通道的wav文件如下
- `0` - The mixture,
- `1` - The drums,
- `2` - The bass,
- `3` - The rest of the accompaniment,
- `4` - The vocals.
"""
import argparse
import os
import subprocess
import tempfile
import librosa
import numpy as np
import soundfile as sf


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('origin_dataset_dir',
                        help='Path of the original dataset (.mp4)',
                        type=str)
    parser.add_argument('new_dataset_dir',
                        help='Output path of .wav dataset',
                        type=str)
    parser.add_argument('--sr',
                        help='Sample rate. (Default: 22050) ',
                        type=int, default=22050)
    args = parser.parse_args()

    origin_dataset_dir = args.origin_dataset_dir
    new_dataset_dir = args.new_dataset_dir

    if os.path.isdir(new_dataset_dir):
        raise FileExistsError(f'{new_dataset_dir} already exists.')
    else:
        os.mkdir(new_dataset_dir)

    os.mkdir(os.path.join(new_dataset_dir, 'train'))
    os.mkdir(os.path.join(new_dataset_dir, 'test'))

    with tempfile.TemporaryDirectory() as tmpdir:
        for subdir in ('train', 'test'):
            origin_dir = os.path.join(origin_dataset_dir, subdir)
            files = [f for f in os.listdir(origin_dir)
                     if os.path.splitext(f)[1] == '.mp4']
            for file in files:
                path = os.path.join(origin_dir, file)
                name = os.path.splitext(file)[0]
                wav_data = []
                # Extract & save the sound of `ch` channel to a temp directory
                # and then concatenate all channels to a single .wav file
                #将'ch'频道的声音提取并保存到临时目录中
                #然后将所有通道连接到单个通道的wav文件
                for ch in range(5):
                    temp_fn = f'{name}.{ch}.wav'
                    out_path = os.path.join(tmpdir, temp_fn)
                    subprocess.run(['ffmpeg', '-i', path,
                                    '-map', f'0:{ch}', out_path])
                    sound, _ = librosa.load(out_path, sr=args.sr, mono=True)
                    wav_data.append(sound)
                wav_data = np.stack(wav_data, axis=1)
                out_path = os.path.join(
                    new_dataset_dir, subdir, f'{name}.wav')
                sf.write(out_path, wav_data, args.sr)

if __name__ == '__main__':
    main()

注意:用命令行转换的wav文件全都是采样率为44100hz的,上述代码会生成22100hz采样率的wav文件,文件大小也有不同。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923645.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号