feat(equipment): 添加阿里巴巴TTS语音合成工具类
- 实现文本转语音功能,支持多种声音、语速、音量等参数调节 - 集成阿里云TTS服务,支持访问令牌自动刷新与缓存 - 提供HTTP客户端配置与请求处理逻辑 - 支持生成标准PCM数据及WAV格式音频文件 - 实现音频文件保存与错误处理机制 - 添加参数校验与日志记录功能 - 集成Redis缓存管理访问令牌- 支持URL编码与请求构建逻辑 - 实现响应处理与音频数据写入文件功能 - 添加静默删除临时文件与错误响应处理机制
This commit is contained in:
@ -0,0 +1,84 @@
|
||||
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) {
|
||||
// 1. 参数校验
|
||||
validateVideoFile(file);
|
||||
|
||||
File tempFile = null;
|
||||
try {
|
||||
// 2. 创建临时文件
|
||||
tempFile = videoProcessUtil.createTempVideoFile(file);
|
||||
|
||||
// 3. 处理视频并提取帧数据
|
||||
List<String> hexList = videoProcessUtil.processVideoToHex(
|
||||
tempFile, FRAME_RATE, DURATION, WIDTH, HEIGHT
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user