java实现切割wav音频文件的方法详解【附外部jar包下载】
作者:lwjwd 时间:2021-07-08 22:29:56
本文实例讲述了java实现切割wav音频文件的方法。分享给大家供大家参考,具体如下:
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* wav音频文件截取工具
* (适用于比特率为128kbps的wav音频文件,此类音频文件的头部信息占用长度44字节)
* @author lwj
*
*/
public class WavCut {
/**
* 截取wav音频文件
* @param sourcepath 源文件地址
* @param targetpath 目标文件地址
* @param start 截取开始时间(秒)
* @param end 截取结束时间(秒)
*
* return 截取成功返回true,否则返回false
*/
public static boolean cut(String sourcefile, String targetfile, int start, int end) {
try{
if(!sourcefile.toLowerCase().endsWith(".wav") || !targetfile.toLowerCase().endsWith(".wav")){
return false;
}
File wav = new File(sourcefile);
if(!wav.exists()){
return false;
}
long t1 = getTimeLen(wav); //总时长(秒)
if(start<0 || end<=0 || start>=t1 || end>t1 || start>=end){
return false;
}
FileInputStream fis = new FileInputStream(wav);
long wavSize = wav.length()-44; //音频数据大小(44为128kbps比特率wav文件头长度)
long splitSize = (wavSize/t1)*(end-start); //截取的音频数据大小
long skipSize = (wavSize/t1)*start; //截取时跳过的音频数据大小
int splitSizeInt = Integer.parseInt(String.valueOf(splitSize));
int skipSizeInt = Integer.parseInt(String.valueOf(skipSize));
ByteBuffer buf1 = ByteBuffer.allocate(4); //存放文件大小,4代表一个int占用字节数
buf1.putInt(splitSizeInt+36); //放入文件长度信息
byte[] flen = buf1.array(); //代表文件长度
ByteBuffer buf2 = ByteBuffer.allocate(4); //存放音频数据大小,4代表一个int占用字节数
buf2.putInt(splitSizeInt); //放入数据长度信息
byte[] dlen = buf2.array(); //代表数据长度
flen = reverse(flen); //数组反转
dlen = reverse(dlen);
byte[] head = new byte[44]; //定义wav头部信息数组
fis.read(head, 0, head.length); //读取源wav文件头部信息
for(int i=0; i<4; i++){ //4代表一个int占用字节数
head[i+4] = flen[i]; //替换原头部信息里的文件长度
head[i+40] = dlen[i]; //替换原头部信息里的数据长度
}
byte[] fbyte = new byte[splitSizeInt+head.length]; //存放截取的音频数据
for(int i=0; i<head.length; i++){ //放入修改后的头部信息
fbyte[i] = head[i];
}
byte[] skipBytes = new byte[skipSizeInt]; //存放截取时跳过的音频数据
fis.read(skipBytes, 0, skipBytes.length); //跳过不需要截取的数据
fis.read(fbyte, head.length, fbyte.length-head.length); //读取要截取的数据到目标数组
fis.close();
File target = new File(targetfile);
if(target.exists()){ //如果目标文件已存在,则删除目标文件
target.delete();
}
FileOutputStream fos = new FileOutputStream(target);
fos.write(fbyte);
fos.flush();
fos.close();
}catch(IOException e){
e.printStackTrace();
return false;
}
return true;
}
/**
* 获取音频文件总时长
* @param filePath 文件路径
* @return
*/
public static long getTimeLen(File file){
long tlen = 0;
if(file!=null && file.exists()){
Encoder encoder = new Encoder();
try {
MultimediaInfo m = encoder.getInfo(file);
long ls = m.getDuration();
tlen = ls/1000;
} catch (Exception e) {
e.printStackTrace();
}
}
return tlen;
}
/**
* 数组反转
* @param array
*/
public static byte[] reverse(byte[] array){
byte temp;
int len=array.length;
for(int i=0;i<len/2;i++){
temp=array[i];
array[i]=array[len-1-i];
array[len-1-i]=temp;
}
return array;
}
public static void main(String[] args){
System.out.println(cut("f:\\111.wav","f:\\111-cut_0_10.wav",0,10));
System.out.println(cut("f:\\111.wav","f:\\111-cut_10_20.wav",10,20));
System.out.println(cut("f:\\111.wav","f:\\111-cut_20_28.wav",20,28));
}
}
wave类型的音频文件切割时必须注意头信息,128kbps比特率的wave文件头信息占用44字节。
可以把头信息作为一个对象,用ByteBuffer获取头信息。
注意:wave文件的头信息字节数组中每个属性都进行了数组反转
wave头信息对象模型如下:
/**
* wave文件头信息
* @author lwj
*
*/
public class Head {
public int riff_id; //4 byte , 'RIFF'
public int file_size; //4 byte , 文件长度(数据长度+36)
public int riff_type; //4 byte , 'WAVE'
public int fmt_id; //4 byte , 'fmt'
public int fmt_size; //4 byte , 数值为16或18,18则最后又附加信息
public short fmt_tag; //2 byte , 编码方式,一般为0x0001
public short fmt_channel; //2 byte , 声道数目,1--单声道;2--双声道
public int fmt_samplesPerSec;//4 byte , 采样频率
public int avgBytesPerSec; //4 byte , 每秒所需字节数,记录每秒的数据量
public short blockAlign; //2 byte , 数据块对齐单位(每个采样需要的字节数)
public short bitsPerSample; //2 byte , 每个采样需要的bit数
public int data_id; //4 byte , 字符data
public int data_size; //4 byte , 数据长度
public int getRiff_id() {
return riff_id;
}
public void setRiff_id(int riff_id) {
this.riff_id = riff_id;
}
public int getFile_size() {
return file_size;
}
public void setFile_size(int file_size) {
this.file_size = file_size;
}
public int getRiff_type() {
return riff_type;
}
public void setRiff_type(int riff_type) {
this.riff_type = riff_type;
}
public int getFmt_id() {
return fmt_id;
}
public void setFmt_id(int fmt_id) {
this.fmt_id = fmt_id;
}
public int getFmt_size() {
return fmt_size;
}
public void setFmt_size(int fmt_size) {
this.fmt_size = fmt_size;
}
public short getFmt_tag() {
return fmt_tag;
}
public void setFmt_tag(short fmt_tag) {
this.fmt_tag = fmt_tag;
}
public short getFmt_channel() {
return fmt_channel;
}
public void setFmt_channel(short fmt_channel) {
this.fmt_channel = fmt_channel;
}
public int getFmt_samplesPerSec() {
return fmt_samplesPerSec;
}
public void setFmt_samplesPerSec(int fmt_samplesPerSec) {
this.fmt_samplesPerSec = fmt_samplesPerSec;
}
public int getAvgBytesPerSec() {
return avgBytesPerSec;
}
public void setAvgBytesPerSec(int avgBytesPerSec) {
this.avgBytesPerSec = avgBytesPerSec;
}
public short getBlockAlign() {
return blockAlign;
}
public void setBlockAlign(short blockAlign) {
this.blockAlign = blockAlign;
}
public short getBitsPerSample() {
return bitsPerSample;
}
public void setBitsPerSample(short bitsPerSample) {
this.bitsPerSample = bitsPerSample;
}
public int getData_id() {
return data_id;
}
public void setData_id(int data_id) {
this.data_id = data_id;
}
public int getData_size() {
return data_size;
}
public void setData_size(int data_size) {
this.data_size = data_size;
}
}
附件为wave切割程序所依赖的外部jar包: jave-1.0.2
希望本文所述对大家java程序设计有所帮助。
来源:https://lwjwd.iteye.com/blog/2025072
标签:java,音频文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot利用限速器RateLimiter实现单机限流的示例代码
2023-04-05 19:57:50
流读取导致StringBuilder.toString()乱码的问题及解决
2022-12-20 13:34:14
Java实战之用springboot+netty实现简单的一对一聊天
2023-12-03 07:28:19
![](https://img.aspxhome.com/file/2023/0/61080_0s.png)
java代码执行字符串中的逻辑运算方法
2023-11-29 12:13:06
Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码
2023-11-23 08:23:43
![](https://img.aspxhome.com/file/2023/5/59865_0s.png)
在Eclipse中使用版本管理工具SVN的图文教程
2022-07-21 17:39:51
![](https://img.aspxhome.com/file/2023/1/61541_0s.png)
List集合多个复杂字段判断去重的案例
2022-08-01 16:23:28
Android6.0 消息机制原理解析
2023-08-06 12:19:44
java实现文件的断点续传
2023-11-23 09:11:00
![](https://img.aspxhome.com/file/2023/4/59544_0s.jpg)
Mybatis SqlSessionFactory与SqlSession详细讲解
2021-12-24 22:42:56
![](https://img.aspxhome.com/file/2023/6/62086_0s.png)
Java面试必备八股文整理
2023-11-29 12:03:50
![](https://img.aspxhome.com/file/2023/8/59068_0s.jpg)
详解SpringMVC如何进行数据回显
2023-09-12 08:48:15
Spring Bean后处理器详细介绍
2021-06-27 07:29:06
![](https://img.aspxhome.com/file/2023/4/61854_0s.png)
Java之策略模式比较器案例讲解
2021-12-25 22:24:32
Flutter加载图片的多样玩法汇总
2023-08-24 09:48:22
![](https://img.aspxhome.com/file/2023/1/108291_0s.png)
一篇文章带你了解XGBoost算法
2021-10-10 01:14:45
![](https://img.aspxhome.com/file/2023/2/61082_0s.jpg)
Java ArrayList源码深入分析
2023-06-16 16:30:26
![](https://img.aspxhome.com/file/2023/8/60918_0s.png)
SpringBoot2.1.4中的错误处理机制
2023-11-06 02:48:47
![](https://img.aspxhome.com/file/2023/3/58923_0s.jpg)
Java8使用stream实现list中对象属性的合并(去重并求和)
2023-06-23 13:44:40
![](https://img.aspxhome.com/file/2023/9/62379_0s.png)
SpringBoot2.x 参数校验问题小结
2023-05-22 02:21:22
![](https://img.aspxhome.com/file/2023/7/61567_0s.png)