c#:将mp3文件覆盖到另一个文件

c#:将mp3文件覆盖到另一个文件,c#,ffmpeg,mp3,naudio,C#,Ffmpeg,Mp3,Naudio,我用下面的代码将3个mp3文件合并成一个mp3文件 using (var fs = File.OpenWrite(Path.Combine(txtOutputFile.Text, "new.mp3"))) { var buffer = File.ReadAllBytes(Path.Combine(txtIntroLoc.Text, fileName1)); fs.Write(buffer, 0, buffer

我用下面的代码将3个mp3文件合并成一个mp3文件

using (var fs = File.OpenWrite(Path.Combine(txtOutputFile.Text, "new.mp3")))
            {
                var buffer = File.ReadAllBytes(Path.Combine(txtIntroLoc.Text, fileName1));
                fs.Write(buffer, 0, buffer.Length);
                buffer = File.ReadAllBytes(Path.Combine(txtOutroloc.Text, fileName2));
                fs.Write(buffer, 0, buffer.Length);
                buffer = File.ReadAllBytes(Path.Combine(txtFileThree.Text, fileName3));
                fs.Write(buffer, 0, buffer.Length);
                fs.Flush();
            }
我想要的是覆盖另一个mp3文件,它将在这个新创建的mp3文件的背景下播放。我知道这是可能的,但不是正确的方式去实现这一点。任何帮助都会很好。 关于“覆盖”的问题,谢谢你:

要做到这一点,必须先将原稿解码为PCM,然后在覆盖层中混合,然后将整个内容重新编码为MP3

在现有代码上:

你可以像这样连接MP3文件。通常我会建议去掉ID3标签,只制作一个文件,每个文件都有MP3帧。如果您使用的是NAudio,则可以使用Mp3FileReader上的
ReadNextFrame()
方法获取每个MP3帧,并将其
RawBytes
写入文件


为了获得最佳效果,您希望所有MP3文件使用相同的采样率和通道数。此外,如果这些是VBR,您将使中的信息无效,因此最好也放弃这些信息。

最后我找到了一个解决方案。使用
NAudio
我们可以混合wav流,因此首先将mp3转换为wav,然后混合wav文件,然后使用
lame.exe
将生成的wav文件重新转换为mp3

感谢Mark Heath,使用NAudio库,可以使用以下代码段将MP3转换为WAV

string file = "new.mp3";
            Mp3FileReader readers = new Mp3FileReader(file);
            WaveFormat targetFormat = new WaveFormat();
            WaveStream convertedStream = new WaveFormatConversionStream(targetFormat, readers);
            WaveFileWriter.CreateWaveFile("firstwav.wav", convertedStream);
现在,可以使用使用NAudio类的代码将其与另一个wav文件混合

string[] inputFiles = new string[2];
            Stream output = new MemoryStream();
            inputFiles[0] = "firstwav.wav";
            inputFiles[1] = "secondwav.wav";
mixWAVFiles(inputFiles);
mixWAVFiles
方法

public void mixWAVFiles(string[] inputFiles)
        {
            int count = inputFiles.GetLength(0);
            WaveMixerStream32 mixer = new WaveMixerStream32();
            WaveFileReader[] reader = new WaveFileReader[count];
            WaveChannel32[] channelSteam = new WaveChannel32[count];
            mixer.AutoStop = true;

            for (int i = 0; i < count; i++)
            {
                reader[i] = new WaveFileReader(inputFiles[i]);
                channelSteam[i] = new WaveChannel32(reader[i]);
                mixer.AddInputStream(channelSteam[i]);
            }
            mixer.Position = 0;
            WaveFileWriter.CreateWaveFile("mixedWavFile.wav", mixer);
        }

如果像这样合并MP3文件真的有效,我会感到惊讶。你试过播放结果文件了吗?谷歌搜索的第二个链接。@Surfbutler,这对我来说也很有趣,但它能起作用:)好的,那太好了:)我想覆盖另一个曲目会困难得多,因为它需要对原始和新曲目进行采样,然后装入一个新的组合文件。我猜那里有一个MP3 api可能会有所帮助。如果你想覆盖文件,请查看
FFMpegConverter
out。您只需安装NuGet包(
NReco.VideoConverter
)并调用
.Invoke(“{arguments find in link}”)。链接到命令:谢谢Mark,我现在已经转到NAudio来合并这两个mp3文件。我想知道一个mp3文件覆盖另一个mp3文件的例子,因为我从未使用过解码和重新编码。
public void convertWAVtoMP3(string wavfile)
        {
            //string lameEXE = @"C:\Users\Jibran\Desktop\MP3 Merger\bin\Debug\lame.exe";
            string lameEXE = Path.GetDirectoryName(Application.ExecutablePath) +"/lame.exe";
            string lameArgs = "-V2";

            string wavFile = wavfile;
            string mp3File = "mixed.mp3";

            Process process = new Process();
            process.StartInfo = new ProcessStartInfo();
            process.StartInfo.FileName = lameEXE;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.Arguments = string.Format(
                "{0} {1} {2}",
                lameArgs,
                wavFile,
                mp3File);

            process.Start();
            process.WaitForExit();

            int exitCode = process.ExitCode;
}