0

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:

  1. Why the new generated file duration is not equal to original 6 files?
  2. 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!

Ria
  • 10,237
  • 3
  • 33
  • 60
ARH
  • 1,566
  • 3
  • 25
  • 56

1 Answers1

1

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.

Community
  • 1
  • 1
flindeberg
  • 4,887
  • 1
  • 24
  • 37
  • Thanks, I figured something interesting here. merging files or merging binary both give the same duration 43 seconds using `Microsoft.WindowsAPICodePack.Shell` and 37 seconds using `WMPLib`. I don't which one is correct? – ARH Jun 29 '14 at 10:43
  • Wasn't it the other way around, 37 sec with binary merge? The one which gives you 43 sec should be the correct one :) – flindeberg Jun 29 '14 at 11:05
  • well, I checked both files with two different methods (Windows Media Player `WMPLib` and `Microsoft.WindowsAPICodePack.Shell` and I am sure that it shows 37 seconds in media player and 43 in other API. – ARH Jun 29 '14 at 11:19
  • mp3wrap cannot merge more than 255 files! – ARH Jul 02 '14 at 04:35
  • @ARH Merge them 255 at a time then? – flindeberg Jul 02 '14 at 07:41
  • @ARH Yes, so merge them 255 at a time, 255 => 1, add one more etc, if it doesn't work do post a new question with your particular issue and code so people can reproduce it. – flindeberg Jul 03 '14 at 14:50