jave 音频剪辑支持mp3,wav

MP3

lib
jave依赖
https://download.csdn.net/download/weixin_44524757/20478070

源代码下载
源码
https://download.csdn.net/download/weixin_44524757/20478301

  1. 获取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-1System.out.println("声道: " + header.getChannels()); //声道System.out.println("采样率: " + header.getSampleRate()); //采样率System.out.println("MPEG: " + header.getMpegLayer()); //MPEGSystem.out.println("MP3起始字节: " + header.getMp3StartByte()); //MP3起始字节System.out.println("精确的音轨长度: " + header.getPreciseTrackLength()); //精确的音轨长度System.out.println("----------------Loading...Content-----------------");}
  1. 剪辑
    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=409600int 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;   //累积tatolif (tatol1 < start1) {  //tatol小于起始值则跳出本次循环continue;}bos.write(b1);   //写入的都是在我们预先指定的字节范围之内if (tatol1 >= end1) {  //当tatol的值超过预先设定的范围,则立刻刷新bos流对象,并结束循环bos.flush();break;}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();return false;} finally {try {//切记要关闭流!!if (bis1 != null) bis1.close();if (bos != null) bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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;}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部