工具:

一个jar包即可:jave-1.0.2.jar

可以切割wav格式的音频文件

完整工程目录

就一个jar包,一个main类

代码:

package com.zit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer; import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
/**
* wav音频文件截取工具
* (适用于比特率为128kbps的wav音频文件,此类音频文件的头部信息占用长度44字节)
* @author 王晓东
*
*/
public class ReadVideo { public static void main(String[] args) {
// System.out.println(cut("E:\\record.mp3","E:\\record-cut_0_15.mp3", 0, 15)); //大于18(也可能别的值),会读取不了
// System.out.println(cut("E:\\record_40s.wav","E:\\record-cut_15_30.wav", 15, 30)); int start = 0;
int end = 0;
int count = 10;
String sourcefile = "E:\\test.wav";
long time = getTimeLen(new File(sourcefile));
int newTime = (int)time;
int internal = newTime - end;
while(internal > 0) {
if(internal < 10) {
cut(sourcefile, "E:\\record-cut_" + start + "_" + (int)time +".wav", start, (int)time);
end += count;
internal = newTime - end;
}else {
end += count;
cut(sourcefile, "E:\\record-cut_" + start + "_" + end +".wav", start, end);
start += count;
internal = newTime - end;
}
} } /**
* 截取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;
} }

  

 切割后的效果

最新文章

  1. Delphi按下F1不能出现帮助文档的解决方法
  2. Mozilla Firefox 24.0 Beta 5 发布
  3. JS 日期格式化和解析工具
  4. P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
  5. Bitmap的一些操作
  6. NET Core RC2 and .NET Core SDK Preview
  7. 自己写的sql排序
  8. Web Worker无阻塞UI的牛逼技术,html5,可惜无法敢于UI
  9. [刷题]Codeforces 786A - Berzerk
  10. 苹果APP发布
  11. AC的故事大结局山寨版(下)(最大流)
  12. Ftp、Ftps与Sftp之间的区别
  13. poi读取word的内容
  14. 作着玩:登录页(纯css,不支持ie9以下)
  15. Zabbix4.0添加端口和进程监控
  16. 彻底理解ReentrantLock
  17. PHP实现一个ip(如:127.0.0.1)和多个域名(虚拟主机)的绑定
  18. HDU 3065 病毒侵袭持续中(AC自动机)题解
  19. AC日记——#2054. 「TJOI / HEOI2016」树
  20. Git Hook 同步服务器代码

热门文章

  1. 大量的rcuob进程
  2. 【Alpha】Scrum Meeting 9
  3. EmberJS 为什么我偏爱 Ember.js 胜过 Angular 和 React.js
  4. ready
  5. SpringBoot和druid数据源集成Jpa
  6. flask No such command &quot;init-db&quot;.
  7. 数据结构与算法(C#)入门 --- 线性表
  8. Mybatis日志
  9. log4.net使用指南
  10. Python自学:第三章 使用方法sort( )对列表进行永久性排序