今天接到领导一个调研需求,音频文件转换为mp3,视频文件转换为mp4
pom.xml
<dependency>
<groupId>it.sauronsoftware</groupId>
<artifactId>jave</artifactId>
<version>1.0.2</version>
</dependency>
工具类
/**
* https://ffmpeg.org/ffmpeg-codecs.html
* @date 2021/5/26 10:52
* @version: 1.0
*/
@Slf4j
public class FfmpegConvertUtils {
/*编码类型 libmp3lame*/
private static String LIBMP3LAME = "libmp3lame";
/*编码类型 mpeg4*/
private static String CODEC_MPEG4 = "mpeg4";
/*输出比率*/
private static int BIT_RATE_128 = 128000;
/*输出比率*/
private static int BIT_RATE_256 = 256000;
/*输出比率*/
private static int BIT_RATE_800 = 800000;
/*通道数量*/
private static int AUDIO_CHANNEL_COUNT = 2;
/*采样比率*/
private static int AUDIO_SAMPLING_RATE = 44100;
/*输出格式*/
private static String AUDIO_OUT_FORMAT = "mp3";
/*输出格式*/
private static String VIDEO_OUT_FORMAT = "mp4";
public static boolean convertToMp3(String sourcePath, String targetPath) {
File source = new File(sourcePath);
if (!source.exists()) {
throw new RuntimeException("输入文件不存在");
}
checkTargetFileDir(targetPath);
File target = new File(targetPath);
try {
AudioAttributes audio = new AudioAttributes();
audio.setCodec(LIBMP3LAME);
audio.setBitRate(BIT_RATE_128);
audio.setChannels(AUDIO_CHANNEL_COUNT);
audio.setSamplingRate(AUDIO_SAMPLING_RATE);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat(AUDIO_OUT_FORMAT);
attrs.setAudioAttributes(audio);
Encoder encoder = new Encoder();
encoder.encode(source, target, attrs);
return true;
} catch (Exception e) {
log.error("convertToMp3 :{}", ExceptionUtils.getStackTrace(e));
return false;
}
}
public static boolean convertToMp4(String sourcePath, String targetPath) {
File source = new File(sourcePath);
if (!source.exists()) {
throw new RuntimeException("输入文件不存在");
}
checkTargetFileDir(targetPath);
File target = new File(targetPath);
try {
AudioAttributes audio = new AudioAttributes();
audio.setCodec(LIBMP3LAME);
audio.setBitRate(BIT_RATE_128);
audio.setChannels(AUDIO_CHANNEL_COUNT);
audio.setSamplingRate(AUDIO_SAMPLING_RATE);
VideoAttributes video = new VideoAttributes();
video.setCodec(CODEC_MPEG4);
//清晰度 ,1080P最好在5Mbps/5120Kbps到8Mbps/8192Kbps之间,因为低于5Mbps不够清晰,而大于8Mbps视频文件会过大。
video.setBitRate(BIT_RATE_800);
//质量高低 1-35,值越小,质量越高
video.setFrameRate(3);
// video.setSize(new VideoSize(1080,800));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat(VIDEO_OUT_FORMAT);
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
/* System.out.println(JSONObject.toJSONString(encoder.getVideoEncoders()));
System.out.println(JSONObject.toJSONString(encoder.getSupportedEncodingFormats()));*/
encoder.encode(source, target, attrs);
return true;
} catch (Exception e) {
log.error("convertToMp4 :{}", ExceptionUtils.getStackTrace(e));
return false;
}
}
/**
* 验证文件目录是否存在,不存在就创建
*
* @param targetFile 文件路径
* @return
*/
public static void checkTargetFileDir(String targetFile) {
String dirPath = targetFile.substring(0, targetFile.lastIndexOf(File.separator) + 1);
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
}
public static void main(String[] args) {
convertToMp3("E:\\videotest\\音频素材\\ape1.ape","E:\\videotest\\output\\ape1.mp3");
System.out.println("开始处理");
long start = System.currentTimeMillis();
convertToMp4("E:\\videotest\\视频素材\\sdf.wmv", "E:\\videotest\\output\\a1.mp4");
System.out.println("耗时:" + (System.currentTimeMillis() - start)+"ms");
}