使用javacv 截取视频指定帧节
个人博客 地址:https://www.wenhaofan.com/article/20190407105818
引入依赖
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.3</version>
</dependency> <dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.0.2-1.4.3</version>
</dependency>
代码案例
package live.autu.ctcms.common.kit; import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import javax.imageio.ImageIO; import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_core.IplImage;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter; import com.jfinal.kit.PathKit;
import com.jfinal.kit.Ret; public class VideoFrameUtil { private static String rootPath = PathKit.getWebRootPath() + "/video/img";// 服务器路径 private static final String IMAGEMAT = "png"; // 图片格式
private static final String ROTATE = "rotate";
public static final int MOD = 1;// 第一帧 public static Ret getVideoFirstFrame(String videoPath, String imageName) {
return getVideoFrame(videoPath, imageName, MOD);
} /**
* 获取视频缩略图
*
* @param lpath
* @param filePath:视频路径
* @param mod:视频长度/mod获取第几帧
* @throws Exception
*/
public static Ret getVideoFrame(String filePath, String targetImageName, int mod) { // 根据视频路径生成缩略图存放路径
String targetFilePath = null; try (FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);) { ff.start(); String rotate = ff.getVideoMetadata(ROTATE);
int ffLength = ff.getLengthInFrames();
Frame f = null;
int i = 0;
int index = mod; if (i >= ffLength) {
return Ret.fail();
} for (int j = 0; j <= index; j++) {
f = ff.grabImage();
} if (null != rotate && rotate.length() > 1) {
OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
IplImage src = converter.convert(f);
f = converter.convert(rotate(src, Integer.valueOf(rotate))); // 旋转图片
} targetFilePath = doExecuteFrame(f, targetImageName);
ff.stop();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return Ret.ok("targetFilePath",targetFilePath)
.set("relativeFilePath", "/video/img/"+targetImageName); // 返回的是视频第一帧
} /**
* 旋转图片
*
* @param src
* @param angle
* @return
*/
public static IplImage rotate(IplImage src, int angle) {
IplImage img = IplImage.create(src.height(), src.width(), src.depth(), src.nChannels());
opencv_core.cvTranspose(src, img);
opencv_core.cvFlip(img, img, angle);
return img;
} /**
* 根据视频路径生成缩略图存放路径
*
* @param filePath:视频路径
* @param index:第几帧
* @return:缩略图的存放路径
*/
private static String getImagePath(String fileName) {
return rootPath+"/" + fileName;
} /**
* 截取缩略图
*
* @param f
* @param targerFilePath:封面图片
* @throws IOException
*/
public static String doExecuteFrame(Frame f, String lpath) throws IOException { if(!new File(rootPath).exists()) {
new File(rootPath).mkdirs();
} String targerFilePath = getImagePath(lpath);
if (null == f || null == f.image) {
return null;
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bi = converter.getBufferedImage(f);
File output = new File(targerFilePath); output.createNewFile();
ImageIO.write(bi, IMAGEMAT, output); return targerFilePath;
} /**
* 根据视频长度随机生成随机数集合
*
* @param baseNum:基础数字,此处为视频长度
* @param length:随机数集合长度
* @return:随机数集合
*/
public static List<Integer> random(int baseNum, int length) {
List<Integer> list = new ArrayList<Integer>(length);
while (list.size() < length) {
Integer next = (int) (Math.random() * baseNum);
if (list.contains(next)) {
continue;
}
list.add(next);
}
Collections.sort(list);
return list;
} }
使用javacv 截取视频指定帧节的更多相关文章
- 【Java SE】使用Javacv截取视频图片
[Java Web开发学习]使用Javacv截取视频图片 转载:https://www.cnblogs.com/yangchongxing/p/9482935.html github地址:https: ...
- vue 截取视频第一帧
最近自己写项目,在项目中涉及功能点又截取视频帧的点:需求澄清:移动端封面展示,平台上传图片(多张上传)取第一张上传图片为封面图:如上传视频则截取视频第一帧作为封面图: 实现思路:h5 video标签 ...
- php ffmpeg截取视频第一帧保存为图片的方法
php ffmpeg截取视频第一帧保存为图片的方法 <pre> $xiangmupath = $this->getxiangmupath(); $filename = 'chengs ...
- Java截取视频首帧并旋转正向
package test; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import j ...
- javacv获取视频第一帧
第一种是用ffmpeg工具,不过还得安装客户端软件,于是放弃了,还有一种是javacv开源工具,所以选择第二种: 第一种:ffmpeg工具 需要安装ffmpeg软件,支持windows和linux,视 ...
- php如何截取出视频中的指定帧作为图片
php如何截取出视频中的指定帧作为图片 一.总结 一句话总结:截取视频指定帧为图片,php ffmpeg扩展已经完美实现,并且php ffmpeg是开源的 二.php如何截取出视频中的指定帧作为图片 ...
- FFmpeg 入门(1):截取视频帧
本文转自:FFmpeg 入门(1):截取视频帧 | www.samirchen.com 背景 在 Mac OS 上如果要运行教程中的相关代码需要先安装 FFmpeg,建议使用 brew 来安装: // ...
- javacv——读取摄像头的图像、截取视频的画面
javacv开发包是用于支持java多媒体开发的一套开发包,可以适用于本地多媒体(音视频)调用以及音视频,图片等文件后期操作(图片修改,音视频解码剪辑等等功能). 这些需要引入的包.音视频处理使用ff ...
- python-----opencv截取按帧截取视频
最近有需求把一个视频从指定帧截取一部分,demo代码如下: import cv2 video_path = r'F:\temp\temp_0806\1\video\test.dat' videoCap ...
随机推荐
- pytorch之 activation funcion
import torch import torch.nn.functional as F from torch.autograd import Variable import matplotlib.p ...
- ibtmp1文件过大
有个数据库发现磁盘告警 已经100% 经过排查发现数据库的data目录下有个 ibtmp1是个什么东西呢?查看官方文档后发现 The temporary tablespace is a tablesp ...
- [MacOS]Chrome 强制刷新
Chrome 调试的时候经常发现缓存没有清理 MacOS :⌘+⇧+r Windows:Ctrl + F5
- 杭电1007-----C语言实现
这道题花了好久的时间才做出来,刚开始没有思路,最后看了网上的解答,好难得样子,每次都没有看完,但是掌握了大概思想,今天试着做了一下,已ac 主要思想:先将点对按照x排序,再在x排好序的基础上按照y来排 ...
- Day7前端学习之路——多栏布局
该文章主要讨论两栏布局和三栏布局,三栏布局包括很著名的圣杯布局和双飞翼布局 一.两栏布局的七种方法(左边固定,右边自适应) 原理: block水平元素宽度能够跟随父容器调节的流动特性,block级别的 ...
- typescript点滴
typescript比较优质的学习资料都放在前端资料汇总 这些都是为了自己更好的理解和记忆而不是为了学术性的博客. 这里讲的是一些容易忘记的.混淆的. #### typescript的模块的导入3种方 ...
- linux 下 go 语言环境搭建
1.首先去官网下载安装包 https://studygolang.com/dl 选择合适的安装包并下载解压 wget https://studygolang.com/dl/golang/go1.13. ...
- Hibernate入门之创建数据库表
前言 Hibernate 5.1和更早版本至少需要Java 1.6和JDBC 4.0,Hibernate 5.2和更高版本至少需要Java 1.8和JDBC 4.2,从本节开始我们正式进入Hibern ...
- Oracle行结果合计的实现
Oracle行结果合计的实现,主要应用于日期结果的集计,下面是具体的实现代码. With AA as ( select 'A' tNo , 10 B from dual union select ' ...
- python第三方库的安装pip的使用与换源(解决pip下载速度慢)
python和其他语言一样,也有大量的第三方库 在安装python时默认都会安装pip,安装了pip后 在cmd.exe下可以运行pip 安装库 pip install 库的名字 换源 因为PyPi地 ...