Reputation: 1
In my application, I fetch multiple audio (mp3) files from a server and get them as input stream or byte array. I convert them to a list of AudioInputStreams then and at the moment concat the data to one large ByteArrayInputStream. I then convert that to an AudioInputStream again and provide the data in it (byte[]) through a REST endpoint. My frontend is a react application where users can listen to it and download it.
Unfortunately, this process seems to break the mp3 header and the length is wrong. My frontend player does play the file to the end, so generally, the process is even working, but if I download it as a file the length is always the same as from the first file I concatenated in backend and some audio players won't even play to the end.
The question is: how to concat two mp3 byte-arrays properly in java?
Upvotes: 0
Views: 477
Reputation: 1
ok, I managed to solve the task with Jaffree which is very similar to ffmpeg-cli-wrapper which is a java wrapper for ffmpeg. This may not have the greatest performance because an external program is started but if fits my needs here. The relevant code is:
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
for (InputStream inputStream : inputStreams) {
FFmpeg.atPath()
.addInput(PipeInput.pumpFrom(inputStream))
.addOutput(
PipeOutput.pumpTo(byteArrayOutputStream)
.setFormat("mp3")
)
.execute();
}
}
where inputStreams are valid mp3 data InputStreams
Upvotes: 0