依赖
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-linux64</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-win64</artifactId>
<version>2.7.3</version>
</dependency>
实现
import cn.hutool.core.io.FileUtil;
import ws.schild.jave.AudioAttributes;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.InputFormatException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.VideoAttributes;
import java.io.File;
import java.io.IOException;
/**
*/
public class Test {
/**
* MP4转其他格式
* @param localPath
* @param targetPath
* @return
* @throws IOException
*/
public static String transcode(String localPath, String targetPath) throws IOException {
File source = new File(localPath);
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("libmp3lame");//音频编码格式
//audio.setBitRate(new Integer(56000));//设置比特率,比特率越大,转出来的音频越大(默认是128000,最好默认就行,有特殊要求再设置)
audio.setChannels(new Integer(1));
audio.setSamplingRate(new Integer(22050));
VideoAttributes video = new VideoAttributes();
video.setCodec("libx264");//视屏编码格式
//video.setBitRate(new Integer(56000));//设置比特率,比特率越大,转出来的视频越大(默认是128000,最好默认就行,有特殊要求再设置)
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 {
encoder.encode(multimediaObject,target,attrs);
}catch (IllegalArgumentException e){
e.printStackTrace();
}catch (InputFormatException e){
e.printStackTrace();
}catch (EncoderException e){
e.printStackTrace();
}
FileUtil.del(localPath);
if (target.exists()) {
return targetPath;
}
return null;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
//视频转码
String localPath ="D:\\output.mp4";
String targetPath ="D:\\output.mpeg";
try {
transcode(localPath,targetPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}