pom.xml
增加依赖
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-core</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-nativebin-win64</artifactId>
<version>3.0.0</version>
</dependency>
进行视频压缩
package fun.gudu.demo;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.filters.FilterGraph;
import ws.schild.jave.info.VideoSize;
import java.io.File;
public class DemoApplicationTest {
@Before
public void before() throws Exception {
}
@After
public void after() throws Exception {
}
/**
* Method: main(String[] args)
*/
@Test
public void testMain() throws Exception {
File source = new File("E:\\BaiduDownload\\gj.mp4"); // 源文件路径
File target = new File("E:\\BaiduDownload\\gj-1.mp4"); // 目标文件路径
try{
AudioAttributes audio = new AudioAttributes(); // 音频设置
audio.setCodec("libmp3lame");
audio.setBitRate(new Integer(22050)); // 比特率
audio.setChannels(new Integer(1)); // 声道
audio.setSamplingRate(new Integer(22050)); // 采样率
VideoAttributes video = new VideoAttributes(); // 视频设置
video.setCodec("mpeg4"); // 编码格式
video.setFaststart(true);
audio.setBitRate(new Integer(10000)); // 比特率
video.setFrameRate(new Integer(15)); // 帧率
video.setQuality(100); // 质量
video.setSize(VideoSize.hd720); // 分辨率
EncodingAttributes attrs = new EncodingAttributes();
attrs.setDecodingThreads(50); // 解码线程数
attrs.setEncodingThreads(50); // 编码线程数
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
encoder.encode(new MultimediaObject(source), target, attrs);
}catch (EncoderException e){
e.printStackTrace();
}
}
}