Java视频格式转换---avi转MP4(h264编码格式的MP4视频在网页播放兼容性更好--试过其它的编码格式,没找到能在网页上直接播放的)
资源借鉴处
原文章
需要添加到pom.xml的包
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-native-win64</artifactId>
<version>2.4.5</version>
</dependency>
<!-- avi转MP4 end -->
java代码
import java.io.File;
import ws.schild.jave.AudioAttributes;
import ws.schild.jave.VideoAttributes;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.MultimediaObject;
public void AviToMp4(String oldPath,String newPath,UploadRecord uploadRecord,String type) {
// File source = new File("C:\\shiping\\1.avi");
// File target = new File("C:\\shiping\\2019-06-27333333测试.mp4");
File source = new File(oldPath);
File target = new File(newPath);
logger.info("转换前的路径:"+oldPath);
logger.info("转换后的路径:"+newPath);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame"); //音频编码格式
audio.setBitRate(new Integer(800000));
audio.setChannels(new Integer(1));
//audio.setSamplingRate(new Integer(22050));
VideoAttributes video = new VideoAttributes();
video.setCodec("libx264");//视频编码格式
video.setBitRate(new Integer(3200000));
video.setFrameRate(new Integer(15));//数字设置小了,视频会卡顿
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp4");
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());
} 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();
}
}