关于amr转mp3格式转化问题
应需求写一个格式转化问题,在本地的windows系统测试完美通过,在Linux系统是弄了一天都是报错。
结果问题是在重写processErrorOutput时方法名写错,在windows系统上方法名不同是可以使用的,但是在Linux系统上是不行的 必须是重写,不能有改变,感谢大佬 java-小胡提供的代码。此代码windows和linux都可以使用。
引入依赖
<!--amr转mp3-->
<dependency>
<groupId>com.github.dadiyang</groupId>
<artifactId>jave</artifactId>
<version>1.0.5</version>
</dependency>
arm转码mp3
public static void amrToMp3(String sourcePath,String targetPath) {
//这里是强制设置ffmpeg的路径,就是默认路径
System.setProperty("ffmpeg.home", "/tmp/jave-1");
File source = new File(sourcePath);
File target = new File(targetPath);
System.out.println("sourcePath============="+sourcePath);
System.out.println("targetPath============="+targetPath);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp3");
attrs.setAudioAttributes(audio);
WavToMp3Encoder encoder = new WavToMp3Encoder();
try {
encoder.encode(source, target, attrs);
} catch (EncoderException e) {
e.printStackTrace();
}
}
WavToMp3Encoder重写Encoder方法如下:
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncoderProgressListener;
import it.sauronsoftware.jave.EncodingAttributes;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WavToMp3Encoder extends Encoder {
private static Logger logger = LoggerFactory.getLogger(WavToMp3Encoder.class);
protected void processErrorOutput(EncodingAttributes attributes, BufferedReader errorReader, File source, EncoderProgressListener listener) throws EncoderException, IOException {
try {
String line;
while ((line = errorReader.readLine()) != null) {
logger.debug(line);
}
} catch (Exception exp) {
logger.error("file convert error message process failed. ", exp);
}
}
}
转载自java-小胡:https://blog.csdn.net/weixin_42167600/article/details/126372311
共勉。