所需要的jar
1、mov 视频转码为 mp4
compile group: 'ws.schild', name: 'jave-core', version: '2.7.1'
compile group: 'ws.schild', name: 'jave-nativebin-linux64', version: '2.7.1'
compile group: 'ws.schild', name: 'jave-nativebin-win64', version: '2.7.1'
2、获取时长、截取帧数
compile group: 'org.bytedeco', name: 'javacv', version: '1.3.1'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg-platform', version: '3.2.1-1.3'
/**
* 作者: 王彦宝
* 时间: 2019年6月27日上午10:48:58
* 描述:截取视频上传第10帧
* @param file
* @param framefile
* @throws Exception
* void
*/
public long fetchFrame(String file, String framefile)
throws Exception {
long start = System.currentTimeMillis();
int select = 10;
long duration = 0L;
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(file);
grabber.start();
int length = grabber.getLengthInFrames();
duration = grabber.getLengthInTime() / (1000 * 1000);// 获取时长
int index = 1;
Frame frame = null;
while (index <= length) {
frame = grabber.grabImage();
if (index >= select) {
break;
}
index++;
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bi = converter.getBufferedImage(frame);
ImageIO.write(bi, "jpg", new File(framefile));
grabber.stop();
System.out.println(System.currentTimeMillis() - start);
return duration;
}
//视频转码 sourcePath 视频来源 targetPath 目标地址
private void movToMp4(String sourcePath, String targetPath) {
File source = new File(sourcePath);
File target = new File(targetPath);
AudioAttributes audio = new AudioAttributes();
audio.setCodec("aac");
VideoAttributes video = new VideoAttributes();
video.setCodec("h264");
video.setBitRate(new Integer(2325 * 1024));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("mp4");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder();
try {
encoder.encode(new MultimediaObject(source), target, attrs);
} catch (IllegalArgumentException | EncoderException e) {
e.printStackTrace();
}
}
3、图片压缩jar
compile group: 'net.coobird', name: 'thumbnailator', version: '0.4.8'
/**
* 作者: 王彦宝
* 描述: 图片压缩返回字节数组
* @param inputStream
* @param width
* @param height
* @param newFName
* @return
* byte[]
*/
public byte[] resize(InputStream inputStream, int width, int height, String newFName) {
byte[] bt = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Thumbnails.of(inputStream).width(width).outputQuality(0.9).toOutputStream(out);
bt = out.toByteArray();
} catch (Exception e1) {
logger.info("filesUpload:" + e1.getMessage());
}
return bt;
}
/**
* 作者: 王彦宝
* 描述:图片压缩并裁剪返回字节数组
* @param inputStream
* @param width
* @param height
* @return
* byte[]
*/
public byte[] resize2(ByteArrayInputStream inputStream, int width, int height) {
byte[] bt = null;
try {
BufferedImage image = ImageIO.read(inputStream);
Builder<BufferedImage> builder = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int imageWidth = image.getWidth();
int imageHeitht = image.getHeight();
if ((float)height / width != (float)imageWidth / imageHeitht) {
inputStream.reset();
if (imageWidth > imageHeitht) {
image = Thumbnails.of(inputStream).height(height).asBufferedImage();
} else {
image = Thumbnails.of(inputStream).width(width).asBufferedImage();
}
builder = Thumbnails.of(image).sourceRegion(Positions.CENTER, width, height).size(width, height);
} else {
builder = Thumbnails.of(image).size(width, height);
}
builder.outputQuality(0.95f).outputFormat("jpg").toOutputStream(out);
bt = out.toByteArray();
} catch (Exception e1) {
logger.info("filesUpload:" + e1.getMessage());
}
return bt;
}
来源:oschina
链接:https://my.oschina.net/u/3267498/blog/3197352