import java.io.File
import ws.schild.jave.{Encoder, MultimediaObject}
import ws.schild.jave.encode.AudioAttributes
import ws.schild.jave.encode.EncodingAttributes
object MP3ConversionUtil {
private val codec = "libmp3lame"
private val bitRate = 32000
private val samplingRate = 44100
private val channel = 2
val toMP3: (File => File) = (file) => {
val source = file
val targetMP3 = new File(s"${file.getAbsolutePath.replace(".", "")}.mp3")
val audio = new AudioAttributes
audio.setCodec(codec)
audio.setBitRate(bitRate)
audio.setSamplingRate(samplingRate)
audio.setChannels(channel)
val attrs = new EncodingAttributes
attrs.setAudioAttributes(audio)
attrs.setOutputFormat("mp3")
val instance = new Encoder()
instance.encode(new MultimediaObject(source), targetMP3, attrs, null)
targetMP3
}
}
上面的代码工作正常,可以完美地转换为 mp3。 在 linux(rehl-Linux x86_64) 服务器中,它不工作,抛出错误 ws.schild.jave.InputFormatException。