- 開源項(xiàng)目 JAVE (Java Audio Video Encoder)
- 引入依賴
<dependency>
<groupId>ws.schild</groupId>
<artifactId>jave-all-deps</artifactId>
<version>2.7.2</version>
</dependency>
- 編碼測(cè)試
package test;
import java.io.File;
import ws.schild.jave.AudioAttributes;
import ws.schild.jave.AudioInfo;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.VideoAttributes;
import ws.schild.jave.VideoInfo;
import ws.schild.jave.VideoSize;
public class Test {
public static void main(String[] args) throws Exception {
File source = new File("d:/source.mp4");
File target = new File("d:/target.mp4");
System.out.println("源視頻信息");
MultimediaObject object = new MultimediaObject(source);
MultimediaInfo info = object.getInfo();
System.out.println("格式format:" + info.getFormat());
System.out.println("時(shí)長(zhǎng)duration:" + info.getDuration());
VideoInfo videoInfo = info.getVideo();
System.out.println("編碼decoder:" + videoInfo.getDecoder());
System.out.println("碼率bitRate:" + videoInfo.getBitRate());
System.out.println("幀率frameRate:" + videoInfo.getFrameRate());
VideoSize videoSize = videoInfo.getSize();
System.out.println("分辨率:" + videoSize.getWidth() + " x " + videoSize.getHeight());
AudioInfo audioInfo = info.getAudio();
System.out.println("編碼decoder:" + audioInfo.getDecoder());
System.out.println("碼率bitRate:" + audioInfo.getBitRate());
System.out.println("聲道channels:" + audioInfo.getChannels());
System.out.println("采樣率samplingRate:" + audioInfo.getSamplingRate());
// 視頻屬性
VideoAttributes video = new VideoAttributes();
video.setCodec("libx264");
video.setBitRate(256000); // 碼率:256kbps
video.setFrameRate(25); // 幀率:25fps
int width = 640; // 固定寬度
int height = width * videoSize.getHeight() / videoSize.getWidth();
height = height%2==0 ? height : height+1; // 視頻寬高必須是偶數(shù)
video.setSize(new VideoSize(width, height));
// 音頻屬性
AudioAttributes audio = new AudioAttributes();
audio.setCodec("aac");
audio.setBitRate(64000); // 碼率:64kbps
audio.setSamplingRate(24000); // 采樣率:24kHz
// 轉(zhuǎn)碼屬性
EncodingAttributes attributes = new EncodingAttributes();
attributes.setVideoAttributes(video);
attributes.setAudioAttributes(audio);
attributes.setFormat("mp4");
Encoder encoder = new Encoder();
encoder.encode(object, target, attributes);
System.out.println("轉(zhuǎn)碼完成:新分辨率:" + width + " x " + height);
}
}
- 測(cè)試結(jié)果
源視頻信息
格式format:mov
時(shí)長(zhǎng)duration:268280
編碼decoder:h264 (Main) (avc1 / 0x31637661)
碼率bitRate:500000
幀率frameRate:25.0
分辨率:768 x 432
編碼decoder:aac (LC) (mp4a / 0x6134706D)
碼率bitRate:96000
聲道channels:2
采樣率samplingRate:44100
轉(zhuǎn)碼完成:新分辨率:640 x 360