您好,登錄后才能下訂單哦!
java獲取視頻文件第一幀,為了防止黑畫面,此例取第五幀,本例子采用maven形式
jdk采用1.8,日志自行添加即可,這里使用的是log4j2
<!-- log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<!-- 引入javacv-platform依賴,視頻處理 -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.2</version>
</dependency>
修改main方法中的視頻地址,運行即可查看到效果
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
/**
* <h6>功能:視頻文件處理工具類</h6>
*
*/
public class ToolVideo {
// main函數中slf4j無法正常顯示,這里暫時使用log4j2輸出日志,遷移到web項目時,建議使用slf4j
// private static final Logger LOG = LoggerFactory.getLogger(ToolVideo.class);
private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger();
// 獲取要取得的幀數
private static final int GET_FRAMES_LENGTH = 5;
public static void main(String[] args) {
Map<String, Object> screenshot = getScreenshot("C:\\Users\\電腦\\Videos\\錦溪古鎮形象片.mp4");
System.out.println(screenshot.toString());
}
/**
* <h6>功能:獲取一張視頻截圖并保存同名的jpg文件到視頻同目錄下</h6>
*
* @param filePath 視頻文件全路徑
* @return
*/
public static Map<String, Object> getScreenshot(String filePath) {
return getScreenshot(filePath, null);
}
/**
* <h6>功能:獲取一張視頻截圖并保存同名的jpg文件到指定目錄</h6>
*
* @param filePath 視頻文件地址
* @param saveImagePath 截圖的圖片存放路徑(絕對路徑,不包含文件名稱)
* @return
*/
public static Map<String, Object> getScreenshot(String filePath, String saveImagePath) {
LOG.info("視頻文件[{}]截圖開始", filePath);
Map<String, Object> result = new HashMap<String, Object>();
FFmpegFrameGrabber grabber;
try {
grabber = FFmpegFrameGrabber.createDefault(filePath);
// 第一幀圖片存儲位置(也是視頻路徑)
String targerFilePath = filePath.substring(0, filePath.lastIndexOf("\\"));
// 視頻文件名
String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
// 圖片名稱
String targetFileName = fileName.substring(0, fileName.lastIndexOf("."));
grabber.start();
// 視頻總幀數
int videoLength = grabber.getLengthInFrames();
Frame frame = null;
int i = 0;
while (i < videoLength) {
// 過濾前5幀,避免出現全黑的圖片,依自己情況而定(每循環一次取一幀)
frame = grabber.grabFrame();
if ((i > GET_FRAMES_LENGTH) && (frame.image != null)) {
break;
}
i++;
}
// 視頻旋轉度
String rotate = grabber.getVideoMetadata("rotate");
Java2DFrameConverter converter = new Java2DFrameConverter();
// 繪制圖片
BufferedImage bi = converter.getBufferedImage(frame);
if (rotate != null) {
// 旋轉圖片
bi = rotate(bi, Integer.parseInt(rotate));
}
// 圖片的類型
String imageMat = "jpg";
// 圖片的完整路徑
String imagePath = targerFilePath + File.separator + targetFileName + "." + imageMat;
if (null != saveImagePath && !"".equals(saveImagePath)) {
// 指定路徑
imagePath = saveImagePath + "." + imageMat;
}
// 創建文件
File output = new File(imagePath);
ImageIO.write(bi, imageMat, output);
// 拼接Map信息
result.put("videoLength", videoLength); // 視頻總幀數
result.put("videoWide", bi.getWidth()); // 視頻的寬
result.put("videoHigh", bi.getHeight());// 頻的高
long duration = grabber.getLengthInTime() / (1000 * 1000); // 此視頻時長(s/秒)
result.put("rotate", (null == rotate || "".equals(rotate))? "0" : rotate); // 視頻的旋轉度
result.put("format", grabber.getFormat()); // 視頻的格式
result.put("imgPath", output.getPath());
result.put("duration", duration);
grabber.stop();
LOG.info("視頻文件[{}]截圖結束,圖片地址為[{}]", filePath, imagePath);
} catch (IOException e) {
LOG.error("視頻信息幀數處理發生異常 [{}]", e.getMessage());
e.printStackTrace();
}
return result;
}
// ==================== private method ====================
/**
* <h6>功能:根據視頻旋轉度來調整圖片</h6>
*
* @param src 捕獲的圖像
* @param angel 視頻旋轉度
* @return BufferedImage
*/
private 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;
}
/**
* <h6>功能:計算圖片旋轉大小</h6>
*
* @param src 屏幕坐標中捕獲的矩形區域
* @param angel 視頻旋轉度
* @return
*/
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));
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。