|
|
@@ -45,9 +45,62 @@ public class AudioUtils {
|
|
|
|
|
|
/**
|
|
|
* 工具地址
|
|
|
+ * 优先级:环境变量 FFMPEG_PATH > 系统 PATH 查找 > 默认 c:\\ffmpeg\\bin\\
|
|
|
**/
|
|
|
- static String path = "c:\\";
|
|
|
- static String destinationDir = "c:\\hook\\";
|
|
|
+ static String path = initFfmpegPath();
|
|
|
+ static String destinationDir = initDestinationDir();
|
|
|
+
|
|
|
+ private static String initFfmpegPath() {
|
|
|
+ // 1. 优先读环境变量 FFMPEG_PATH
|
|
|
+ String envPath = System.getenv("FFMPEG_PATH");
|
|
|
+ if (envPath != null && !envPath.trim().isEmpty()) {
|
|
|
+ String normalized = envPath.endsWith("\\") || envPath.endsWith("/") ? envPath : envPath + File.separator;
|
|
|
+ log.info("[AudioUtils] 使用环境变量 FFMPEG_PATH: {}", normalized);
|
|
|
+ return normalized;
|
|
|
+ }
|
|
|
+ // 2. 从系统 PATH 中查找 ffmpeg.exe 真实位置
|
|
|
+ String ffmpegPath = resolveFfmpegFromPath();
|
|
|
+ if (ffmpegPath != null) {
|
|
|
+ String dir = ffmpegPath.substring(0, ffmpegPath.lastIndexOf(File.separator) + 1);
|
|
|
+ log.info("[AudioUtils] 从 PATH 查找到 ffmpeg,目录: {}", dir);
|
|
|
+ return dir;
|
|
|
+ }
|
|
|
+ // 3. 兜底默认值
|
|
|
+ log.info("[AudioUtils] 未找到 ffmpeg,使用默认路径: c:\\ffmpeg\\bin\\");
|
|
|
+ return "c:\\ffmpeg\\bin\\";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 遍历 PATH 环境变量,查找 ffmpeg.exe 的真实绝对路径
|
|
|
+ */
|
|
|
+ private static String resolveFfmpegFromPath() {
|
|
|
+ String pathEnv = System.getenv("PATH");
|
|
|
+ if (pathEnv == null || pathEnv.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String[] dirs = pathEnv.split(File.pathSeparator);
|
|
|
+ for (String dir : dirs) {
|
|
|
+ if (dir == null || dir.trim().isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ File ffmpegFile = new File(dir, "ffmpeg.exe");
|
|
|
+ if (ffmpegFile.exists() && ffmpegFile.isFile()) {
|
|
|
+ return ffmpegFile.getAbsolutePath();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String initDestinationDir() {
|
|
|
+ String envDir = System.getenv("FFMPEG_TEMP_DIR");
|
|
|
+ if (envDir != null && !envDir.trim().isEmpty()) {
|
|
|
+ String normalized = envDir.endsWith("\\") || envDir.endsWith("/") ? envDir : envDir + File.separator;
|
|
|
+ log.info("[AudioUtils] 使用环境变量 FFMPEG_TEMP_DIR: {}", normalized);
|
|
|
+ return normalized;
|
|
|
+ }
|
|
|
+ log.info("[AudioUtils] 未配置 FFMPEG_TEMP_DIR,使用默认值: c:\\hook\\");
|
|
|
+ return "c:\\hook\\";
|
|
|
+ }
|
|
|
public static AudioVO createVoiceUrl(Long id,String userVoiceUrl){
|
|
|
String fileUrl = staticAiHostProper.getVoiceApi() + "/app/common/createVoiceUrl?id=" + id + "&userVoiceUrl=" + userVoiceUrl;
|
|
|
|