Concatenating mp3 files in Java

I have a problem with files as concatenation. For example, I have 5 mp3 files. And I want to combine them into 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 that does not work. I get one mp3 file at the end of this process, it contains all the music data from the original mp3 files, but in Options I see the mp3 fish time only from the first mp3 source file. And when I try to use this final mp3 file, when I use the Xuggle library to add this mp3 audio to mp4 video, I get an error.

I am sure the problem is in bytes from each file of the mp3 file where the meta information is recorded. Maybe there is some kind of library for the correct concatenation of mp3?

0
java concatenation audio mp3 xuggler
source share
2 answers

There are several problems with you.

a.) If there is metadata in the source files, you copy the metadata. In the combined file, this metadata (Id3v1 and Id3v2) will be present several times and worse, in those places where they should not be (Id3v1 should be located at the end of the file, Id3v2 at the beginning of IIRC). This can prevent the decoder from recognizing the file as corrupted (it can play the first section, and then hiccup in the unexpected metadata of the second part).

b.) Calculating the length of MP3s is not so simple, you would need to count the number of MP3 frames in the file. Since there is no header that reports the number of frames, the only safe way to do this is to scan the file sequentially (for example, decode). It used to be an expensive operation when entering MP3, so there were several ways to fix this (VBR header block and Id3 meta tags). If your player relies on them, he will always determine the length as soon as the 1st part.

c.) Even if you delete the metadata properly, you can concatenate MP3 streams using the same parameters (sample rate), some decoders may even have additional restrictions on the bit rate and type (mono / MS / Istereo, etc. .).).

You see, this can be done, but it is not trivial.

+2
source share

You cannot add music files this way. You need an external library (lame or ...), in java ... almost impossible, JMF is dead.

0
source share

All Articles