参考:
https://blog.csdn.net/dadiyang/article/details/85003240
https://hk.saowen.com/a/2ec2a73ec73091967c3ebdb5697832006cb255a7183377b6e8fae1c13f5e54bc
(1)ffmpeg.home does not exists
解决:java代码中添加:System.setProperty("ffmpeg.home", "xxx/ffmpeg/bin");
(2)jave.EncoderException: Stream mapping
解决:继承Encoder重写processErrorOutput方法
package com.campus.voice.utils;
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); } } } |
wav转mp3
try{ File source = new File(wavPath); File target = new File(saveMp3Path); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); //编解码器 audio.setBitRate(new Integer(128000)); //比特率 audio.setChannels(new Integer(2)); //渠道 audio.setSamplingRate(new Integer(44100)); //采样率 EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp3"); attrs.setAudioAttributes(audio); // Encoder encoder = new Encoder(); WavToMp3Encoder encoder = new WavToMp3Encoder(); try { encoder.encode(source, target, attrs); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InputFormatException e) { e.printStackTrace(); } catch (EncoderException e) { e.printStackTrace(); } }catch (Exception e){ e.printStackTrace();
} |