简体   繁体   English

用Java连接mp3文件

[英]Concatenate mp3 files in Java

I have some problem with concatenation mp3 files. 我有连接mp3文件的一些问题。 For example, I have 5 mp3 files. 例如,我有5个mp3文件。 And I want concatenate them to one file. 我想将它们连接到一个文件。

I try this: 我试试这个:

try {
    InputStream in = new FileInputStream("C:/a.mp3");
    byte[] buffer = new byte[1024];
    OutputStream os = new FileOutputStream(new File("C:/output.mp3",
            true));
    int count;
    while ((count = in.read(buffer)) != -1) {
        os.write(buffer, 0, count);
        os.flush();
    }
    in.close();
    in = new FileInputStream("C:/b.mp3");// second mp3
    while ((count = in.read(buffer)) != -1) {
        os.write(buffer, 0, count);
        os.flush();
    }
    in.close();
    os.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

But this do not working. 但这不起作用。 I get one mp3 file on the finish of this process, it contains all music data from source mp3 files, but in Options I see time of fihish mp3 only from first source mp3 file. 我在这个过程结束时得到一个mp3文件,它包含来自源mp3文件的所有音乐数据,但是在Options中我只看到来自第一个源mp3文件的fihish mp3的时间。 And, when I try use this final mp3 file when I use Xuggle library for adding this mp3 audio to mp4 video I get error. 而且,当我尝试使用这个最终的mp3文件,当我使用Xuggle库将此mp3音频添加到mp4视频时,我收到错误。

I'm sure, problem in bytes from every mp3 source file, where recordered meta infromation. 我敢肯定,来自每个mp3源文件的字节问题,其中记录了元信息。 Maybe exist some library for correct concatenation mp3? 也许存在一些正确连接mp3的库?

There are multiple problems with you approach. 你的方法有很多问题。

a.) If there are any meta data in the source files, you copy the meta data. a。)如果源文件中有任何元数据,则复制元数据。 In the concatinated file, those meta data (Id3v1 and Id3v2) will be present multiple times and worse, at locations where they are not supposed to be (Id3v1 must be located at the end of the file, Id3v2 at the beginning IIRC). 在连续文件中,那些元数据(Id3v1和Id3v2)将出现多次,更糟糕的是,在它们不应该存在的位置(Id3v1 必须位于文件的末尾,Id3v2位于开始的IIRC)。 This can confuse the decoder to recognize the file as corrupt (it may play the first section and then hiccup on the unexpected meta data of the 2nd part). 这可能会使解码器混淆将文件识别为损坏(它可能会播放第一部分,然后打击第二部分的意外元数据)。

b.) Calculating the length of an MP3 isn't all that simple, one would need to count the number of MP3 frames in the file. b。)计算MP3的长度并不是那么简单,需要计算文件中MP3帧的数量。 Since there is no header that tells the number of frames, the only safe way to do this is scan the file sequentially (like a decode). 由于没有标题可以告诉帧数,唯一安全的方法是按顺序扫描文件(如解码)。 This used to be a costly operation when MP3 was introduced, so there were multiple ways introduced to remedy this (VBR header block and Id3 meta tags). 在引入MP3时,这曾经是一项代价高昂的操作,因此有多种方法可以解决这个问题(VBR标题块和Id3元标记)。 If your player relies on these, it will always detect the length as only the 1st part. 如果您的玩家依赖于这些,它将始终检测长度仅为第一部分。

c.) Even if you strip away meta data properly, you can only concatenate MP3 streams using the same parameters (sample rate), some decoders may even have additional restrictions on bit rate and type (mono/MS/Istereo etc.). c。)即使您正确剥离元数据,您也只能使用相同的参数(采样率)连接MP3流,有些解码器甚至可能对比特率和类型(单声道/ MS / Istereo等)有额外的限制。

You see, it can be done, but its not trivial. 你看,它可以做到,但它不是微不足道的。

You cannot append this way music files. 你不能以这种方式追加音乐文件。 What you need is an external library (lame or...), in java is... almost imposible, JMF is dead. 你需要的是一个外部库(跛脚或......),在java中......几乎是不可能的,JMF已经死了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM