1.引入依赖
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv-platform</artifactId>
<version>4.1.2-1.5.2</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.2.1-1.5.2</version>
</dependency>
2.封装工具类获取
public class FileUtil {
public static class UploadPath {
public static final String ChattingPath = "";
}
public enum UpLoadType {
CHATTING_IMAGE("聊天图片上传"),
CHATTING_AUDIO("聊天音频上传"),
CHATTING_VIDEO("聊天视频上传");
private String description;
UpLoadType(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public String uploadPath(UpLoadType upLoadType) {
String path = null;
switch (upLoadType) {
case CHATTING_IMAGE:
break;
case CHATTING_AUDIO:
break;
case CHATTING_VIDEO:
break;
}
return path;
}
public enum Type {
Images("图片"),
Audio("音频"),
Video("视频"),
File("文件");
private String description;
Type(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public static VideoInfo getVideoInfo(File targetFile) {
VideoInfo videoInfo = new VideoInfo();
Encoder encoder = new Encoder();
try {
it.sauronsoftware.jave.MultimediaInfo multimediaInfo = encoder.getInfo(targetFile);
videoInfo.setVideo_duration(duration(multimediaInfo.getDuration()));
videoInfo.setVideo_width(multimediaInfo.getVideo().getSize().getWidth());
videoInfo.setVideo_height(multimediaInfo.getVideo().getSize().getHeight());
videoInfo.setFormat(multimediaInfo.getFormat());
InputStream inputStream = new FileInputStream(targetFile);
MultipartFile multipartFile = new MockMultipartFile(targetFile.getName(), inputStream);
videoInfo.setSize(multipartFile.getSize());
byte[] frame = frame(targetFile);
videoInfo.setVideo_frame(frame);
} catch (EncoderException | IOException e) {
e.printStackTrace();
}
return videoInfo;
}
private static byte[] frame(File targetFile) {
byte[] content = null;
try {
FFmpegFrameGrabber ff = new FFmpegFrameGrabber(targetFile);
ff.start();
int ftp = ff.getLengthInFrames();
int flag = 0;
Frame frame = null;
while (flag <= ftp) {
frame = ff.grabImage();
if ((flag > 3) && (frame != null)) {
break;
}
flag++;
}
RenderedImage renderedImage = frameToBufferedImage(frame);
String rotate = ff.getVideoMetadata("rotate");
if (rotate != null) {
renderedImage = rotate((BufferedImage) renderedImage, Integer.parseInt(rotate));
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(renderedImage, "jpg", os);
ff.close();
ff.stop();
content = os.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
private static RenderedImage frameToBufferedImage(Frame frame) {
Java2DFrameConverter converter = new Java2DFrameConverter();
return converter.getBufferedImage(frame);
}
public static BufferedImage rotate(BufferedImage src, int angel) {
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
int type = src.getColorModel().getTransparency();
Rectangle rect_des = calcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel);
BufferedImage bi = new BufferedImage(rect_des.width, rect_des.height, type);
Graphics2D g2 = bi.createGraphics();
g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2);
g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
g2.drawImage(src, 0, 0, null);
g2.dispose();
return bi;
}
private static Rectangle calcRotatedSize(Rectangle src, int angel) {
if (angel >= 90) {
if (angel / 90 % 2 == 1) {
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
}
double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
double angel_dalta_width = Math.atan((double) src.height / src.width);
double angel_dalta_height = Math.atan((double) src.width / src.height);
int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width));
int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height));
int des_width = src.width + len_dalta_width * 2;
int des_height = src.height + len_dalta_height * 2;
return new java.awt.Rectangle(new Dimension(des_width, des_height));
}
private static String duration(Long ms) {
String duration = null;
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
String strHour = hour < 10 ? "0" + hour : "" + hour;
String strMinute = minute < 10 ? "0" + minute : "" + minute;
String strSecond = second < 10 ? "0" + second : "" + second;
if (strHour.equals("00")) {
duration = strMinute + ":" + strSecond;
} else {
duration = strHour + ":" + strMinute + ":" + strSecond;
}
return duration;
}
}