Reputation: 1716
I am using below very simple code to merge 6 mp3 files using FileStream
and it looks very simple. Though the merging is OK, but when I check the new generated file duration it's 6 seconds shorter then the orignal 6 mp3 files. below I have pasted the code with individual file duration as well.
string dirPath = @"C:\downloads\114\";
FileInfo[] files = new DirectoryInfo(dirPath).GetFiles(); // 6 audio files to merge
foreach (var file in files)
{
using (var f = file.Open(FileMode.Open))
{
byte[] bytes = new byte[f.Length];
using (FileStream isfs = new FileStream(dirPath + "114.mp3", FileMode.OpenOrCreate))
{
isfs.Seek(0, SeekOrigin.End); //go to the last byte
while (f.Read(bytes, 0, bytes.Length) > 0)
{
isfs.Write(bytes, 0, bytes.Length);
isfs.Flush();
}
}
}
}
The file duration in seconds:
#File #Duration in Seconds
114000.mp3 6.64 sec
114001.mp3 5.935 sec
114002.mp3 4.864 sec
114003.mp3 4.838 sec
114004.mp3 7.58 sec
114005.mp3 7.554 sec
114006.mp3 6.431 sec
Total Duration is : 43.842 seconds
#The new Generated File
114.mp3 37.198 seconds
The questions:
Just to mention: The new generated file and the 6 files Length
is the same!
Thanks!
Upvotes: 0
Views: 1716
Reputation: 5007
1) You are not merging mp3-files, you are merging binary files. You must take the header etc into account, else the merging is not going to work... (in your case you have more headers in the middle of the file, and there is no guarantee that the bitrate is the same between the files)
2) Have a look at What is the best way to merge mp3 files?, and Playing a MP3 file in a WinForm application might help you as well.
Upvotes: 1