hby100japp功能,语音音量增加2
This commit is contained in:
@ -15,6 +15,7 @@ import com.fuyuanshen.equipment.utils.AudioProcessUtil;
|
|||||||
import com.fuyuanshen.equipment.utils.FileHashUtil;
|
import com.fuyuanshen.equipment.utils.FileHashUtil;
|
||||||
import com.fuyuanshen.equipment.utils.Mp3Duration;
|
import com.fuyuanshen.equipment.utils.Mp3Duration;
|
||||||
import com.fuyuanshen.global.mqtt.utils.FfmpegVolumeUtil;
|
import com.fuyuanshen.global.mqtt.utils.FfmpegVolumeUtil;
|
||||||
|
import com.fuyuanshen.global.mqtt.utils.FfmpegVolumeUtil3;
|
||||||
import com.fuyuanshen.system.domain.vo.SysOssVo;
|
import com.fuyuanshen.system.domain.vo.SysOssVo;
|
||||||
import com.fuyuanshen.system.service.ISysOssService;
|
import com.fuyuanshen.system.service.ISysOssService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
@ -31,7 +32,6 @@ import javax.xml.stream.XMLStreamException;
|
|||||||
import javax.xml.stream.XMLStreamReader;
|
import javax.xml.stream.XMLStreamReader;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -182,7 +182,7 @@ public class AudioProcessService {
|
|||||||
|
|
||||||
private String saveByteArrayToFile(byte[] data, String filename) throws IOException {
|
private String saveByteArrayToFile(byte[] data, String filename) throws IOException {
|
||||||
// 确定保存路径(可以是临时目录或指定目录)
|
// 确定保存路径(可以是临时目录或指定目录)
|
||||||
String directory = System.getProperty("java.io.tmpdir"); // 使用系统临时目录
|
String directory = System.getProperty("java.io.tmpdir");// 使用系统临时目录
|
||||||
File dir = new File(directory);
|
File dir = new File(directory);
|
||||||
if (!dir.exists()) {
|
if (!dir.exists()) {
|
||||||
dir.mkdirs();
|
dir.mkdirs();
|
||||||
@ -469,10 +469,12 @@ public class AudioProcessService {
|
|||||||
appBusinessFileBo.setDuration(mp3Duration);
|
appBusinessFileBo.setDuration(mp3Duration);
|
||||||
|
|
||||||
String directory = System.getProperty("java.io.tmpdir"); // 使用系统临时目录
|
String directory = System.getProperty("java.io.tmpdir"); // 使用系统临时目录
|
||||||
File file = new File(directory, generateRandomFileName(fileSuffix));
|
String fileName = generateRandomFileName(fileSuffix);
|
||||||
savedMp3VolumePath = file.getAbsolutePath();
|
savedMp3VolumePath = directory + "/" + fileName;
|
||||||
|
log.info("保存MP3文件: {}", savedMp3VolumePath);
|
||||||
FfmpegVolumeUtil.increaseMp3Volume(savedPath, savedMp3VolumePath, 12);
|
FfmpegVolumeUtil.increaseMp3Volume(savedPath, savedMp3VolumePath, 12);
|
||||||
|
|
||||||
|
File file = new File(savedMp3VolumePath);
|
||||||
String fileHash = fileHashUtil.getFileHash(file,"SHA-256");
|
String fileHash = fileHashUtil.getFileHash(file,"SHA-256");
|
||||||
SysOssVo upload = ossService.updateHash(file, fileHash);
|
SysOssVo upload = ossService.updateHash(file, fileHash);
|
||||||
|
|
||||||
@ -492,11 +494,12 @@ public class AudioProcessService {
|
|||||||
} catch (Exception e){
|
} catch (Exception e){
|
||||||
log.error("上传音频文件失败", e);
|
log.error("上传音频文件失败", e);
|
||||||
} finally {
|
} finally {
|
||||||
log.info("删除临时文件: {}", savedPath);
|
log.info("删除savedPath临时文件: {}", savedPath);
|
||||||
if(savedPath != null){
|
if(savedPath != null){
|
||||||
deleteTempFile(new File(savedPath));
|
deleteTempFile(new File(savedPath));
|
||||||
}
|
}
|
||||||
if(savedMp3VolumePath != null){
|
if(savedMp3VolumePath != null){
|
||||||
|
log.info("删除savedMp3VolumePath临时文件: {}", savedMp3VolumePath);
|
||||||
deleteTempFile(new File(savedMp3VolumePath));
|
deleteTempFile(new File(savedMp3VolumePath));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.fuyuanshen.global.mqtt.utils;
|
package com.fuyuanshen.global.mqtt.utils;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
@ -19,11 +20,33 @@ public class FfmpegVolumeUtil {
|
|||||||
System.err.println("FFmpeg未安装或未找到!请安装FFmpeg并确保在系统路径中。");
|
System.err.println("FFmpeg未安装或未找到!请安装FFmpeg并确保在系统路径中。");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查输入文件
|
||||||
|
File inputFile = new File(inputFilePath);
|
||||||
|
if (!inputFile.exists()) {
|
||||||
|
System.err.println("输入文件不存在:" + inputFilePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!inputFile.canRead()) {
|
||||||
|
System.err.println("输入文件不可读:" + inputFilePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建输出目录
|
||||||
|
File outputFile = new File(outputFilePath);
|
||||||
|
File parentDir = outputFile.getParentFile();
|
||||||
|
if (parentDir != null) {
|
||||||
|
parentDir.mkdirs();
|
||||||
|
if (!parentDir.canWrite()) {
|
||||||
|
System.err.println("输出目录不可写:" + parentDir.getAbsolutePath());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Process process = null;
|
Process process = null;
|
||||||
try {
|
try {
|
||||||
// 构建FFmpeg命令
|
|
||||||
String command = String.format(
|
String command = String.format(
|
||||||
"ffmpeg -i \"%s\" -af \"volume=%sdB\" \"%s\"",
|
"ffmpeg -y -i \"%s\" -af \"volume=%sdB\" \"%s\"",
|
||||||
inputFilePath,
|
inputFilePath,
|
||||||
volumeGain,
|
volumeGain,
|
||||||
outputFilePath
|
outputFilePath
|
||||||
@ -31,37 +54,72 @@ public class FfmpegVolumeUtil {
|
|||||||
|
|
||||||
System.out.println("执行命令: " + command);
|
System.out.println("执行命令: " + command);
|
||||||
|
|
||||||
// 执行FFmpeg命令
|
// 使用 ProcessBuilder 提供更好的控制
|
||||||
process = Runtime.getRuntime().exec(command);
|
ProcessBuilder processBuilder = new ProcessBuilder("ffmpeg", "-y",
|
||||||
|
"-i", inputFilePath, "-af", "volume=" + volumeGain + "dB", outputFilePath);
|
||||||
|
processBuilder.redirectErrorStream(false);
|
||||||
|
process = processBuilder.start();
|
||||||
|
|
||||||
// 读取命令执行结果
|
// 同时读取标准输出和错误输出
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
Process finalProcess = process;
|
||||||
|
Thread stdOutThread = new Thread(() -> {
|
||||||
|
try (BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(finalProcess.getInputStream()))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
System.out.println(line);
|
System.out.println("[FFmpeg STDOUT] " + line);
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("读取标准输出时出错: " + e.getMessage());
|
||||||
|
}finally {
|
||||||
|
finalProcess.destroy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Process finalProcess1 = process;
|
||||||
|
Thread stdErrThread = new Thread(() -> {
|
||||||
|
try (BufferedReader reader = new BufferedReader(
|
||||||
|
new InputStreamReader(finalProcess1.getErrorStream()))) {
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
System.err.println("[FFmpeg STDERR] " + line);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("读取错误输出时出错: " + e.getMessage());
|
||||||
|
}finally {
|
||||||
|
finalProcess1.destroy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
stdOutThread.start();
|
||||||
|
stdErrThread.start();
|
||||||
|
|
||||||
// 等待命令执行完成
|
|
||||||
int exitCode = process.waitFor();
|
int exitCode = process.waitFor();
|
||||||
|
stdOutThread.join();
|
||||||
|
stdErrThread.join();
|
||||||
|
|
||||||
if (exitCode == 0) {
|
if (exitCode == 0) {
|
||||||
System.out.println("音量调整成功!输出文件:" + outputFilePath);
|
System.out.println("音量调整成功!输出文件:" + outputFilePath);
|
||||||
} else {
|
} else {
|
||||||
System.err.println("FFmpeg命令执行失败,退出码:" + exitCode);
|
System.err.println("FFmpeg命令执行失败,退出码:" + exitCode);
|
||||||
}
|
}
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
|
System.err.println("执行FFmpeg命令时发生错误: " + e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
if (process != null) {
|
if (process != null) {
|
||||||
|
System.out.println("已销毁进程");
|
||||||
process.destroy();
|
process.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// 示例用法
|
// 示例用法
|
||||||
String inputPath = "D:\\http_output9.mp3";
|
String inputPath = "/app/input.mp3";
|
||||||
String outputPath = "D:\\output17.mp3";
|
String outputPath = "/app/output17.mp3";
|
||||||
int volumeGain = 12;
|
int volumeGain = 12;
|
||||||
|
|
||||||
increaseMp3Volume(inputPath, outputPath, volumeGain);
|
increaseMp3Volume(inputPath, outputPath, volumeGain);
|
||||||
|
|||||||
@ -0,0 +1,92 @@
|
|||||||
|
package com.fuyuanshen.global.mqtt.utils;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
public class FfmpegVolumeUtil3 {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用FFmpeg增加MP3文件的音量
|
||||||
|
*
|
||||||
|
* @param inputFilePath 输入MP3文件路径
|
||||||
|
* @param outputFilePath 输出MP3文件路径
|
||||||
|
* @param volumeGain 音量增益(例如:1.5表示增加50%音量,2.0表示翻倍)
|
||||||
|
*/
|
||||||
|
public static void increaseMp3Volume(String inputFilePath, String outputFilePath, int volumeGain) {
|
||||||
|
boolean ffmpegAvailable = isFfmpegAvailable();
|
||||||
|
if (!ffmpegAvailable) {
|
||||||
|
System.err.println("FFmpeg未安装或未找到!请安装FFmpeg并确保在系统路径中。");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 创建输出文件的父目录
|
||||||
|
File outputFile = new File(outputFilePath);
|
||||||
|
File parentDir = outputFile.getParentFile();
|
||||||
|
if (parentDir != null && !parentDir.exists()) {
|
||||||
|
boolean created = parentDir.mkdirs();
|
||||||
|
if (!created) {
|
||||||
|
System.err.println("无法创建输出目录:" + parentDir.getAbsolutePath());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Process process = null;
|
||||||
|
try {
|
||||||
|
// 构建FFmpeg命令
|
||||||
|
String command = String.format(
|
||||||
|
"ffmpeg -y -i \"%s\" -af \"volume=%sdB\" \"%s\"",
|
||||||
|
inputFilePath,
|
||||||
|
volumeGain,
|
||||||
|
outputFilePath
|
||||||
|
);
|
||||||
|
|
||||||
|
System.out.println("执行命令: " + command);
|
||||||
|
|
||||||
|
// 执行FFmpeg命令
|
||||||
|
process = Runtime.getRuntime().exec(command);
|
||||||
|
|
||||||
|
// 读取命令执行结果
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
System.out.println(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 等待命令执行完成
|
||||||
|
int exitCode = process.waitFor();
|
||||||
|
if (exitCode == 0) {
|
||||||
|
System.out.println("音量调整成功!输出文件:" + outputFilePath);
|
||||||
|
} else {
|
||||||
|
System.err.println("FFmpeg命令执行失败,退出码:" + exitCode);
|
||||||
|
}
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (process != null) {
|
||||||
|
process.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
// 示例用法
|
||||||
|
String inputPath = "/app/input.mp3";
|
||||||
|
String outputPath = "/app/output18.mp3";
|
||||||
|
int volumeGain = 12;
|
||||||
|
|
||||||
|
increaseMp3Volume(inputPath, outputPath, volumeGain);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isFfmpegAvailable() {
|
||||||
|
try {
|
||||||
|
Process process = Runtime.getRuntime().exec("ffmpeg -version");
|
||||||
|
int exitCode = process.waitFor();
|
||||||
|
return exitCode == 0;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user