Concatenating MP3 files in Scala / Java

I am trying to combine mp3s in Scala, although I actually use mostly Java libraries. So far I have put the necessary things on my CLASSPATH (see here: http://www.javazoom.net/mp3spi/documents.html to learn how to read mp3) and the following is written:

import java.io._; import java.io.IOException; import java.io.SequenceInputStream; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; object Main { def main(args: Array[String]) { val fileA = "Track 0000.mp3" val fileB = "Track 0001.mp3" try { var ClipA = AudioSystem.getAudioInputStream(new File(fileA)) var ClipB = AudioSystem.getAudioInputStream(new File(fileB)) var appendedFiles = new AudioInputStream( new SequenceInputStream(ClipA, ClipB), ClipA.getFormat(), ClipA.getFrameLength() + ClipB.getFrameLength()); } catch { case ioe: IOException => println(ioe.getMessage()) case e: Exception => println(e.getMessage()) } } } 

But now I'm struggling to burn mp3 to disk. Can someone help me? How it's done?

+4
source share
1 answer

The AudioSystem class has a helper method for this, AudioSystem.write :

 public static int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException 

You should be able to do something like:

 AudioSystem.write(appendedFiles, mp3AudioType, new File("/tmp/out.mp3")) 

If mp3AudioType is the correct AudioFileFormat.Type for mp3; maybe only AudioFileFormat.Type("MP3", "mp3") will work.

0
source

Source: https://habr.com/ru/post/1483964/


All Articles