加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python多个MP3文件合并

(2018-10-09 17:05:17)
标签:

ffmpeg

ffmpy

合并多个mp3

拼接多个mp3

拼接多个音频

分类: Python
因为语音合成时字数有限制,不得不将长篇的文字分开来进行合成,然后再将多个MP3文件按照顺序来合并。

1、需要安装的工具(lame、ffmpeg)
tar -xzvf lame-3.100.tar.gz
./configure
make
make install

tar -xf ffmpeg-4.0.2.tar.bz2
ffmpeg-4.0.2# ./configure --enable-shared --enable-libmp3lame --prefix=/usr/local/ffmpeg --disable-yasm
make
make install

2、系统环境变量配置
vi ~/.bashrc
alias ffmpeg="/usr/local/ffmpeg/bin/ffmpeg"
alias ffprobe="/usr/local/ffmpeg/bin/ffprobe"
source ~/.bashrc

3、需要安装的python模块
pip install ffmpy

4、需要引入的模块
import ffmpy
from ffmpy import FFmpeg
from collections import OrderedDict

5、合并音频
def createAudio(cont, pid, conn):
    contlen = len(cont.decode('utf-8'))  # 获取汉字长度
    startindex = 0
    i = 0
    inputs = OrderedDict() # 创建有序的文件字典
    outfile = ''
    while contlen > startindex:
        s = startindex + 300
        endindex = min(s, contlen)
        srctext = cont.decode('utf-8')[startindex:endindex].encode('utf-8') # 截取中文长度
        outfile = getResponse(i, srctext)
        if outfile != '':
            inputs.update({outfile: None}) # 向有序字典中加入item
        startindex = s
        if startindex >= contlen:
            break
        i = i + 1
    if i > 0:
        param = '-filter_complex \''
        for j in range(i+1):
            param += '[' + str(j) + ':0]'
        param += ' concat=n='+ str(i+1) + ':v=0:a=1 [a]\' -map [a]'

        m2 = hashlib.md5()
        m2.update(cont)
        outfile = 'audio/%s.mp3' % m2.hexdigest() # 合并后文件名

        outputs={outfile: param}
        ff = FFmpeg(executable="/usr/local/ffmpeg/bin/ffmpeg",
                inputs=inputs, outputs=outputs)
        #ff.cmd # 查看ffmpeg命令
        ff.run()

注:ffmpeg命令:
ffmpeg -i a.mp3 -i b.mp3 -filter_complex '[0:0] [1:0] concat=n=2:v=0:a=1 [a]' -map [a] out.mp3

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有