python音频处理

2020-01-17  本文已影响0人  Colleen_oh

环境:ubuntu18
ubuntu上要安装ffmpeg

用于mp3转wav

def mp3_wav(path):#mp3转wav
    for category in os.listdir(path):
        catdir = os.path.join(path,category)
        if  os.path.isdir(catdir):# 如果不是文件夹则跳过
            for mp3file in os.listdir(catdir):
                filename = os.path.splitext(mp3file)[0]
                new_filename = filename + '.wav'
                os.system('ffmpeg -i ' + os.path.join(catdir, mp3file) +' -f wav '+ os.path.join(catdir, new_filename))
        else :
            filename = os.path.splitext(category)[0]
            new_filename = filename + '.wav'
            os.system('ffmpeg -i ' + catdir +' -f wav '+ os.path.join(path,new_filename))

把采样率转为16k

def tran_sample_rate(path):
    for category in os.listdir(path):
        catdir = os.path.join(path,category)
        if  os.path.isdir(catdir):# 如果不是文件夹则跳过
            for audio_file in os.listdir(catdir):
                filename = os.path.splitext(audio_file)[0]
                new_filename = filename + '_16k.wav'
                os.system('ffmpeg -i ' + os.path.join(catdir,audio_file) +' -ar 16000 '+ os.path.join(catdir,new_filename))
        else :
            filename = os.path.splitext(category)[0]
            new_filename = filename + '_16k.wav'
            os.system('ffmpeg -i ' + catdir +' -ar 16000 '+ os.path.join(path,new_filename))

转为单声道

def tran_mono(path):
    for category in os.listdir(path):
        catdir = os.path.join(path, category)
        if os.path.isdir(catdir):  # 如果不是文件夹则跳过
            for audio_file in os.listdir(catdir):
                filename = os.path.splitext(audio_file)[0]
                new_filename = filename + '_mono.wav'
                os.system('ffmpeg -i ' + os.path.join(catdir, audio_file) + ' -ac 1 ' + os.path.join(catdir,
                                                                                                         new_filename))
        else:
            filename = os.path.splitext(category)[0]
            new_filename = filename + '_mono.wav'
            os.system(
                'ffmpeg -i ' + catdir + ' -ac 1 ' + os.path.join(path, new_filename))
上一篇 下一篇

猜你喜欢

热点阅读