如何加入多个mp3文件?
How to join multiple mp3 files?
阅读其他问题我想知道如何加入多个 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!')
阅读其他问题我想知道如何加入多个 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!')