连接Java中的mp3文件 [英] Concatenate mp3 files in Java

查看:161
本文介绍了连接Java中的mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在连接mp3文件时遇到了一些问题.例如,我有5个mp3文件.我想将它们连接到一个文件.

I have some problem with concatenation mp3 files. For example, I have 5 mp3 files. And I want concatenate them to one file.

我尝试:

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

但是这不起作用.在此过程完成后,我得到了一个mp3文件,其中包含来自源mp3文件的所有音乐数据,但是在选项"中,我仅从第一个源mp3文件中看到了mp3的时间.而且,当我使用Xuggle库将此mp3音频添加到mp4视频时,尝试使用此最终mp3文件时,出现错误.

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. 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源文件的字节数问题,其中记录了元信息.也许存在一些用于正确串联mp3的库?

I'm sure, problem in bytes from every mp3 source file, where recordered meta infromation. Maybe exist some library for correct concatenation mp3?

推荐答案

您的方法存在多个问题.

There are multiple problems with you approach.

a.)如果源文件中有任何元数据,则复制元数据.在隐藏的文件中,这些元数据(Id3v1和Id3v2)将多次出现,并且在不应该存在的位置(Id3v1 必须位于文件末尾,Id3v2)处更糟.在开始时IIRC).这可能会使解码器无法识别文件已损坏(它可能播放第一部分,然后打断第二部分的意外元数据).

a.) If there are any meta data in the source files, you copy the meta data. 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). 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.)计算MP3的长度并不是那么简单,需要计算文件中MP3帧的数量.由于没有标头说明帧数,因此唯一的 safe 方法是按顺序扫描文件(如解码).引入MP3时,这曾经是一项昂贵的操作,因此引入了多种补救方法(VBR标头块和Id3元标记).如果您的播放器依靠这些,它将始终仅将长度检测为第一部分.

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. 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). If your player relies on these, it will always detect the length as only the 1st part.

c.)即使您正确地剥离了元数据,也只能使用相同的参数(采样率)连接MP3流,某些解码器甚至可能对比特率和类型(mono/MS/Istereo等)有更多限制. ).

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.).

您知道,可以做到的,但是 没什么.

You see, it can be done, but its not trivial.

这篇关于连接Java中的mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆