C#:连接2个MP3文件

Mar*_*kus 6 c# merge mp3 concat

我尝试使用下面的代码连接2个MP3文件.我有一个新文件,我可以播放上半部分(完成第一个文件),但下半部分是静音.新文件的长度是正确的.我做错了什么?

List<Byte[]> files = new List<byte[]>();
var tempfile = File.ReadAllBytes(Path.Combine(path, "1.mp3"));
files.Add(tempfile);
tempfile = File.ReadAllBytes(Path.Combine(path, "2.mp3"));
files.Add(tempfile);
Byte[] a=new Byte[files[0].Length+files[1].Length];
Array.Copy(files[0], a, files[0].Length);
Array.Copy(files[1], a, files[1].Length);

File.WriteAllBytes(Path.Combine(path, "3.mp3") , a);
Run Code Online (Sandbox Code Playgroud)

Mat*_*ted 12

我愿意打赌你只听第二首歌.(并且两个文件的长度相同或者第一个较短)

您正在复制第一首歌曲数据.并且MP3数据是流式的,因此您可以将文件彼此附加而不必担心比特率(虽然它们可能会出现故障)比特率应该自动调整.

MP3帧标题上的细节

... 试试这个...

Array.Copy(files[0], 0, a, 0, files[0].Length);
Array.Copy(files[1], 0, a, files[0].Length, files[1].Length);
Run Code Online (Sandbox Code Playgroud)

......还是更好......

using (var fs = File.OpenWrite(Path.Combine(path, "3.mp3")))
{
    var buffer = File.ReadAllBytes(Path.Combine(path, "1.mp3"));
    fs.Write(buffer, 0, buffer.Length);
    buffer = File.ReadAllBytes(Path.Combine(path, "2.mp3"));
    fs.Write(buffer, 0, buffer.Length);
    fs.Flush();
}
Run Code Online (Sandbox Code Playgroud)

  • 这将有点工作,但你可能会得到ID3标签位于输出文件的中间.剥离它们是最好的(至少对于后续文件) (3认同)

Mar*_*ath 5

以下是使用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

浏览此处获取更多信息。


har*_*eyt 5

简单的:

public static void Combine(string[] mp3Files, string mp3OuputFile)
{
    using (var w = new  BinaryWriter(File.Create(mp3OuputFile)))
    {
        new List<string>(mp3Files).ForEach(f => w.Write(File.ReadAllBytes(f)));
    }
}
Run Code Online (Sandbox Code Playgroud)