mp3格式音频转wav格式音频
程序员文章站
2022-06-21 15:07:32
...
1.mp3格式音频转wav格式音频
package com.sunnyart.common.utils.file;
import java.io.File;
import org.springframework.stereotype.Service;
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.AudioInfo;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.MultimediaInfo;
/**
* MP3转wav
*
*/
@Service
public class AudioUtil {
/**
*MP3转 wav
* @param source mp3文件地址
* @param desFileName wav文件地址
* @return
* @throws Exception
*/
public static File mp3ToWav(File source, String desFileName)
throws Exception {
Encoder encoder = new Encoder();
MultimediaInfo mul = encoder.getInfo(source);
AudioInfo audioEntity = mul.getAudio();
File target = new File(desFileName);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(audioEntity.getBitRate()*1000)); //音频比率
audio.setChannels(new Integer(2));
audio.setSamplingRate(new Integer(audioEntity.getSamplingRate()));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("wav");
attrs.setAudioAttributes(audio);
encoder.encode(source, target, attrs);
return target;
}
}
2.pom依赖
<!-- https://mvnrepository.com/artifact/it.sauronsoftware/jave -->
<dependency>
<groupId>it.sauronsoftware</groupId>
<artifactId>jave</artifactId>
<version>1.0.2</version>
</dependency>
jave官方文档:http://www.sauronsoftware.it/projects/jave/manual.php