一、依赖
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>3.3.1</version>
</dependency>
二、工具类编写
public class VideoConvertUtil {
public static boolean formatToMp4(String url, String targetPath) {
File source = new File(url);
File target = new File(targetPath);
try {
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");
audio.setBitRate(800000);
audio.setChannels(1);
audio.setSamplingRate(44100);
VideoAttributes video = new VideoAttributes();
video.setCodec("libx264");
video.setBitRate(3200000);
video.setFrameRate(15);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setOutputFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
encoder.encode(new MultimediaObject(source), target, attrs);
} catch (Exception e) {
System.out.println("格式化视频异常");
e.printStackTrace();
}
}
}