- 新增BGR565格式转换逻辑,支持RGB565与BGR565两种颜色格式- 视频上传接口增加code参数,默认值为1(RGB565) - 在VideoProcessUtil中实现convertFramesToBGR565方法 - 添加bgr565ToMp4工具方法用于将BGR565数据编码为MP4文件 - MQTT规则新增对“设备已收到通知”的处理逻辑 - 设备确认消息后更新数据库日志状态并推送SSE消息 - 引入ScheduledExecutorService延时推送SSE消息- 增加设备日志和设备Mapper依赖以支持数据操作
85 lines
2.7 KiB
Java
85 lines
2.7 KiB
Java
package com.fuyuanshen.app.service;
|
||
|
||
import com.fuyuanshen.web.util.VideoProcessUtil;
|
||
import lombok.RequiredArgsConstructor;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.stereotype.Service;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
import java.io.File;
|
||
import java.util.Arrays;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* 视频处理服务
|
||
*/
|
||
@Slf4j
|
||
@Service
|
||
@RequiredArgsConstructor
|
||
public class VideoProcessService {
|
||
|
||
// 配置参数
|
||
private static final int MAX_VIDEO_SIZE = 10 * 1024 * 1024;
|
||
private static final List<String> SUPPORTED_FORMATS = Arrays.asList(".mp4", ".avi", ".mov", ".mkv");
|
||
private static final int FRAME_RATE = 15;
|
||
private static final int DURATION = 2;
|
||
private static final int WIDTH = 160;
|
||
private static final int HEIGHT = 80;
|
||
|
||
private final VideoProcessUtil videoProcessUtil;
|
||
|
||
public List<String> processVideo(MultipartFile file, int code) {
|
||
// 1. 参数校验
|
||
validateVideoFile(file);
|
||
|
||
File tempFile = null;
|
||
try {
|
||
// 2. 创建临时文件
|
||
tempFile = videoProcessUtil.createTempVideoFile(file);
|
||
|
||
// 3. 处理视频并提取帧数据
|
||
List<String> hexList = videoProcessUtil.processVideoToHex(
|
||
tempFile, FRAME_RATE, DURATION, WIDTH, HEIGHT, code
|
||
);
|
||
log.info("code: {} hexList(前100个): {}", code,
|
||
hexList.subList(0, Math.min(100, hexList.size())));
|
||
log.info("视频处理成功,生成Hex数据长度: {}", hexList.size());
|
||
return hexList;
|
||
|
||
} catch (Exception e) {
|
||
log.error("视频处理失败", e);
|
||
throw new RuntimeException("视频处理失败", e);
|
||
} finally {
|
||
// 4. 清理临时文件
|
||
videoProcessUtil.deleteTempFile(tempFile);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 验证视频文件
|
||
*/
|
||
private void validateVideoFile(MultipartFile file) {
|
||
if (file == null || file.isEmpty()) {
|
||
throw new IllegalArgumentException("上传文件不能为空");
|
||
}
|
||
|
||
if (!isVideoFile(file.getOriginalFilename())) {
|
||
throw new IllegalArgumentException("只允许上传视频文件");
|
||
}
|
||
|
||
if (file.getSize() > MAX_VIDEO_SIZE) {
|
||
throw new IllegalArgumentException("视频大小不能超过10MB");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 判断是否是支持的视频格式
|
||
*/
|
||
private boolean isVideoFile(String filename) {
|
||
if (filename == null || filename.lastIndexOf('.') == -1) {
|
||
return false;
|
||
}
|
||
String ext = filename.substring(filename.lastIndexOf('.')).toLowerCase();
|
||
return SUPPORTED_FORMATS.contains(ext);
|
||
}
|
||
} |