java利用javacv和ffmpeg截取视频提取视频封面生成缩略图,精减引用包

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java利用javacv和ffmpeg截取视频提取视频封面,精减引用包

废话少说,直接上代码:
package mons.util;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class VideoUtil {

static Logger log = LoggerFactory.getLogger(VideoUtil.class);
/**
* 获取视频时长,单位为秒
*
* @param video 源视频文件
* @return 时长(s)
*/
public static Long getVideoDuration(File video) {
Long duration = 0L;
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
try {
ff.start();
duration = ff.getLengthInTime() / (1000 * 1000);
ff.stop();
} catch (FrameGrabber.Exception e) {
e.printStackTrace();
}
return duration;
}


/**
* 截取视频获得指定帧的图片
*
* @param video 源视频文件
* @param picPath 截图存放路径
*/
public static void getVideoPic(File video, String picPath) {
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(video);
try {
ff.start();

// 截取中间帧图片(具体依实际情况而定)
int i = 0;
int length = ff.getLengthInFrames();
int middleFrame = length / 2;
org.bytedeco.javacv.Frame frame = null;
while (i < length) {
frame = ff.grabFrame();
if ((i > middleFrame) && (frame.image != null)) {
break;
}
i++;
}

// 截取的帧图片
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage srcImage = converter.getBufferedImage(frame);
int srcImageWidth = srcImage.getWidth();
int srcImageHeight = srcImage.getHeight();

// 对截图进行等比例缩放(缩略图)
int width = 480;
int height = (int) (((double) width / srcImageWidth) * srcImageHeight);
BufferedImage thumbnailImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
thumbnailImage.getGraphics().drawImage(srcImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

File picFile = new File(picPath);
ImageIO.write(thumbnailImage, "jpg", picFile);

ff.stop();
} catch (IOException e) {
e.printStackTrace();
}
}



public static void main(String[] args) {
String videoPath ="d:\\111.mp4";
// String audioPath = "d:\\20200222_161159.m4a";

File video = new File(videoPath);
String picPath = "d:\\video.jpg";
getVideoPic(video, picPath);

long duration = getVideoDuration(video);
System.out.println("videoDuration = " + duration);

}
}

网上一般说,maven引用如下:

org.bytedeco
javacv-platform
1.5.2


结果引用了一大堆包,超过500M,我特意花了不少时间,进行了精减,正确的maven引用如下:


org.bytedeco
javacv
1.5.2


org.bytedeco
javacpp


org.bytedeco
openblas


org.bytedeco
opencv


org.bytedeco
ffmpeg


org.bytedeco
flycapture


org.bytedeco
libdc1394


org.bytedeco
libfreenect


org.bytedeco
libfreenect2


org.bytedeco
librealsense


org.bytedeco
librealsense2


org.bytedeco
videoinput


org.bytedeco
artoolkitplus


org.bytedeco
flandmark



org.bytedeco
leptonica


org.bytedeco
tesseract



com.google.android
android



org.jogamp.gluegen
gluegen-rt-main


org.jogamp.jogl
jogl-all-main


org.jogamp.jocl
jocl-main




org.bytedeco
ffmpeg
4.2.1-1.5.2


org.bytedeco
javacpp




org.bytedeco
ffmpeg
4.2.1-1.5.2
windows-x86_64





注意ffmpeg包要区分操作系统,解开ffmpeg-platform.jar文件,在pom文件中可以找到所有的classifier。

相关文档
最新文档