前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python处理音频文件(mp3)

python处理音频文件(mp3)

作者头像
py3study
发布2020-01-03 16:26:24
1.8K0
发布2020-01-03 16:26:24
举报
文章被收录于专栏:python3python3

今天aiping点读笔的MP3源文件出现声音大小不一致,而且需要将英文单词MP3与单词翻译MP3文件连接起来,刚开始使用ffmpeg效果很差。万能的gitgub再次证明了它的万能,我找到了pydub

1 网址:https://github.com/jiaaro/pydub

2 pydub需要依赖 libav或者ffmpeg

3 在mac环境下安装依赖:(二选一)

代码语言:javascript
复制
brew install libav --with-libvorbis --with-sdl --with-theora
将所有依赖都安装上~~
brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis--with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools --with-fdk-aac --with-freetype --with-ffplay --with-ffplay --with-freetype --with-frei0r --with-libass --with-libbluray --with-libcaca --with-libquvi --with-libvidstab --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-openssl --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools --with-x265

4 安装pydub:  pip install pydub

5 使用pydub:

代码语言:javascript
复制
            enPath = "%s%s/%s"%(enDir,file,enfile) #英文文件的路径
            cnPath = "%s%s/%s"%(cnDir,file,enfile.replace("en_w","cn_w"))#中文文件的路径
            targetPath = "%s%s/%s"%(toDir,file,enfile.replace("en_w","all")) #合并文件的路径
            #加载MP3文件
            song1 = AudioSegment.from_mp3(enPath)
            song2 = AudioSegment.from_mp3(cnPath)

            #取得两个MP3文件的声音分贝
            db1 = song1.dBFS
            db2 = song2.dBFS

            song1 = song1[300:] #从300ms开始截取英文MP3

            #调整两个MP3的声音大小,防止出现一个声音大一个声音小的情况
            dbplus = db1 - db2
            if dbplus < 0: # song1的声音更小
                song1+=abs(dbplus)
            elif dbplus > 0: #song2的声音更小
                song2+=abs(dbplus)

            #拼接两个音频文件
            song = song1 + song2

            #导出音频文件
            song.export(targetPath, format="mp3") #导出为MP3格式
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档