You've already forked FrameTour-BE
1111
This commit is contained in:
@ -3,6 +3,7 @@ package com.ycwl.basic.task;
|
||||
import com.ycwl.basic.device.DeviceFactory;
|
||||
import com.ycwl.basic.device.entity.common.FileObject;
|
||||
import com.ycwl.basic.device.operator.IDeviceStorageOperator;
|
||||
import com.ycwl.basic.device.repository.DeviceRepository;
|
||||
import com.ycwl.basic.mapper.DeviceMapper;
|
||||
import com.ycwl.basic.mapper.FaceSampleMapper;
|
||||
import com.ycwl.basic.mapper.SourceMapper;
|
||||
@ -47,12 +48,14 @@ public class VideoPieceGetter {
|
||||
@Autowired
|
||||
private DeviceMapper deviceMapper;
|
||||
@Autowired
|
||||
private DeviceRepository deviceRepository;
|
||||
@Autowired
|
||||
private SourceMapper sourceMapper;
|
||||
|
||||
@Data
|
||||
public static class Task {
|
||||
public String type = "normal";
|
||||
public Long faceSampleId;
|
||||
public List<Long> faceSampleId;
|
||||
public Callback callback;
|
||||
public Long memberId;
|
||||
public Long faceId;
|
||||
@ -86,92 +89,95 @@ public class VideoPieceGetter {
|
||||
task.getCallback().onInvoke();
|
||||
return;
|
||||
}
|
||||
FaceSampleRespVO faceSample = faceSampleMapper.getById(task.getFaceSampleId());
|
||||
DeviceEntity device = deviceMapper.getByDeviceId(faceSample.getDeviceId());
|
||||
DeviceConfigEntity config = deviceMapper.getConfigByDeviceId(faceSample.getDeviceId());
|
||||
task.getFaceSampleId().parallelStream().forEach(faceSampleId -> {
|
||||
FaceSampleRespVO faceSample = faceSampleMapper.getById(faceSampleId);
|
||||
DeviceEntity device = deviceRepository.getDevice(faceSample.getDeviceId());
|
||||
DeviceConfigEntity config = deviceRepository.getDeviceConfig(faceSample.getDeviceId());
|
||||
|
||||
SourceEntity source = sourceMapper.querySameVideo(faceSample.getId(), device.getId());
|
||||
if (source != null) {
|
||||
// 有原视频
|
||||
int count = sourceMapper.hasRelationTo(task.getMemberId(), source.getId(), 1);
|
||||
if (count > 0) {
|
||||
SourceEntity source = sourceMapper.querySameVideo(faceSample.getId(), device.getId());
|
||||
if (source != null) {
|
||||
// 有原视频
|
||||
int count = sourceMapper.hasRelationTo(task.getMemberId(), source.getId(), 1);
|
||||
if (count > 0) {
|
||||
return;
|
||||
}
|
||||
MemberSourceEntity videoSource = new MemberSourceEntity();
|
||||
videoSource.setId(SnowFlakeUtil.getLongId());
|
||||
videoSource.setScenicId(faceSample.getScenicId());
|
||||
videoSource.setFaceId(task.getFaceId());
|
||||
videoSource.setMemberId(task.getMemberId());
|
||||
videoSource.setType(1);
|
||||
videoSource.setIsBuy(0);
|
||||
videoSource.setSourceId(source.getId());
|
||||
sourceMapper.addRelation(videoSource);
|
||||
return;
|
||||
}
|
||||
BigDecimal cutPre = BigDecimal.valueOf(5L);
|
||||
BigDecimal cutPost = BigDecimal.valueOf(4L);
|
||||
if (config == null) {
|
||||
return;
|
||||
}
|
||||
// 有配置
|
||||
if (config.getCutPre() != null) {
|
||||
cutPre = config.getCutPre();
|
||||
}
|
||||
if (config.getCutPost() != null) {
|
||||
cutPost = config.getCutPost();
|
||||
}
|
||||
IDeviceStorageOperator pieceGetter = DeviceFactory.getDeviceStorageOperator(device, config);
|
||||
if (pieceGetter == null) {
|
||||
return;
|
||||
}
|
||||
BigDecimal duration = cutPre.add(cutPost);
|
||||
List<FileObject> listByDtRange = pieceGetter.getFileListByDtRange(
|
||||
new Date(faceSample.getCreateAt().getTime() - cutPre.multiply(BigDecimal.valueOf(1000)).longValue()),
|
||||
new Date(faceSample.getCreateAt().getTime() + cutPost.multiply(BigDecimal.valueOf(1000)).longValue())
|
||||
);
|
||||
if (listByDtRange.isEmpty()) {
|
||||
log.warn("没有可用的文件");
|
||||
return;
|
||||
}
|
||||
long offset = faceSample.getCreateAt().getTime() - cutPre.multiply(BigDecimal.valueOf(1000)).longValue() - listByDtRange.get(0).getCreateTime().getTime();
|
||||
FfmpegTask ffmpegTask = new FfmpegTask();
|
||||
ffmpegTask.setFileList(listByDtRange);
|
||||
ffmpegTask.setDuration(duration);
|
||||
ffmpegTask.setOffsetStart(BigDecimal.valueOf(offset, 3));
|
||||
File outFile = new File(faceSample.getDeviceId().toString() + "_" + faceSample.getId() + ".mp4");
|
||||
ffmpegTask.setOutputFile(outFile.getAbsolutePath());
|
||||
boolean result = startFfmpegTask(ffmpegTask);
|
||||
if (!result) {
|
||||
log.warn("视频裁切失败");
|
||||
return;
|
||||
}
|
||||
log.info("视频裁切成功");
|
||||
IStorageAdapter adapter = StorageFactory.use("assets");
|
||||
String url = adapter.uploadFile(outFile, "video-source", outFile.getName());
|
||||
// 上传成功后删除文件
|
||||
outFile.delete();
|
||||
SourceEntity imgSource = sourceMapper.findBySampleId(faceSample.getId());
|
||||
SourceEntity sourceEntity = new SourceEntity();
|
||||
sourceEntity.setId(SnowFlakeUtil.getLongId());
|
||||
sourceEntity.setCreateTime(faceSample.getCreateAt());
|
||||
MemberSourceEntity videoSource = new MemberSourceEntity();
|
||||
videoSource.setId(SnowFlakeUtil.getLongId());
|
||||
videoSource.setScenicId(faceSample.getScenicId());
|
||||
videoSource.setFaceId(task.getFaceId());
|
||||
videoSource.setMemberId(task.getMemberId());
|
||||
videoSource.setType(1);
|
||||
videoSource.setIsBuy(0);
|
||||
videoSource.setSourceId(source.getId());
|
||||
videoSource.setFaceId(task.getFaceId());
|
||||
videoSource.setScenicId(faceSample.getScenicId());
|
||||
videoSource.setSourceId(sourceEntity.getId());
|
||||
if (imgSource != null) {
|
||||
sourceEntity.setUrl(imgSource.getUrl());
|
||||
sourceEntity.setPosJson(imgSource.getPosJson());
|
||||
}
|
||||
sourceEntity.setVideoUrl(url);
|
||||
sourceEntity.setFaceSampleId(faceSample.getId());
|
||||
sourceEntity.setScenicId(faceSample.getScenicId());
|
||||
sourceEntity.setDeviceId(faceSample.getDeviceId());
|
||||
sourceEntity.setType(1);
|
||||
sourceMapper.add(sourceEntity);
|
||||
sourceMapper.addRelation(videoSource);
|
||||
return;
|
||||
}
|
||||
BigDecimal cutPre = BigDecimal.valueOf(5L);
|
||||
BigDecimal cutPost = BigDecimal.valueOf(4L);
|
||||
if (config == null) {
|
||||
return;
|
||||
}
|
||||
// 有配置
|
||||
if (config.getCutPre() != null) {
|
||||
cutPre = config.getCutPre();
|
||||
}
|
||||
if (config.getCutPost() != null) {
|
||||
cutPost = config.getCutPost();
|
||||
}
|
||||
IDeviceStorageOperator pieceGetter = DeviceFactory.getDeviceStorageOperator(device, config);
|
||||
if (pieceGetter == null) {
|
||||
return;
|
||||
}
|
||||
BigDecimal duration = cutPre.add(cutPost);
|
||||
List<FileObject> listByDtRange = pieceGetter.getFileListByDtRange(
|
||||
new Date(faceSample.getCreateAt().getTime() - cutPre.multiply(BigDecimal.valueOf(1000)).longValue()),
|
||||
new Date(faceSample.getCreateAt().getTime() + cutPost.multiply(BigDecimal.valueOf(1000)).longValue())
|
||||
);
|
||||
if (listByDtRange.isEmpty()) {
|
||||
log.warn("没有可用的文件");
|
||||
return;
|
||||
}
|
||||
long offset = faceSample.getCreateAt().getTime() - cutPre.multiply(BigDecimal.valueOf(1000)).longValue() - listByDtRange.get(0).getCreateTime().getTime();
|
||||
FfmpegTask ffmpegTask = new FfmpegTask();
|
||||
ffmpegTask.setFileList(listByDtRange);
|
||||
ffmpegTask.setDuration(duration);
|
||||
ffmpegTask.setOffsetStart(BigDecimal.valueOf(offset, 3));
|
||||
File outFile = new File(faceSample.getDeviceId().toString() + "_" + faceSample.getId() + ".mp4");
|
||||
ffmpegTask.setOutputFile(outFile.getAbsolutePath());
|
||||
boolean result = startFfmpegTask(ffmpegTask);
|
||||
if (!result) {
|
||||
log.warn("视频裁切失败");
|
||||
return;
|
||||
}
|
||||
log.info("视频裁切成功");
|
||||
IStorageAdapter adapter = StorageFactory.use("assets");
|
||||
String url = adapter.uploadFile(outFile, "video-source", outFile.getName());
|
||||
// 上传成功后删除文件
|
||||
outFile.delete();
|
||||
SourceEntity imgSource = sourceMapper.findBySampleId(faceSample.getId());
|
||||
SourceEntity sourceEntity = new SourceEntity();
|
||||
sourceEntity.setId(SnowFlakeUtil.getLongId());
|
||||
sourceEntity.setCreateTime(faceSample.getCreateAt());
|
||||
MemberSourceEntity videoSource = new MemberSourceEntity();
|
||||
videoSource.setMemberId(task.getMemberId());
|
||||
videoSource.setType(1);
|
||||
videoSource.setIsBuy(0);
|
||||
videoSource.setFaceId(task.getFaceId());
|
||||
videoSource.setScenicId(faceSample.getScenicId());
|
||||
videoSource.setSourceId(sourceEntity.getId());
|
||||
if (imgSource != null) {
|
||||
sourceEntity.setUrl(imgSource.getUrl());
|
||||
sourceEntity.setPosJson(imgSource.getPosJson());
|
||||
}
|
||||
sourceEntity.setVideoUrl(url);
|
||||
sourceEntity.setFaceSampleId(faceSample.getId());
|
||||
sourceEntity.setScenicId(faceSample.getScenicId());
|
||||
sourceEntity.setDeviceId(faceSample.getDeviceId());
|
||||
sourceEntity.setType(1);
|
||||
sourceMapper.add(sourceEntity);
|
||||
sourceMapper.addRelation(videoSource);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public boolean startFfmpegTask(FfmpegTask task) {
|
||||
|
Reference in New Issue
Block a user