开发者

Combining multiple audio files in Python (with delay)

开发者 https://www.devze.com 2023-02-02 18:00 出处:网络
I\'m looking to combine a range of different audio files (mp3) in Python. One of the requirements is that I need to be able to specify a delay at the end of each file. To illustrate, something like:

I'm looking to combine a range of different audio files (mp3) in Python. One of the requirements is that I need to be able to specify a delay at the end of each file. To illustrate, something like:

[file1.mp3--------3 seconds----------][delay---------2 seconds--------][file2.mp3]-------------4 seconds][delay---------2 seconds][file3.mp3----------3 seconds---------]

Does an开发者_如何学编程yone here know of any mp3 libraries that can accomplish this? Python isn't really a necessity here. If it'll be easier in another language, that'll be fine.


I think FFmpeg can do this, given the right arguments. No real need to use a library.


To combine wav or aiff files, you can do something like this: (inspiration from here)

import aifc

def concatenate(*items):
    data = []

    for item in items:
        f = aifc.open(item, 'rb')
        data.append([f.getparams(), f.readframes(f.getnframes())])
        f.close()

    output = aifc.open('output.aif', 'wb')
    output.setparams(data[0][0])

    for item in data:
        output.writeframes(item[1])

    output.close()

See the link for the wav format (it's pretty much the same, but with the wave library)

To add silence, I would just make a one second silent file using your favorite audio editor and then concatenate in the proper amount of silence.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号