现在的位置: 首页 > 编程语言 > 正文

javacv视频抽帧的实现过程详解(附代码)

2020年02月14日 编程语言 ⁄ 共 1544字 ⁄ 字号 评论关闭

视频抽帧可以做一些处理,比如水印,去水印等操作,然后再合成视频。下面直接上代码:

引入maven步骤看javacv去水印的文章

这里直接上关键操作:

/** * 视频文件指定时间段的帧截取 * @param file * @param start * @param end */ public static List<File> videoIntercept(File file, Integer start, Integer end) { Frame frame = null; List<File> files = Lists.newArrayList(); FFmpegFrameGrabber fFmpegFrameGrabber = new FFmpegFrameGrabber(file); String filePath = "D://video//images//"; String fileTargetName = "movie"; try { fFmpegFrameGrabber.start(); int ftp = fFmpegFrameGrabber.getLengthInFrames(); System.out.println("开始视频提取帧"); for (int i=0 ; i < ftp ; i++){ if( i >= start && i <= end){ frame = fFmpegFrameGrabber.grabImage(); doExecuteFrame(frame, filePath, fileTargetName, i ,files); } } System.out.println("============运行结束============"); fFmpegFrameGrabber.stop(); } catch (IOException E) {// Loggers.ERROR.error("视频抽帧异常", e); } return files; } public static void doExecuteFrame(Frame frame, String targetFilePath, String targetFileName, int index ,List<File> files) { if ( frame == null || frame.image == null) { return; } Java2DFrameConverter converter = new Java2DFrameConverter(); String imageMat = "jpg"; String fileName = targetFilePath + targetFileName + "_" + index + "." + imageMat; BufferedImage bi = converter.getBufferedImage(frame); File output = new File(fileName); files.add(output); try{ ImageIO.write(bi, imageMat, output); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { List<File> files = videoIntercept(new File("D://video//1553583033205-480p.mp4"), 10, 20); System.out.println(files); }

我们可以看到文件夹下抽取了视频的第10,20之间的帧。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: javacv视频抽帧的实现过程详解(附代码)

以上就上有关javacv视频抽帧的实现过程详解(附代码)的相关介绍,要了解更多java,javacv,视频抽帧内容请登录学步园。

抱歉!评论已关闭.