java如何截取视频文件,Java文件上传截取视频首图

依赖

org.bytedeco

javacpp

1.4.1

org.bytedeco

javacv

1.4.1

org.bytedeco.javacpp-presets

opencv-platform

3.4.1-1.4.1

org.bytedeco.javacpp-presets

ffmpeg-platform

3.4.2-1.4.1

关于文件的操作的类(包含截取第一帧,详情看注释)

/**

*

* @ClassName: FileUtil

* @Description: 文件管理

* @author: Zing

* @date: 2019年12月11日 上午11:55:40

* @Copyright:

*/

public class FileUtil {

/**

*

* @Title: getTempPath

* @Description: 生成视频的首帧图片方法

* @author: Zing

* @param: @param tempPath 生成首帧图片的文件地址

* @param: @param filePath 传进来的线上文件

* @param: @return

* @param: @throws Exception

* @return: boolean

* @throws

*/

public static boolean getTempPath(String tempPath, String filePath) throws Exception {

File targetFile = new File(tempPath);

if (!targetFile.getParentFile().exists()) {

targetFile.getParentFile().mkdirs();

}

File file2 = new File(filePath);

//判断文件是否为视频

if(FileUtil.isVideo("filePath")) {

if (file2.exists()) {

FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file2);

ff.start();

int ftp = ff.getLengthInFrames();

int flag=0;

Frame frame = null;

while (flag <= ftp) {

//获取帧

frame = ff.grabImage();

//过滤前3帧,避免出现全黑图片

if ((flag>3)&&(frame != null)) {

break;

}

flag++;

}

if(ImageIO.write(FrameToBufferedImage(frame), "jpg", targetFile)) {

ff.close();

ff.stop();

return true;

}else {

ff.close();

ff.stop();

return false;

}

}

}

return false;

}

/***

*

* @Title: FrameToBufferedImage

* @Description: 创建格式化BufferedImage对象

* @author: Zing

* @param: @param frame

* @param: @return

* @return: RenderedImage

* @throws

*/

private static RenderedImage FrameToBufferedImage(Frame frame) {

//创建BufferedImage对象

Java2DFrameConverter converter = new Java2DFrameConverter();

BufferedImage bufferedImage = converter.getBufferedImage(frame);

return bufferedImage;

}

/**

*

* @Title: isVideo

* @Description:判断是不是视频

* @author: Zing

* @param: @param path 文件路径

* @param: @return

* @return: boolean true是视频 false非视频

* @throws

*/

public static boolean isVideo(String path) {

List typeList = new ArrayList();

typeList.add("mp4");

typeList.add("flv");

typeList.add("avi");

typeList.add("rmvb");

typeList.add("rm");

typeList.add("wmv");

//获取文件名和后缀

String suffix = path.substring(path.lastIndexOf(".") + 1);

for(String type : typeList) {

if(type.toUpperCase().equals(suffix.toUpperCase())) {

return true;

}

}

return false;

}

/**

*

* @Title: isVideo

* @Description: 判断是不是图片

* @author: Zing

* @param: @param path 文件路径

* @param: @return

* @return: boolean true是图片 false非图片

* @throws

*/

public static boolean isImage(String path) {

List typeList = new ArrayList();

typeList.add("jpg");

typeList.add("png");

//获取文件名和后缀

String suffix = path.substring(path.lastIndexOf(".") + 1);

for(String type : typeList) {

if(type.toUpperCase().equals(suffix.toUpperCase())) {

return true;

}

}

return false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值