准备:
官网地址:https://github.com/a-schild/jave2
步骤
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>2.4.6</version>
</dependency>
<!--根据环境添加ffmpeg执行包-->
<!--For one platform only (Linux 64Bit in this case)-->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-native-linux64</artifactId>
<version>2.4.6</version>
</dependency>
<!--For one platform only (Windows 64Bit in this case)-->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-native-win64</artifactId>
<version>2.4.6</version>
</dependency>
<!--For one platform only (MACOS 64Bit in this case)-->
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-native-osx64</artifactId>
<version>2.4.6</version>
</dependency>
代码路径见:https://github.com/muxiyue/docker-test/blob/master/src/test/java/video/VideoTest.java
普通的转码操作
@Test
public void test() {
ConvertProgressListener listener = new ConvertProgressListener();
String inputFilePath = VideoTest.class.getResource("/").getPath() + "file/input/test2.3gp";
String outputFilePath = VideoTest.class.getResource("/").getPath() + "file/output/test.mp4";
try {
File source = new File(inputFilePath);
File target = new File(outputFilePath);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("aac");
audio.setBitRate(new Integer(236000/2));
// audio.setChannels(new Integer(1));
audio.setSamplingRate(new Integer(8000));
VideoAttributes video = new VideoAttributes();
video.setCodec("mpeg4");
video.setBitRate(new Integer(128000));
video.setFrameRate(new Integer(15));
video.setSize(new VideoSize(1760, 1440));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("3gp");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
try {
encoder.encode(new MultimediaObject(source), target, attrs, listener);
}
catch (Exception e) {
e.printStackTrace();
System.out.println(encoder.getUnhandledMessages());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class ConvertProgressListener implements EncoderProgressListener {
public ConvertProgressListener() {
//code
}
public void message(String m) {
//code
}
public void progress(int p) {
//Find %100 progress
double progress = p / 1000.00;
System.out.println(progress);
}
public void sourceInfo(MultimediaInfo m) {
//code
}
}
filter测试
@Test
public void test2() {
ConvertProgressListener listener = new ConvertProgressListener();
String inputFilePath = VideoTest.class.getResource("/").getPath() + "file/input/test2.3gp";
String outputFilePath = VideoTest.class.getResource("/").getPath() + "file/output/test.mp4";
String ttfPath = VideoTest.class.getResource("/").getPath() + "file/input/Songti.ttc";
try {
File source = new File(inputFilePath);
File target = new File(outputFilePath);
AudioAttributes audio = new AudioAttributes();
audio.setSamplingRate(new Integer(8000));
audio.setVolume(512);
audio.setChannels(1);
VideoAttributes video = new VideoAttributes();
// 命令行: ffplay -i test2.mp4 -vf drawtext="fontfile=Songti.ttc:text='指定时间段内才能看见':x='if((gte(t\,5) * (lte(t\,10)))\,w-t*50\,NAN)':fontcolor=darkorange:fontsize=30"
// 去掉了反斜杠和drawtext后面的双引号环绕
VideoFilter videoFilter = new VideoFilter("drawtext=fontfile=" + ttfPath + ":text='指定时间段内才能看见':x='if((gte(t,1) * (lte(t,8))),w-t*50,NAN)':fontcolor=darkorange:fontsize=50");
video.addFilter(videoFilter);
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("3gp");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
try {
encoder.encode(new MultimediaObject(source), target, attrs, listener);
}
catch (Exception e) {
e.printStackTrace();
System.out.println(encoder.getUnhandledMessages());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}