如何使用 C# 合并/加入 MP3 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1160888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I merge/join MP3 files with C#?
提问by James
I have a library of different words/phrases, and in order to build sentences at present I add a combination of these phrases into a playlist to make a sentence. Unfortunately if the user is running CPU intensive applications (which most of my users are) there can be a lag of a few seconds mid-sentence (in between phrases).
我有一个包含不同单词/短语的库,为了构建句子,目前我将这些短语的组合添加到播放列表中以构成句子。不幸的是,如果用户正在运行 CPU 密集型应用程序(我的大多数用户都是这样),句子中间(在短语之间)可能会有几秒钟的延迟。
In order to combat this I was thinking of an approach which will merge the right combination of MP3 files on the fly into an appropriate phrase, save this in the %temp% directory, and then play this 1 MP3 file which should overcome the problem I'm experiencing with gaps.
为了解决这个问题,我正在考虑一种方法,它将动态地将 MP3 文件的正确组合合并成一个适当的短语,将其保存在 %temp% 目录中,然后播放这个 1 MP3 文件,这应该可以解决我的问题正在经历差距。
What is the easiest way to do this in C#? Is there an easy way to do this? The files are fairly small, 3-4 seconds long each, and a sentence can consist of 3-20ish phrases.
在 C# 中执行此操作的最简单方法是什么?是否有捷径可寻?这些文件相当小,每个文件长 3-4 秒,一个句子可以由 3-20 个短语组成。
采纳答案by bart
MP3 files consist of "frames", that each represent a short snippet (I think around 25 ms) of audio.
MP3 文件由“帧”组成,每个帧代表一段简短的音频片段(我认为大约 25 毫秒)。
So yes, you canjust concatenate them without a problem.
所以是的,你可以毫无问题地连接它们。
回答by Jeff Yates
As MP3s are a compressed audio source, I imagine that you can't just concatenate them into a single file without decoding each one first to the wave form that it would play. This may be quite intensive. Perhaps you could cheat by using a critical section when playing back your phrase so that the CPU is not stolen from you until the phrase was complete. This isn't necessarily playing nice with other threads but might work if your phrases are short.
由于 MP3 是一种压缩音频源,我想您不能将它们连接成一个文件而不先将每个文件解码为它会播放的波形。这可能非常密集。也许您可以在播放您的乐句时使用临界区作弊,这样您就可以在乐句完成之前 CPU 不会被窃取。这不一定适用于其他线程,但如果您的短语很短,可能会奏效。
回答by Jon Galloway
On simple option is to shell to the command line:
一个简单的选项是 shell 到命令行:
copy /b *.mp3 c:\new.mp3
Better would be to concatenate the streams. That's been answered here: What would be the fastest way to concatenate three files in C#?
更好的是连接流。已在此处回答: 在 C# 中连接三个文件的最快方法是什么?
回答by Mark Heath
here's how you can concatenate MP3 files using NAudio:
以下是使用NAudio连接 MP3 文件的方法:
public static void Combine(string[] inputFiles, Stream output)
{
foreach (string file in inputFiles)
{
Mp3FileReader reader = new Mp3FileReader(file);
if ((output.Position == 0) && (reader.Id3v2Tag != null))
{
output.Write(reader.Id3v2Tag.RawData, 0, reader.Id3v2Tag.RawData.Length);
}
Mp3Frame frame;
while ((frame = reader.ReadNextFrame()) != null)
{
output.Write(frame.RawData, 0, frame.RawData.Length);
}
}
}
see herefor more info
看到这里获取更多信息