美文网首页
python音频处理

python音频处理

作者: Colleen_oh | 来源:发表于2020-01-17 09:34 被阅读0次

    环境: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))
    

    相关文章

      网友评论

          本文标题:python音频处理

          本文链接:https://www.haomeiwen.com/subject/nzcczctx.html