前言
本文提供一个可以给一个wav音频添加自定义时长静音的工具类。正好工作中用到,所以正好分享分享。
Maven依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.2</version>
</dependency>
代码
package ai.guiji.csdn.tools;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.URLUtil;
import com.google.common.base.Joiner;
import com.google.common.primitives.Bytes;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.LongStream;
public class WavAddSilenceUtils {
public static byte[] buildWavHeader(int dataLength, int srate, int channel, int format)
throws IOException {
byte[] header = new byte[44];
long totalDataLen = dataLength + 36;
long bitrate = srate * channel * format;
header[0] = 'R';
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (byte) (totalDataLen & 0xff);
header[5] = (byte) ((totalDataLen >> 8) & 0xff);
header[6] = (byte) ((totalDataLen >> 16) & 0xff);
header[7] = (byte) ((totalDataLen >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f';
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = (byte) format;
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1;
header[21] = 0;
header[22] = (byte) channel;
header[23] = 0;
header[24] = (byte) (srate & 0xff);
header[25] = (byte) ((srate >> 8) & 0xff);
header[26] = (byte) ((srate >> 16) & 0xff);
header[27] = (byte) ((srate >> 24) & 0xff);
header[28] = (byte) ((bitrate / 8) & 0xff);
header[29] = (byte) (((bitrate / 8) >> 8) & 0xff);
header[30] = (byte) (((bitrate / 8) >> 16) & 0xff);
header[31] = (byte) (((bitrate / 8) >> 24) & 0xff);
header[32] = (byte) ((channel * format) / 8);
header[33] = 0;
header[34] = 16;
header[35] = 0;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (byte) (dataLength & 0xff);
header[41] = (byte) ((dataLength >> 8) & 0xff);
header[42] = (byte) ((dataLength >> 16) & 0xff);
header[43] = (byte) ((dataLength >> 24) & 0xff);
return header;
}
public static boolean writeToFile(String filePath, byte[] pcmData) {
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] header = buildWavHeader(pcmData.length, 16000, 1, 16);
bos.write(header, 0, 44);
bos.write(pcmData);
bos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
public static String delayWav(String filePath, String tmpDirPath, Long delayTime)
throws Exception {
byte[] bytes;
if (filePath.startsWith("http")) {
bytes = IoUtil.readBytes(URLUtil.getStream(new URL(filePath)));
} else {
bytes = FileUtil.readBytes(filePath);
}
List<Byte> resultByte =
new ArrayList<>(Bytes.asList(Arrays.copyOfRange(bytes, 44, bytes.length)));
LongStream.range(0, (long) (delayTime * 32)).forEach(x -> resultByte.add((byte) 0));
String resultPath =
Joiner.on(File.separator).join(Arrays.asList(tmpDirPath, IdUtil.simpleUUID() + ".wav"));
writeToFile(resultPath, Bytes.toArray(resultByte));
return resultPath;
}
public static void main(String[] args) throws Exception {
System.out.println(delayWav("http://xxxx/xxx.wav", "C:\\Users\\huyi\\Desktop\\", 10000L));
}
}
代码说明:
1、delayWav方法参数分别为wav音频文件地址(可以支持http的url地址)、临时文件目录地址、延时静音时长(单位毫秒)
2、对wav的要求默认为:采样率16K、单声道。可以参考:Java实现生成自定义时长的静音音频
对需要处理的音频参数调整。
3、生成uuid的随机文件名,避免重复。
验证一下
下面是准备的音频
执行结果
执行后音频
OK,加了10秒的静音。
到此这篇关于Java实现音频添加自定义时长静音的示例代码的文章就介绍到这了,更多相关Java音频添加静音内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!