MP3
lib
jave依赖
https://download.csdn.net/download/weixin_44524757/20478070
源代码下载
源码
https://download.csdn.net/download/weixin_44524757/20478301
- 获取mp3信息
private static void getMp3Head(String mp3Path) {
MP3File mp3File = null;//封装好的类
try {
mp3File = new MP3File(mp3Path);
} catch (Exception e) {
e.printStackTrace();
System.out.println("获取文件失败");
return;
}
System.out.println("----------------Loading...Head-----------------");
MP3AudioHeader header = mp3File.getMP3AudioHeader();
System.out.println("时长: " + header.getTrackLength()); //获得时长
System.out.println("比特率: " + header.getBitRateAsNumber()); //获得比特率
System.out.println("音轨长度: " + header.getTrackLength()); //音轨长度
System.out.println("格式: " + header.getFormat()); //格式,例 MPEG-1
System.out.println("声道: " + header.getChannels()); //声道
System.out.println("采样率: " + header.getSampleRate()); //采样率
System.out.println("MPEG: " + header.getMpegLayer()); //MPEG
System.out.println("MP3起始字节: " + header.getMp3StartByte()); //MP3起始字节
System.out.println("精确的音轨长度: " + header.getPreciseTrackLength()); //精确的音轨长度
System.out.println("----------------Loading...Content-----------------");
}
- 剪辑
public static boolean cut(File sou, String targetFile, int start, int end, int kbps) {
File out = new File(targetFile);
try {
if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs();
}
boolean p = out.createNewFile();
System.out.println("创建文件:{" + p + "}");
} catch (Exception e) {
e.printStackTrace();
return false;
}
BufferedInputStream bis1 = null;
BufferedOutputStream bos = null;
//剪切部分起始字节
int start1 = start * kbps * 128;//128kbps(比特率)*20s*1024/8=327680 比特率可以查看音频属性获知
int end1 = end * kbps * 128;//128kbps*25s*1024/8=409600
int tatol1 = 0;
try {
//输入流
bis1 = new BufferedInputStream(new FileInputStream(sou));
//缓冲字节输出流(true表示可以在流的后面追加数据,而不是覆盖!!)
bos = new BufferedOutputStream(new FileOutputStream(out, false));
//剪切、写入
byte[] b1 = new byte[1024];
int len1 = 0;
while ((len1 = bis1.read(b1)) != -1) {
tatol1 += len1; //累积tatol
if (tatol1 < start1) { //tatol小于起始值则跳出本次循环
continue;
}
bos.write(b1); //写入的都是在我们预先指定的字节范围之内
if (tatol1 >= end1) { //当tatol的值超过预先设定的范围,则立刻刷新bos流对象,并结束循环
bos.flush();
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} finally {
try {//切记要关闭流!!
if (bis1 != null) bis1.close();
if (bos != null) bos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
wav
剪辑
public static boolean cut(File wav, String targetFile, int start, int end ) throws IOException {
try{
RandomAccessFile src = new RandomAccessFile(wav, "r");
int headSize= getHeadSize(src);
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 wavSize = wav.length()-headSize; //音频数据大小(wav文件头长度不一定是44)
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头部信息数组
byte[] head = new byte[headSize];
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]; //替换原头部信息里的数据长度
head[i+headSize-4] = 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;
}