*/
可以鼠标右键---属性 查看“有声读物 - 弟子规 - 童声朗诵版。mp3”文件的大小
点击详细信息查看比特率 此MP3的比特率为128kbps 即是128*1024bps。
例如想截取的音乐段是“有声读物 - 弟子规 - 童声朗诵版。mp3”的第10s~34s
方法一:
因为比特率是指每秒传送的比特(bit)数。 1B(Byte)=8b(bit) 。
所以“有声读物 - 弟子规 - 童声朗诵版。mp3” 1秒的字节数=比特率/8=128*1024/8=16384 B
则10s的字节数为16384*10B , 34s的字节数为16384*34=557056B
方法二:
MP3总字节数a/.总时间b=求的字节数c/ 想截取的时间点d
a=19,152,896 字节 b=19分56秒=1196s d=10s 求得c等于16015
两个方法相差的字节数不大。我试过都行。
运行下列代码即可截取出”弟子规1.MP3“
package file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CutMusic { public static void main(String[] args) { BufferedInputStream bis = null;//缓冲区 BufferedOutputStream bos = null; int start = 163840; // 开始的字节数 int end = 16384*34;// 结束的字节数
try {//通过字节输入流、字节输出流操作数据 bos = new BufferedOutputStream(new FileOutputStream("d:\\弟子规.mp3"));生成的文件 bis = new BufferedInputStream(new FileInputStream("d:\\有声读物 - 弟子规 .mp3"));//读取的文件 byte[] b = new byte[1024]; int len = 0; int total = 0;
每次读1024字节,只要len不等于-1则继续读 while ((len = bis.read(b)) != -1) { total += len; // 如果小于开始字节的就抛弃掉 if (total < start) { continue; } bos.write(b); // 如果大于 结尾字节数 就终止读取 if (total >= end) { bos.flush();满足条件则刷新一次 break; } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bos != null) bos.close();//关闭输出流 if (bis != null) bis.close();// 关闭输入流 } catch (IOException e) { e.printStackTrace(); } } }
}
bos = new BufferedOutputStream(new FileOutputStream("d:\\弟子规.mp3"));生成的文件
改掉括号里面的字符串名即可生成新的MP3
最后合并你截取的MP3
方法一:
public class CombineMusic { public static void main(String[] args) { BufferedInputStream bis1 = null; BufferedInputStream bis2 = null; BufferedOutputStream bos = null; SequenceInputStream si = null; try { bos = new BufferedOutputStream(new FileOutputStream("弟子规合.mp3",true)); bis1 = new BufferedInputStream(new FileInputStream("弟子规1.mp3")); bis2 = new BufferedInputStream(new FileInputStream("弟子规2.mp3")); si = new SequenceInputStream(bis1,bis2); byte[] b = new byte[1024]; int len = 0; while ((len = si.read(b)) != -1) { bos.write(b,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (si != null) si.close(); if (bis1 != null) bis1.close(); if (bis2 != null) bis2.close(); } catch (IOException e) { e.printStackTrace(); } } } }
方法二:
package file; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CombineMusic { public static void main(String[] args) { BufferedInputStream bis1 = null; BufferedInputStream bis2 = null; BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream("弟子规合.mp3",true)); bis1 = new BufferedInputStream(new FileInputStream("弟子规1.mp3")); bis2 = new BufferedInputStream(new FileInputStream("弟子规2.mp3")); byte[] b = new byte[1024]; int len = 0; int total = 0; while ((len = bis1.read(b)) != -1) { //读弟子规1.mp3" bos.write(b);先写弟子规1.mp3" } while ((len = bis2.read(b)) != -1) { //读弟子规.mp3" bos.write(b);//再写弟子规.mp3" } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bos != null) bos.close(); if (bis1 != null) bis1.close(); if (bis2 != null) bis2.close(); } catch (IOException e) { e.printStackTrace(); } } } }