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:
- Why the new generated file duration is not equal to original 6 files?
- How can I make the new file duration the same or equal to the original files?
Just to mention: The new generated file and the 6 files Length
is the same!
Thanks!