1、使用jave获取视频时长
mvn install:install-file -Dfile=E:jave-1.0.2.jar -DgroupId=it.sauronsoftware -DartifactId=jave -Dversion=1.0.2 -Dpackaging=jar
命令说明:mvn install:install-file -Dfile=jar包的位置 -DgroupId=包的groupId -DartifactId=包的artifactId -Dversion=包的version -Dpackaging=jar
然后pom.xml中引入依赖
it.sauronsoftware
jave
1.0.2
代码(摘自网络)
package com.eya.video;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import java.io.File;
/**
*
* description
*
*
* @author ll
* @date 06/11/20 10:23
*/
public class VideoTimeReader {
public static void main(String[] args) {
System.out.println(readTime(new File("E:\personal\video\855558d1dcc2ad522c548cefe6a67bd8.mp4")));
}
// 获取视频时长
private static String readTime(File source) {
Encoder encoder = new Encoder();
String length = "";
try {
MultimediaInfo m = encoder.getInfo(source);
long ls = m.getDuration() / 1000;
int hour = (int) (ls / 3600);
int minute = (int) (ls % 3600) / 60;
int second = (int) (ls - hour * 3600 - minute * 60);
length = hour + "小时" + minute + "分" + second + "秒";
} catch (
Exception e) {
e.printStackTrace();
}
return length;
}
}
2、使用javacv-platform和javacv截取视频帧作为视频封面
引入jar包,注意这2个包的版本需要匹配,不然会出错
org.bytedeco
javacv-platform
1.4.1
org.bytedeco
javacv
1.4.1
编写java代码
package com.eya.video;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
/**
*
* description
*
*
* @author ll
* @date 06/04/20 10:29
*/
public class VideoCoverCut {
/**
* 截取视频第六帧的图片
*
* @param filePath 视频路径
* @param dir 文件存放的根目录
* @return 图片的相对路径 例:pic/1.png
*/
public static String videoImage(String filePath, String dir) throws Exception {
String pngPath = "";
// FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(new File(filePath));
ff.start();
int ffLength = ff.getLengthInFrames();
Frame f;
int i = 0;
while (i < ffLength) {
f = ff.grabFrame();
//截取第6帧
if ((i > 5) && (f.image != null)) {
//生成图片的相对路径 例如:pic/uuid.png
pngPath = getPngPath();
//执行截图并放入指定位置
System.out.println("存储图片 : " + (dir + pngPath));
doExecuteFrame(f, dir + pngPath);
break;
}
i++;
}
ff.stop();
return pngPath;
}
/**
* 生成图片的相对路径
*
* @return 图片的相对路径 例:pic/1.png
*/
private static String getPngPath() {
return getUUID() + ".png";
}
/**
* 生成唯一的uuid
*
* @return uuid
*/
private static String getUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
/**
* 截取缩略图
*
* @param f Frame
* @param targerFilePath:封面图片存放路径
*/
private static void doExecuteFrame(Frame f, String targerFilePath) {
String imagemat = "png";
if (null == f || null == f.image) {
return;
}
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bi = converter.getBufferedImage(f);
File output = new File(targerFilePath);
try {
ImageIO.write(bi, imagemat, output);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
VideoCoverCut.videoImage("E:\personal\video\855558d1dcc2ad522c548cefe6a67bd8.mp4", "E:\");
} catch (Exception e) {
e.printStackTrace();
}
}
}