2

How do i merge multiple mp3(not wav) files with Python's pyaudio . I found a few ways to merge mp3 files with other applications or languages. For instance I found mp3wrap application.And this in go language . But how do i do this in Python . When try to do that I failed.

munir.aygun
  • 414
  • 1
  • 4
  • 11

3 Answers3

1

I can recommend ffmpeg-python for this.

!pip install ffmpeg-python

Concat function, where mp3s is a list of the mp3 paths and output a path of the output dir:

import ffmpeg

def concat_mp3s(mp3s, output):
    input_args = []
    for mp3 in mp3s:
        input_args.append(ffmpeg.input(mp3))
    ffmpeg.concat(*input_args, v=0, a=1).output(output).run()
yhgsag
  • 39
  • 5
0

I would use a python wrapper around ffmpeg (one of the best command-line utilities for doing this kind of media processing). One example is https://github.com/kkroening/ffmpeg-python.

Alexander Tsepkov
  • 3,946
  • 3
  • 35
  • 59
0

You can use ffmpeg to concatenate audios. ffmpeg must be installed in your system, for mac use this :

$ brew install ffmpeg

Then create concatenate command using python.

Send the list of audio file location in the following function and your output audio location. At the end use os module in python to run that command, and it will work like a charm. lis -> list of audio file location like : ['/home/name/music_1.mp3', '/home/name/music_2.mp3'] and so on..

It will run in sequential order, you can add any number of file locations here.

import os

def concat_audios(lis, file_location):
    st = ["ffmpeg"]
    for i in range(len(lis)):
        st.append("-i")
        st.append(lis[i])
    st.append("-filter_complex")
    st = ' '.join(st) + " "
    for i in range(len(lis)):
        st = st + "[" + str(i) + ":0]"

    st = st + "concat=n=" + str(len(lis)) + ":v=0:a=1[out] -map [out] " + file_location
    print(st)
    try:
        os.remove(file_location)
    except:
        pass

    os.system(st)

If you want to know the Main command to run in terminal is this otherwise just call the above function and your work is done:

ffmpeg -i /home/name/music_1.mp3 -i /home/name/music_2.mp3 -filter_complex [0:0][1:0]concat=n=2:v=0:a=1[out] -map [out] /home/name/output_music.mp3

If you want you can also use this module: https://github.com/kkroening/ffmpeg-python

I just did the same thing using above mentioned method.

Sheetansh Kumar
  • 191
  • 2
  • 8