效果视频
美女屏幕跳舞视频
本功能实现需要用到第三方jar包 javaCV。
javaCV简介
JavaCV 提供了在计算机视觉领域的封装库,包括:OpenCV、ARToolKitPlus、libdc1394 2.x 、PGR FlyCapture和FFmpeg。此外,该工具可以很容易地使用Java平台的功能。
JavaCV 还带有硬件加速的全屏幕图像显示(CanvasFrame),易于在多个内核中执行并行代码(并行),用户友好的几何和色彩的相机和投影仪校准(GeometricCalibrator,ProCamGeometricCalibrator,ProCamColorCalibrator ),检测和特征点(ObjectFinder),一类是实现投影,摄像系统(直接图像对齐设置匹配主要GNImageAligner,ProjectiveTransformer,ProjectiveGainBiasTransformer,ProCamTransformer 和ReflectanceInitializer),以及在 JavaCV 类杂项功能。
spring boot项目pom文件中添加以下依赖
-
<!-- https://mvnrepository.com/artifact/org.bytedeco/javacv-platform -->
-
<dependency>
-
<groupId>org.bytedeco
</groupId>
-
<artifactId>javacv-platform
</artifactId>
-
<version>1.5.5
</version>
-
</dependency>
Java单类实现代码,创建Spring boot项目,再项目中新建JavaCV 类,复制粘贴下面代码, 用idea编辑器 主方法运行即可。共有两个方法
videoToImages()
将视频按帧数分成图片
imagesToVideo()
将图片合成视频
-
package com.tarzan.reptile.demo;
-
-
import org.bytedeco.ffmpeg.global.avcodec;
-
import org.bytedeco.javacv.FFmpegFrameGrabber;
-
import org.bytedeco.javacv.FFmpegFrameRecorder;
-
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;
-
-
public
class JavaCV {
-
final
static String imagePath =
"C:/Users/tarzan/Desktop/image/";
-
final
static String videoPath =
"C:/Users/tarzan/Desktop/video/";
-
final
static String videoName =
"video.mp4";
-
final
static String imageMat =
"png";
-
-
-
public static void main(String[] args) throws Exception {
-
//视频按帧数分成图片
-
videoToImages(videoPath + videoName, imagePath + videoName.substring(
0, videoName.lastIndexOf(
".")));
-
//图片合成视频
-
imagesToVideo(videoPath +
"newVideo.mp4", imagePath);
-
}
-
-
-
public static void videoToImages(String filePath, String fileTargetName) throws Exception {
-
FFmpegFrameGrabber ff = FFmpegFrameGrabber.createDefault(filePath);
-
ff.start();
-
System.out.println(
"视频编码:" + ff.getVideoCodec());
-
System.out.println(
"像素格式:" + ff.getPixelFormat());
-
System.out.println(
"帧速率:" + ff.getFrameRate());
-
System.out.println(
"比特率:" + ff.getVideoBitrate());
-
System.out.println(
"图片高度:" + ff.getImageHeight());
-
System.out.println(
"图片宽度:" + ff.getImageWidth());
-
int grabSize = ff.getLengthInFrames();
-
for (
int i =
0; i < grabSize; i++) {
-
Frame frame = ff.grabImage();
-
doExecuteFrame(frame, fileTargetName, i);
-
}
-
ff.stop();
-
}
-
-
public static void doExecuteFrame(Frame frame, String targetFileName, int index) {
-
if (frame ==
null || frame.image ==
null) {
-
return;
-
}
-
Java2DFrameConverter converter =
new Java2DFrameConverter();
-
String fileName = targetFileName +
"/" + index +
"." + imageMat;
-
BufferedImage bi = converter.getBufferedImage(frame);
-
File output =
new File(fileName);
-
if (!output.exists()) {
-
output.mkdirs();
-
}
-
try {
-
ImageIO.write(bi, imageMat, output);
-
}
catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
-
public static void imagesToVideo(String saveMp4name, String imagesPath) throws Exception {
-
FFmpegFrameRecorder recorder =
new FFmpegFrameRecorder(saveMp4name,
640,
480);
-
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
// 13
-
recorder.setFormat(
"mp4");
-
recorder.setFrameRate(
24.148380638004276);
-
recorder.setPixelFormat(
0);
-
recorder.setVideoBitrate(
846472);
-
recorder.setImageHeight(
736);
-
recorder.setImageWidth(
544);
-
recorder.start();
-
// 列出目录中所有的图片
-
File file =
new File(imagesPath + videoName.substring(
0, videoName.lastIndexOf(
".")));
-
File[] listFiles = file.listFiles();
-
for (
int i =
0; i < listFiles.length; i++) {
-
String imgPath = imagesPath +
"/" + videoName.substring(
0, videoName.lastIndexOf(
".")) +
"/" + i +
"." + imageMat;
-
System.out.println(imgPath);
-
Java2DFrameConverter frameConverter =
new Java2DFrameConverter();
-
BufferedImage image = ImageIO.read(
new File(imgPath));
-
recorder.record(frameConverter.convert(image));
-
}
-
recorder.stop();
-
recorder.release();
-
}
-
-
-
}
运行结果截图
在桌面建立video和image文件夹,将要转换的视频文件放入进去video文件夹里。
原视频文件
将视频按原帧数分割图片
测试结果
视频格式为mp4,大小约2.80MB,按帧数分离成316张图片,转化时间1s左右。
相关文章《震惊,java仅用30行代码就实现了视频转音频的批量转换》
转载:https://blog.csdn.net/weixin_40986713/article/details/116593539