java上传视频并且截取当前视频第一帧
java上传视频并且截取当前视频第一帧
1.Maven依赖
<!--截取视频第一帧-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
<version>3.4.1-1.4.1</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>3.4.2-1.4.1</version>
</dependency>
2.FileImageUtil
/** * Created by ls on 2020/7/15. */
**
* Created by ls on 2020/7/15.
*/
public class FileImageUtil {
/** * 获取指定视频的帧并保存为图片至指定目录 * @param filePath 视频存放的地址 例如:D:/ruoyi/uploadPath/avatar/123.mp4 * @param targetFilePath 截图存放的地址 D:/ruoyi/uploadPath/images * @param targetFileName 截图保存的文件名称 123 * @return 图片的地址 * @throws Exception */
public static String executeCodecs(String filePath, String targetFilePath, String targetFileName) throws Exception {
try{
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
ff.start();
String rotate =ff.getVideoMetadata("rotate");
Frame f;
int i = 0;
String fileName=null;
while (i <1) {
f =ff.grabImage();
IplImage src = null;
if(null !=rotate &&rotate.length() > 1) {
OpenCVFrameConverter.ToIplImage converter =new OpenCVFrameConverter.ToIplImage();
src =converter.convert(f);
f =converter.convert(rotate(src, Integer.valueOf(rotate)));
}
fileName= doExecuteFrame(f, targetFilePath, targetFileName);
i++;
}
ff.stop();
return fileName;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/* * 旋转角度的 */
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;
}
public static String doExecuteFrame(Frame f, String targetFilePath, String targetFileName) {
if (null ==f ||null ==f.image) {
return null;
}
Java2DFrameConverter converter =new Java2DFrameConverter();
String imageMat ="jpg";
String fileName =targetFilePath + "/" +targetFileName +"." +imageMat;
BufferedImage bi =converter.getBufferedImage(f);
System.out.println("width:" + bi.getWidth());//打印宽、高
System.out.println("height:" + bi.getHeight());
File file =new File(fileName);
try {
ImageIO.write(bi,imageMat,file);
}catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
}
还没有评论,来说两句吧...