【问题标题】:Merging mp3 files into one using FileStream使用 FileStream 将 mp3 文件合并为一个
【发布时间】:2014-08-19 22:14:34
【问题描述】:

我正在使用下面非常简单的代码来合并 6 个使用 FileStream 的 mp3 文件,它看起来非常简单。虽然合并没问题,但是当我检查新生成的文件持续时间时,它比原来的 6 mp3 文件短 6 秒。下面我也粘贴了带有单个文件持续时间的代码。

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();
            }
       }
    }
}

文件持续时间,以秒为单位:

#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

总时长为:43.842 秒

#The new Generated File
114.mp3         37.198 seconds

问题:

  1. 为什么新生成的文件时长不等于原来的6个文件?
  2. 如何使新文件的持续时间与原始文件相同或相等?

顺便提一下:新生成的文件和Length的6个文件是一样的! 谢谢!

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    1) 您不是在合并 mp3 文件,而是在合并二进制文件。您必须考虑标头等,否则合并将无法正常工作...(在您的情况下,文件中间有更多标头,并且不能保证文件之间的比特率相同)

    2) 看看What is the best way to merge mp3 files?Playing a MP3 file in a WinForm application 也可能对您有所帮助。

    【讨论】:

    • 谢谢,我在这里发现了一些有趣的东西。合并文件或合并二进制文件都使用Microsoft.WindowsAPICodePack.Shell 给出相同的持续时间43 秒,使用WMPLib 给出37 秒。我不知道哪个是正确的?
    • 难道不是反过来,37 秒二进制合并?给你 43 秒的那个应该是正确的:)
    • 好吧,我用两种不同的方法检查了这两个文件(Windows Media Player WMPLibMicrosoft.WindowsAPICodePack.Shell,我确信它在媒体播放器中显示为 37 秒,在其他 API 中显示为 43 秒。
    • mp3wrap 不能合并超过 255 个文件!
    • @ARH 然后一次合并 255 个?
    猜你喜欢
    • 2013-12-09
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2011-12-06
    • 2011-10-07
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多