support add media thumb attachment
This commit is contained in:
42
src/main/java/com/imyeyu/api/util/JavaCV.java
Normal file
42
src/main/java/com/imyeyu/api/util/JavaCV.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.imyeyu.api.util;
|
||||
|
||||
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.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-10-23 17:05
|
||||
*/
|
||||
public class JavaCV {
|
||||
|
||||
public static ByteArrayOutputStream captureThumbnail(InputStream stream, double targetSeconds) throws Exception {
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(stream)) {
|
||||
grabber.start();
|
||||
long targetMillis = (long) (targetSeconds * 1000);
|
||||
grabber.setTimestamp(targetMillis);
|
||||
Frame frame;
|
||||
while ((frame = grabber.grabImage()) != null) {
|
||||
if (grabber.getTimestamp() >= targetMillis) {
|
||||
Java2DFrameConverter converter = new Java2DFrameConverter();
|
||||
try (converter) {
|
||||
BufferedImage bi = converter.getBufferedImage(frame);
|
||||
if (bi != null) {
|
||||
ImageIO.write(bi, "png", outStream);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return outStream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user