1.pom添加:
<dependency> <groupId>ws.schild</groupId> <artifactId>jave-core</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-nativebin-linux64</artifactId> <version>3.0.1</version> </dependency>
2.新建类:
package com.api.ynlc.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ws.schild.jave.*; import ws.schild.jave.encode.AudioAttributes; import ws.schild.jave.encode.EncodingAttributes; import ws.schild.jave.encode.VideoAttributes; import java.io.File; import java.util.Date; /** * @Auther: liyl * @Date: 2021/9/16 10:35 * @Description: */ public class VideoChangeUtil { private static final Logger logger = LoggerFactory.getLogger(com.api.ynlc.util.VideoChangeUtil.class); public static boolean AviToMp4(String oldPath,String newPath) { // File source = new File("D:\\temp\\1.avi"); // File target = new File("D:\\file\\1.mp4"); File source = new File(oldPath); File target = new File(newPath); logger.info("转换后的路径:"+newPath); AudioAttributes audio = new AudioAttributes(); //音频编码格式 audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(800000)); audio.setChannels(new Integer(1)); VideoAttributes video = new VideoAttributes(); //视频编码格式 video.setCodec("libx264"); video.setBitRate(new Integer(6000000)); //数字设置小了,视频会卡顿 video.setFrameRate(new Integer(15)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); MultimediaObject multimediaObject = new MultimediaObject(source); try { logger.info("avi转MP4 --- 转换开始:"+new Date()); encoder.encode(multimediaObject, target, attrs); logger.info("avi转MP4 --- 转换结束:"+new Date()); return true; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InputFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (EncoderException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } }