|
@@ -3,9 +3,11 @@ package org.jeecg.modules.opencv.service.impl;
|
|
import cn.com.ctop.common.module.utils.*;
|
|
import cn.com.ctop.common.module.utils.*;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.jeecg.modules.opencv.entity.ModelVideoCompositeInfo;
|
|
import org.jeecg.modules.opencv.entity.ModelVideoInfo;
|
|
import org.jeecg.modules.opencv.entity.ModelVideoInfo;
|
|
import org.jeecg.modules.opencv.entity.ModelVideoInfoConfig;
|
|
import org.jeecg.modules.opencv.entity.ModelVideoInfoConfig;
|
|
import org.jeecg.modules.opencv.service.IFfmpegService;
|
|
import org.jeecg.modules.opencv.service.IFfmpegService;
|
|
|
|
+import org.jeecg.modules.opencv.service.IModelVideoCompositeInfoService;
|
|
import org.jeecg.modules.opencv.service.IModelVideoInfoConfigService;
|
|
import org.jeecg.modules.opencv.service.IModelVideoInfoConfigService;
|
|
import org.jeecg.modules.opencv.service.IModelVideoInfoService;
|
|
import org.jeecg.modules.opencv.service.IModelVideoInfoService;
|
|
import org.jeecg.modules.opencv.utils.ImageUtils;
|
|
import org.jeecg.modules.opencv.utils.ImageUtils;
|
|
@@ -19,9 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
-import java.io.InputStream;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
import java.math.RoundingMode;
|
|
import java.math.RoundingMode;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
@@ -156,8 +156,55 @@ public class FfmpegServiceImpl implements IFfmpegService {
|
|
builder.command(command);
|
|
builder.command(command);
|
|
builder.redirectErrorStream(true);
|
|
builder.redirectErrorStream(true);
|
|
Process process = builder.start();
|
|
Process process = builder.start();
|
|
|
|
+ //获取进程的标准输入流
|
|
|
|
+ final InputStream is1 = process.getInputStream();
|
|
|
|
+ //获取进城的错误流
|
|
|
|
+ final InputStream is2 = process.getErrorStream();
|
|
|
|
+ //启动两个线程,一个线程负责读标准输出流,另一个负责读标准错误流
|
|
|
|
+ new Thread() {
|
|
|
|
+ public void run() {
|
|
|
|
+ BufferedReader br1 = new BufferedReader(new InputStreamReader(is1));
|
|
|
|
+ try {
|
|
|
|
+ String line1 = null;
|
|
|
|
+ while ((line1 = br1.readLine()) != null) {
|
|
|
|
+ if (line1 != null) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ is1.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }.start();
|
|
|
|
+
|
|
|
|
+ new Thread() {
|
|
|
|
+ public void run() {
|
|
|
|
+ BufferedReader br2 = new BufferedReader(new InputStreamReader(is2));
|
|
|
|
+ try {
|
|
|
|
+ String line2 = null;
|
|
|
|
+ while ((line2 = br2.readLine()) != null) {
|
|
|
|
+ if (line2 != null) {
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ try {
|
|
|
|
+ is2.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }.start();
|
|
// 等待进程执行结束
|
|
// 等待进程执行结束
|
|
process.waitFor();
|
|
process.waitFor();
|
|
|
|
+ process.destroy();
|
|
log.info("ffmpeg命令执行完成");
|
|
log.info("ffmpeg命令执行完成");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
@@ -165,7 +212,7 @@ public class FfmpegServiceImpl implements IFfmpegService {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public Map<String, Object> videoMerge(JSONObject data) {
|
|
|
|
|
|
+ public Map<String, Object> videoMerge(JSONObject data, String userId) {
|
|
Map<String, Object> result = new HashMap<>();
|
|
Map<String, Object> result = new HashMap<>();
|
|
try {
|
|
try {
|
|
Long modelId = data.getLong("modelId");
|
|
Long modelId = data.getLong("modelId");
|
|
@@ -181,7 +228,7 @@ public class FfmpegServiceImpl implements IFfmpegService {
|
|
}
|
|
}
|
|
//1:
|
|
//1:
|
|
url = "https:" + url;
|
|
url = "https:" + url;
|
|
- String getUrl = mergeVideo(videoInfo, url);
|
|
|
|
|
|
+ String getUrl = mergeVideo(videoInfo, url, userId);
|
|
ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
|
|
ResultMapUtils.setResultMap(result, StatusCode.COMMON_SUCCESS);
|
|
result.put("url", getUrl);
|
|
result.put("url", getUrl);
|
|
return result;
|
|
return result;
|
|
@@ -195,17 +242,17 @@ public class FfmpegServiceImpl implements IFfmpegService {
|
|
@Autowired
|
|
@Autowired
|
|
private IModelVideoInfoConfigService configService;
|
|
private IModelVideoInfoConfigService configService;
|
|
|
|
|
|
- private String mergeVideo(ModelVideoInfo videoInfo, String url) throws Exception {
|
|
|
|
|
|
+ private String mergeVideo(ModelVideoInfo videoInfo, String url, String userId) throws Exception {
|
|
|
|
+ Long current = System.currentTimeMillis();
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
|
|
//根据url下载视频
|
|
//根据url下载视频
|
|
- String srcVideoPath = PropertiesUtils.getValue("kuaishou_config", "model_video_src_path_test");
|
|
|
|
- String fillVideoPath = PropertiesUtils.getValue("kuaishou_config", "model_video_fill_path_test");
|
|
|
|
- String finalVideoPath = PropertiesUtils.getValue("kuaishou_config", "model_video_final_path_test");
|
|
|
|
|
|
+ String srcVideoPath = PropertiesUtils.getValue("kuaishou_config", "model_video_src_path");
|
|
|
|
+ String fillVideoPath = PropertiesUtils.getValue("kuaishou_config", "model_video_fill_path");
|
|
|
|
+ String finalVideoPath = PropertiesUtils.getValue("kuaishou_config", "model_video_final_path");
|
|
|
|
|
|
String fillFilePath = LoadFileUtil.downLoadFromUrl(url, fillVideoPath);
|
|
String fillFilePath = LoadFileUtil.downLoadFromUrl(url, fillVideoPath);
|
|
String srcFilePath = LoadFileUtil.downLoadFromUrl(videoInfo.getModelVideoUrl(), srcVideoPath);
|
|
String srcFilePath = LoadFileUtil.downLoadFromUrl(videoInfo.getModelVideoUrl(), srcVideoPath);
|
|
|
|
|
|
- Long current = System.currentTimeMillis();
|
|
|
|
String splitSrcPath = srcVideoPath + current + File.separator;
|
|
String splitSrcPath = srcVideoPath + current + File.separator;
|
|
String splitFillPath = fillVideoPath + current + File.separator;
|
|
String splitFillPath = fillVideoPath + current + File.separator;
|
|
String splitFinalPath = finalVideoPath + current + File.separator;
|
|
String splitFinalPath = finalVideoPath + current + File.separator;
|
|
@@ -289,9 +336,15 @@ public class FfmpegServiceImpl implements IFfmpegService {
|
|
String finalVideoFilePath = splitSrcPath + "final.mp4";
|
|
String finalVideoFilePath = splitSrcPath + "final.mp4";
|
|
mergeAudioAndVideo(noVoiceVideoFilePath, finalM4aFilePath, finalVideoFilePath);
|
|
mergeAudioAndVideo(noVoiceVideoFilePath, finalM4aFilePath, finalVideoFilePath);
|
|
//3.6 上传视频文件至OSS并返回url存入数据库
|
|
//3.6 上传视频文件至OSS并返回url存入数据库
|
|
- InputStream inputStream = new FileInputStream(finalM4aFilePath);
|
|
|
|
- String finalUrl = OSSUtils.uploadFile2OSS(inputStream, "/model/final/", videoInfo.getTitle() + current + ".mp4");
|
|
|
|
|
|
+ InputStream inputStream = new FileInputStream(finalVideoFilePath);
|
|
|
|
+ String finalUrl = OSSUtils.uploadFile2OSS(inputStream, "model/final/", videoInfo.getId() + "_" + current + ".mp4");
|
|
System.out.println(finalUrl);
|
|
System.out.println(finalUrl);
|
|
|
|
+ ModelVideoCompositeInfo compositeInfo = new ModelVideoCompositeInfo(userId, videoInfo, finalUrl);
|
|
|
|
+ compositeInfoService.saveOrUpdate(compositeInfo);
|
|
|
|
+ //删除文件
|
|
|
|
+ deleteFile(splitSrcPath, splitFillPath, splitFinalPath, srcFilePath, fillFilePath);
|
|
|
|
+ log.info("视频合成成功:总时长{}毫秒", System.currentTimeMillis() - current);
|
|
|
|
+ return finalUrl;
|
|
} else if (config.getType() == 2) {
|
|
} else if (config.getType() == 2) {
|
|
//2:合并图片
|
|
//2:合并图片
|
|
|
|
|
|
@@ -301,6 +354,17 @@ public class FfmpegServiceImpl implements IFfmpegService {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void deleteFile(String splitSrcPath, String splitFillPath, String splitFinalPath, String srcFilePath, String fillFilePath) {
|
|
|
|
+ LoadFileUtil.delFile(splitFillPath);
|
|
|
|
+ LoadFileUtil.delFile(splitSrcPath);
|
|
|
|
+ LoadFileUtil.delFile(splitFinalPath);
|
|
|
|
+ LoadFileUtil.delFile(srcFilePath);
|
|
|
|
+ LoadFileUtil.delFile(fillFilePath);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IModelVideoCompositeInfoService compositeInfoService;
|
|
|
|
+
|
|
private String getSecond(int exchangeStartFrameNum) {
|
|
private String getSecond(int exchangeStartFrameNum) {
|
|
BigDecimal second = new BigDecimal(exchangeStartFrameNum).divide(new BigDecimal("25"), 3, RoundingMode.HALF_UP);
|
|
BigDecimal second = new BigDecimal(exchangeStartFrameNum).divide(new BigDecimal("25"), 3, RoundingMode.HALF_UP);
|
|
if (second.compareTo(new BigDecimal("10")) < 0) {
|
|
if (second.compareTo(new BigDecimal("10")) < 0) {
|