<!-- JavaCV是一款基于JavaCPP调用方式(JNI的一层封装),由多种开源计算机视觉库组成的包装库,封装了包含FFmpeg、OpenCV、tensorflow、caffe、tesseract、libdc1394、OpenKinect、videoInput和ARToolKitPlus等在内的计算机视觉领域的常用库和实用程序类。 -->
<!-- FFmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。它提供了录制、转换以及流化音视频的完整解决方案。它包含了非常先进的音频/视频编解码库libavcodec,为了保证高可移植性和编解码质量,libavcodec里很多code都是从头开发的。-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.9</version>
</dependency>
/**
* 视频处理工具
*/
public class VideoUtil {
private static final String vttJsonStr = "{\"code\":200,\"use_time\":36.47124886512756,\"text\":\"今天中國銀行股價報收4.16元 掌幅0.0%成交量21.08% 售 成交額10.26億 主力進流入3.38.71萬今天中國銀行報收4.16元 上漲0.0% 上證指數下跌1.9億 行業指數下跌0.0% 在同行業中掌跌幅排名11今天最高價4.50元 最低價4.38元 政府2.69%今天中國銀行成交量21.0% 售 相比上個交易日 增加47.19%今天中國銀行主力資金 流入5.1517.52萬 流出4.8億18.80萬 進流入3.38.72萬 在所屬好業中主力資金進流入排名2\",\"data\":{\"vtt\":[\"WEBVTT\",\"\",\"00:00.000 --> 00:15.000\",\"今天中國銀行股價報收4.16元 掌幅0.0%成交量21.08% 售 成交額10.26億 主力進流入3.38.71萬\",\"\",\"00:15.000 --> 00:28.000\",\"今天中國銀行報收4.16元 上漲0.0% 上證指數下跌1.9億 行業指數下跌0.0% 在同行業中掌跌幅排名11\",\"\",\"00:29.000 --> 00:36.000\",\"今天最高價4.50元 最低價4.38元 政府2.69%\",\"\",\"00:36.000 --> 00:44.000\",\"今天中國銀行成交量21.0% 售 相比上個交易日 增加47.19%\",\"\",\"00:44.000 --> 00:59.000\",\"今天中國銀行主力資金 流入5.1517.52萬 流出4.8億18.80萬 進流入3.38.72萬 在所屬好業中主力資金進流入排名2\",\"\"]}}";
private static final String TEMPORARY_IMG_URL = "/data/upload/img/";
private static final String TEMPORARY_VIDEOTRANSCODE_URL = "/data/upload/video/";
private static final String TEMPORARY_VTT_URL = "/data/upload/vtt/";
/**
* main测试入口
*
* @param args
*/
public static void main(String[] args) throws IOException, InterruptedException {
VideoUtil.videoScreenshot("D:/data/daily_market_review.mp4");
VideoUtil.videoTranscode("D:/data/daily_market_review.mp4");
VttResp vttResp = JSONObject.parseObject(vttJsonStr, VttResp.class);
VideoUtil.vtt(vttResp.getData().getVtt());
}
/**
* 视频截图
*
* @param localVideoPath 视频文件本地路径
* @return
*/
public static void videoScreenshot(String localVideoPath) throws IOException {
File localFile = new File(localVideoPath);
// 检查本地视频文件是否存在
if (!localFile.exists()) {
return;
}
// 创建FFmpegFrameGrabber对象(使用try-with-resources语句确保FFmpegFrameGrabber在使用完后被正常关闭)
try (FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(localVideoPath)) {
// 启动视频抓取
grabber.start();
// 获取视频时长
long lengthInTime = grabber.getLengthInTime();
// 获取截图时间点
List<Long> times = random(lengthInTime);
// 对每个时间点进行截图
for (Long time : times) {
// 设置grabber的时间戳为当前时间点
grabber.setTimestamp(time);
// 抓取当前时间点的视频帧
Frame frame = grabber.grabImage();
// 创建Java2DFrameConverter对象,用于将帧转换成BufferedImage
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
// 生成唯一的图片文件名
String imgName = "img" + UUID.randomUUID().toString() + ".jpg";
// 在临时文件夹路径后追加图片文件名,得到完整的图片文件路径
String outputLocalImgPath = TEMPORARY_IMG_URL.concat(imgName);
// 创建对应路径的文件对象
File file = new File(outputLocalImgPath);
// 确保父文件夹存在
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 将BufferedImage写入图片文件
ImageIO.write(bufferedImage, "jpg", file);
}
}
}
/**
* 从1s开始截取,每隔5秒截取一张(最多截取8张)
*
* @param lengthInTime 视频时长
* @return 截图时间点列表
*/
private static List<Long> random(long lengthInTime) {
List<Long> times = new ArrayList<>();
long index = lengthInTime / (6 * 1000000L);
// 最多截取8张图
long num = Math.min(index + 1, 8);
for (int i = 0; i < num; i++) {
long time = (i * 5 + 1) * 1000000L;
times.add(time);
}
Collections.sort(times);
return times;
}
/**
* 视频转码
*
* @param localVideoPath 视频文件本地路径
* @throws IOException
* @throws InterruptedException
*/
public static void videoTranscode(String localVideoPath) throws IOException, InterruptedException {
// 创建本地视频文件对象
File localFile = new File(localVideoPath);
// 检查文件是否存在
if (!localFile.exists()) {
return;
}
// 使用FFmpegFrameGrabber读取视频信息
try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(localVideoPath)) {
grabber.start();
// 获取视频的宽度和高度
int width = grabber.getImageWidth();
int height = grabber.getImageHeight();
// 检查视频宽度和高度,若满足条件则进行转码
if (width >= 1280 && height >= 720) {
// 加载并获取FFmpeg可执行文件路径
String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
// 生成唯一的输出视频文件名
String videoName = "video" + UUID.randomUUID().toString() + ".mp4";
// 在临时文件夹路径后追加输出视频文件名,得到完整的输出视频文件路径
String outputLocalVideoPath = TEMPORARY_VIDEOTRANSCODE_URL.concat(videoName);
// 创建对应路径的文件对象
File file = new File(outputLocalVideoPath);
// 确保父文件夹存在
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 构建FFmpeg命令行进程构建器(按照720x480分辨率进行转码)
ProcessBuilder processBuilder = new ProcessBuilder(ffmpeg, "-i", localVideoPath, "-s", "720x480", outputLocalVideoPath);
// 启动进程并等待转码完成
Process process = processBuilder.inheritIO().start();
int exitCode = process.waitFor();
// 判断转码结果
if (exitCode == 0) {
System.out.println("视频转换成功!");
} else {
System.err.println("视频转换失败!");
}
}
}
}
/**
* 根据传入参数生成vtt字幕文件
*
* @param vtt 字幕数组,eg:["WEBVTT","","00:00.000 --> 00:03.200","最近家族信拖这个概念越来越火","","00:03.200 --> 00:06.000","几乎成了高静直客户的标配选择","","00:06.000 --> 00:07.600","那家族信拖是什么","","00:07.600 --> 00:08.960","为什么活起来了呢","","00:08.960 --> 00:10.560","今天就给大家讲明白","","00:10.560 --> 00:12.800","家族信拖我们打个比方","","00:12.800 --> 00:13.920","就是很好理解了","","00:13.920 --> 00:16.320","假设我是一个身家过一的老板","","00:16.320 --> 00:18.560","犀下有一个儿子和一个女儿","","00:18.560 --> 00:21.280","我希望我的儿子能继承我的公司","","00:21.280 --> 00:22.720","将产业发言光大","","00:22.720 --> 00:23.680","而我的女儿","","00:23.680 --> 00:25.360","和更希望他价格好人价","","00:25.360 --> 00:27.040","过的依食无忧就好了","","00:27.040 --> 00:28.880","那么我就可以找到信拖公司","","00:28.880 --> 00:30.960","将我的财产能为拖给他们","","00:30.960 --> 00:32.880","按照我的分配意愿传承","","00:32.880 --> 00:34.080","在我过世之后","","00:34.080 --> 00:37.600","就可以根据信拖约定来执行财产的分配了","","00:37.600 --> 00:40.240","另外我可以预留出一部分钱","","00:40.240 --> 00:41.920","在他们预到意外的时候","","00:41.920 --> 00:43.760","解决他们的原美之级","","00:43.760 --> 00:45.040","至于为什么活了","","00:45.040 --> 00:46.480","最直接的原因就是","","00:46.480 --> 00:48.800","财富传承的需求越来越多了","","00:48.800 --> 00:50.800","而从信拖本身的优势来讲","","00:50.800 --> 00:52.720","他可以实现财产的隔离","","00:52.720 --> 00:53.680","从而规避","","00:53.680 --> 00:55.360","企业精灵带来的风险","","00:55.360 --> 00:58.240","而且信拖还可以保护婚音财产","","00:58.240 --> 01:00.080","尤其是个人的婚前财产","","01:00.080 --> 01:02.400","在现在离婚率这么高的环境里","","01:02.400 --> 01:04.320","越来越多的人开始考虑","","01:04.320 --> 01:06.560","财取财产的保护错失了","","01:06.560 --> 01:08.560","另外信拖还很灵火","","01:08.560 --> 01:10.960","只要是所有权可以转移的财产","","01:10.960 --> 01:12.000","都可以为拖","","01:12.000 --> 01:13.680","像我们的防产企业","","01:13.680 --> 01:16.480","甚至是连专力和板权这样也可以","","01:16.480 --> 01:18.800","而且分配的方式也可以很多样","","01:18.800 --> 01:20.000","可以一次性分配","","01:20.000 --> 01:21.280","也可以多次的分配","","01:21.280 --> 01:23.440","甚至可以设置副家条件","","01:23.440 --> 01:25.040","像我们刚刚举例说的","","01:25.040 --> 01:26.560","意外应急之间","","01:26.560 --> 01:28.320","就属于副家条件的这种","","01:28.320 --> 01:29.360","除此之外","","01:29.360 --> 01:32.080","近几年来法律政策等各方面","","01:32.080 --> 01:33.360","都最加组性拖了","","01:33.360 --> 01:35.120","做出了更加明确的规范","","01:35.120 --> 01:37.120","使得信拖相对于其他","","01:37.120 --> 01:39.120","财产的继续方式更有效","","01:39.120 --> 01:39.920","更专业","","01:39.920 --> 01:41.440","所以大家也会说","","01:41.440 --> 01:42.960","加组性拖是专门","","01:42.960 --> 01:44.960","给高庭值客户准备的服务","","01:44.960 --> 01:46.400","因为他们的财产","","01:46.400 --> 01:47.600","龐大而且多样","","01:47.600 --> 01:49.520","需要更完善有效的方式","","01:49.520 --> 01:51.760","现在你对加组性拖了解了吗","","01:51.760 --> 01:52.640","还有什么疑问","","01:52.640 --> 01:54.160","我们可以在评论区界","","01:54.160 --> 01:55.200","欢迎大家关注我","","01:55.200 --> 01:56.160","但你了解更多","","01:56.160 --> 01:57.200","高庭值的世界",""]
* @throws IOException
*/
public static void vtt(List<String> vtt) throws IOException {
if (CollectionUtils.isEmpty(vtt)) {
return;
}
// 生成一个唯一的VTT文件名
String vttName = "vtt" + UUID.randomUUID().toString() + ".vtt";
// 在临时文件夹路径后追加VTT文件名,得到完整的VTT文件路径
String outputLocalVttPath = TEMPORARY_VTT_URL.concat(vttName);
// 创建对应路径的文件对象
File file = new File(outputLocalVttPath);
// 确保父文件夹存在
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
// 使用BufferedWriter写入VTT内容
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputLocalVttPath))) {
for (String line : vtt) {
writer.write(line);
writer.newLine();
}
}
}
}