如何加入多个mp3文件?

本尼·古德曼:

阅读其他问题,我想知道如何加入这样的多个mp3文件。我尝试了这段代码,但是我得到的是output.mp3中最新的2个mp3文件作为输出...预期的输出应该是将所有文件都转换为1个mp3文件。

from pydub import AudioSegment
from os import getcwd
import glob

cwd = (getcwd()).replace(chr(92), '/')
export_path = f'{cwd}/result.mp3'

MP3_FILES = glob.glob(pathname=f'{cwd}/*.mp3', recursive=True)
silence = AudioSegment.silent(duration=15000)
count, lenght = 0, len(MP3_FILES)

for n, mp3_file in enumerate(MP3_FILES):
    mp3_file = mp3_file.replace(chr(92), '/')
    print(n, mp3_file)

    count += 1
    if count == 1:
        # We get here upon fetching the first audio file from the list.
        # So we load it into `audio1`
        audio1 = AudioSegment.from_mp3(mp3_file)
        print('audio1')

    elif count == 2:
        # We get here upon fetching the second audio file from the list.
        # So we load it into `audio2`
        audio2 = AudioSegment.from_mp3(mp3_file)
        print('audio2')

    elif count == 3:
        # We get here upon fetching the THIRD audio file from the list.
        # Instead of loading it, we SET `res` to the sum of `audio1+silence+audio2`
        # We don't do anything with the third file, in fact we skip it
        #
        res = (audio1 + silence + audio2)
        print('Merging')

        # Here we reset `count`, so we basically start over the same cycle:
        #  - read the NEXT two audio files, skip the third, and REPLACE `res` to the NEW sum
        #    of the current `audio1`/`audio2` files
        count = 0

    if (n + 1) == lenght:
        res.export(export_path, format='mp3')
        print('\ndone!')
萨尔:

从我可以看到,循环存在问题。我在原始代码下方进行评论(for虽然只是循环),以提供一些见解:

for n, mp3_file in enumerate(MP3_FILES):
    mp3_file = mp3_file.replace(chr(92), '/')
    print(n, mp3_file)

    count += 1
    if count == 1:
        # We get here upon fetching the first audio file from the list.
        # So we load it into `audio1`
        audio1 = AudioSegment.from_mp3(mp3_file)
        print('audio1')

    elif count == 2:
        # We get here upon fetching the second audio file from the list.
        # So we load it into `audio2`
        audio2 = AudioSegment.from_mp3(mp3_file)
        print('audio2')

    elif count == 3:
        # We get here upon fetching the THIRD audio file from the list.
        # Instead of loading it, we SET `res` to the sum of `audio1+silence+audio2`
        # We don't do anything with the third file, in fact we skip it
        #
        res = (audio1 + silence + audio2)
        print('Merging')

        # Here we reset `count`, so we basically start over the same cycle:
        #  - read the NEXT two audio files, skip the third, and REPLACE `res` to the NEW sum
        #    of the current `audio1`/`audio2` files
        count = 0

    if (n + 1) == lenght:
        res.export(export_path, format='mp3')
        print('\ndone!')

正如您提到的那样,如果您只想将所有文件连接成一个音频,并且之间保持静音,则无需检查计数,也不需要检查enumerate我会继续,enumerate因为它可以很好地打印以显示更多上下文。所以,这是我的看法:

from pydub import AudioSegment
from os import getcwd
import glob

cwd = (getcwd()).replace(chr(92), '/')
export_path = f'{cwd}/result.mp3'

MP3_FILES = glob.glob(pathname=f'{cwd}/*.mp3', recursive=True)

silence = AudioSegment.silent(duration=15000)
full_audio = AudioSegment.empty()    # this will accumulate the entire mp3 audios

for n, mp3_file in enumerate(MP3_FILES):
    mp3_file = mp3_file.replace(chr(92), '/')
    print(n, mp3_file)

    # Load the current mp3 into `audio_segment`
    audio_segment = AudioSegment.from_mp3(mp3_file)

    # Just accumulate the new `audio_segment` + `silence`
    full_audio += audio_segment + silence
    print('Merging ', n)

# The loop will exit once all files in the list have been used
# Then export    
full_audio.export(export_path, format='mp3')
print('\ndone!')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章