我有两个 mp3 文件,有一些重叠。第一个文件的最后几秒(通常小于 2 秒)始终出现在最后一个文件的前几秒。
如何将两个音频文件合并为一个并消除重叠部分?
1、目前音频文件为mp3。 2、需要一个python库。我知道 FFMPEG 可能有效,但我没有经验。 3、其他编程语言解决方案也可以。
请给我一些关键词,我希望我能完成剩下的工作。
提前谢谢您:)
我尝试询问 ChatGPT 但没有得到答案。
您可以使用
pydub
在 Python 中编写,最好的部分是 Python 具有内置的音频操作功能。
第一
pip install pydub
from pydub import AudioSegment
# Load both MP3 files
audio1 = AudioSegment.from_mp3("file1.mp3")
audio2 = AudioSegment.from_mp3("file2.mp3")
# Define the overlap duration (in milliseconds)
overlap_duration = 2000 # 2 seconds overlap
# Trim the overlap from the second file
audio2_trimmed = audio2[overlap_duration:] # Remove the first 'overlap_duration' milliseconds
# Combine the two audio files
combined_audio = audio1 + audio2_trimmed
# Export the combined audio to a new MP3 file
combined_audio.export("combined.mp3", format="mp3")
print("MP3 files combined successfully!")
您也可以在 Java 中实现此目的,但您需要额外依赖于
JAVE (Java Audio Video Encoder),
一个常用的音频处理库。
查看他们的 GitHub 链接和 Maven 依赖项 https://github.com/a-schild/jave2
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.MultimediaInfo;
import java.io.File;
public class Mp3Combiner {
public static void main(String[] args) {
try {
File file1 = new File("file1.mp3");
File file2 = new File("file2.mp3");
// Determine overlap duration (in milliseconds)
int overlapDuration = 2000; // 2 seconds overlap
// Trim the overlapping part from the second file
File trimmedFile2 = trimMp3(file2, overlapDuration);
// Combine the two audio files
File combinedFile = new File("combined.mp3");
concatenateMp3(file1, trimmedFile2, combinedFile);
System.out.println("MP3 files combined successfully!");
} catch (Exception e) {
// handle exp
}
}
private static File trimMp3(File mp3File, int trimStartMs) throws Exception {
File trimmedFile = new File("trimmed_file2.mp3");
Encoder encoder = new Encoder();
MultimediaInfo info = encoder.getInfo(mp3File);
long duration = info.getDuration(); // duration in milliseconds
// Calculate the part to trim (keep only after the overlap)
long newStart = trimStartMs;
long newDuration = duration - trimStartMs;
AudioAttributes audioAttributes = new AudioAttributes();
audioAttributes.setCodec("libmp3lame");
EncodingAttributes attributes = new EncodingAttributes();
attributes.setAudioAttributes(audioAttributes);
attributes.setOffset((float) newStart / 1000); // trim start in seconds
attributes.setDuration((float) newDuration / 1000); // trim duration in seconds
// Re-encode the trimmed file
encoder.encode(mp3File, trimmedFile, attributes);
return trimmedFile;
}
private static void concatenateMp3(File mp3File1, File mp3File2, File outputFile) throws Exception {
Encoder encoder = new Encoder();
AudioAttributes audioAttributes = new AudioAttributes();
audioAttributes.setCodec("libmp3lame");
EncodingAttributes attributes = new EncodingAttributes();
attributes.setAudioAttributes(audioAttributes);
File[] inputFiles = { mp3File1, mp3File2 };
encoder.encode(inputFiles, outputFile, attributes);
}
}