feat: 增加ttl 使用实例

This commit is contained in:
wyl
2023-07-23 23:22:31 +08:00
parent ec6035d80f
commit b6ec759ae5
10 changed files with 151 additions and 93 deletions
+66 -13
View File
@@ -8,30 +8,83 @@
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>javacv</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<javacv.version>1.5.9</javacv.version>
<system.windowsx64>windows-x86_64</system.windowsx64>
<system.linuxx64>linux-x86_64</system.linuxx64>
</properties>
<dependencies>
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv-480</artifactId>
<version>1.0.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.opencv</groupId>-->
<!-- <artifactId>opencv-480</artifactId>-->
<!-- <version>1.0.0-java1.8</version>-->
<!-- </dependency>-->
<!--JAVA使用javacv实现图片合成短视频,相关JAR包-->
<!-- <dependency>-->
<!-- <groupId>org.bytedeco</groupId>-->
<!-- <artifactId>javacv-platform</artifactId>-->
<!-- <version>1.5.9</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.5.9</version>
<version>${javacv.version}</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.9</version>
<artifactId>javacpp-platform</artifactId>
<version>${javacv.version}</version>
</dependency>
<!-- ffmpeg最小依赖包,必须包含上面的javacv+javacpp核心库 -->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>6.0-${javacv.version}</version>
<classifier>${system.windowsx64}</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>6.0-${javacv.version}</version>
<classifier>${system.linuxx64}</classifier>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.wyl.javacv.JavaCvTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<!-- 在执行 package 打包时,执行 assembly:single 进行 jar 包合并操作 -->
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -1,58 +1,59 @@
package com.wyl.javacv;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class ImageToVideoConverter {
public static void main(String[] args) throws IOException {
URL url = ClassLoader.getSystemResource("opencv_java480.dll");
System.load(url.getPath());
String imageFolderPath = "C:\\Users\\95981\\Desktop\\picture"; // 你的图片文件夹路径
String outputVideoPath = "C:\\Users\\95981\\Desktop\\vidoe\\video14.mp4"; // 输出视频文件路径
try {
Size frameSize = new Size(910, 910); // 设置视频帧的大小(根据你的图片大小进行调整)
int fps = 15; // 视频帧
int x = VideoWriter.fourcc('D', 'I', 'V', 'X');
VideoWriter videoWriter = new VideoWriter(outputVideoPath, x, fps, frameSize, true);
// 获取图片文件列表
File imageFolder = new File(imageFolderPath);
File[] imageFiles = imageFolder.listFiles();
if (imageFiles != null) {
for (File imageFile : imageFiles) {
if (imageFile.isFile()) {
// Mat image = Imgcodecs.imread(imageFile.getAbsolutePath());
InputStream fos = new FileInputStream(imageFile);
Mat image = Imgcodecs.imdecode(new MatOfByte(fos.readAllBytes()), Imgcodecs.IMREAD_COLOR);
if (!image.empty()) {
// 调整图片大小,以适应视频帧大小(可选,如果图片与视频帧大小相同,可省略)
// Imgproc.resize(image, image, frameSize);
videoWriter.write(image);
image.release();
}
}
}
videoWriter.release(); // 释放VideoWriter资源
System.out.println("Video created successfully!");
} else {
System.out.println("No image files found in the folder.");
}
} catch (Exception e) {
File file = new File(outputVideoPath);
file.deleteOnExit();
}
}
}
//package com.wyl.javacv;
//
//import org.apache.commons.io.IOUtils;
//import org.opencv.core.Core;
//import org.opencv.core.Mat;
//import org.opencv.core.MatOfByte;
//import org.opencv.core.Size;
//import org.opencv.imgcodecs.Imgcodecs;
//import org.opencv.videoio.VideoWriter;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//
//public class ImageToVideoConverter {
// public static void main(String[] args) throws IOException {
// System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// String imageFolderPath = "/home/wyl/project/aos/opencv/picture"; // 你的图片文件夹路径
//// String imageFolderPath = "E:\\桌面\\picture";
// String outputVideoPath = "video14.mp4"; // 输出视频文件路径
//// String outputVideoPath = "F:\\test\\video14.mp4"; // 输出视频文件路径
// try {
// Size frameSize = new Size(910, 910); // 设置视频帧的大小(根据你的图片大小进行调整)
// int fps = 15; // 视频帧率
// int x = VideoWriter.fourcc('D', 'I', 'V', 'X');
// VideoWriter videoWriter = new VideoWriter(outputVideoPath, x, fps, frameSize, true);
// // 获取图片文件列表
// File imageFolder = new File(imageFolderPath);
// File[] imageFiles = imageFolder.listFiles();
//
// if (imageFiles != null) {
// for (File imageFile : imageFiles) {
// if (imageFile.isFile()) {
//// System.out.println(imageFile.getAbsolutePath());
//// Mat image = Imgcodecs.imread(imageFile.getAbsolutePath());
// InputStream fos = new FileInputStream(imageFile);
// Mat image = Imgcodecs.imdecode(new MatOfByte(IOUtils.toByteArray(fos)), Imgcodecs.IMREAD_COLOR);
// if (!image.empty()) {
// // 调整图片大小,以适应视频帧大小(可选,如果图片与视频帧大小相同,可省略)
//// Imgproc.resize(image, image, frameSize);
// videoWriter.write(image);
// image.release();
// }
// }
// }
// videoWriter.release(); // 释放VideoWriter资源
// System.out.println("Video created successfully!");
// } else {
// System.out.println("No image files found in the folder.");
// }
// } catch (Exception e) {
// File file = new File(outputVideoPath);
// file.deleteOnExit();
// e.printStackTrace();
// }
//
// }
//}
@@ -1,5 +1,6 @@
package com.wyl.javacv;
import org.apache.commons.io.IOUtils;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameRecorder;
@@ -17,9 +18,11 @@ public class JavaCvTest {
public static void main(String[] args) throws Exception {
//合成的MP4 存放的地址路径 这里的路径并不会自动创建,需要手动提前创建好,否则会报错:Could not open 'null'
String mp4SavePath = "C:\\Users\\95981\\Desktop\\vidoe\\img2.mp4";
String mp4SavePath = "E:\\桌面\\vidoe\\img2.mp4";
// String img = "/home/wyl/project/aos/opencv/picture";
// String mp4SavePath = "/home/wyl/project/aos/opencv/video/video14.mp4"; // 输出视频文件路径
//图片存放的地址路径
String img = "C:\\Users\\95981\\Desktop\\picture";
String img = "E:\\桌面\\picture";
int width = 910;
int height = 910;
//读取所有图片
@@ -37,20 +40,17 @@ public class JavaCvTest {
private static void createMp4(String mp4SavePath, Map<Integer, File> imgMap, int width, int height) throws FrameRecorder.Exception {
//视频宽高最好是按照常见的视频的宽高 16:9 或者 9:16
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(mp4SavePath, width, height);
int bitrate = 1000000;
//设置视频编码层模式 import org.bytedeco.ffmpeg.global.avcodec;可能需要手动复制添加
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
//设置视频为25帧每秒
recorder.setFrameRate(15);
//设置视频图像数据格式 import org.bytedeco.ffmpeg.global.avutil;可能需要手动复制添加
recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
recorder.setVideoBitrate(bitrate);
// recorder.setVideoBitrate(100000000);
recorder.setFormat("mp4");
try {
recorder.start();
Java2DFrameConverter converter = new Java2DFrameConverter();
//录制一个22秒的视频,22秒为自定义的一个视频时间长度,图片少则在22秒内,多则到22秒停止
imgMap.entrySet().stream().forEach(e -> {
BufferedImage read = null;
Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 730 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.
+19
View File
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>JavaBasiceDemo</artifactId>
<groupId>com.wyl.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mapsturct</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
+1
View File
@@ -21,6 +21,7 @@
<module>mqtt</module>
<module>Socket</module>
<module>jna</module>
<module>mapsturct</module>
</modules>
<packaging>pom</packaging>
<properties>
-16
View File
@@ -21,22 +21,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.aivfo</groupId>-->
<!-- <artifactId>aivfo-dfs-client-spring-boot-starter</artifactId>-->
<!-- <version>1.0.0-SNAPSHOT</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-web</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.aivfo</groupId>
<artifactId>aivfo-kafka-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>