最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)切割wav音頻文件的方法詳解【附外部jar包下載】

 更新時(shí)間:2019年05月16日 10:58:57   作者:lwjwd  
這篇文章主要介紹了java實(shí)現(xiàn)切割wav音頻文件的方法,結(jié)合實(shí)例形式詳細(xì)分析了java切割wav音頻文件的相關(guān)原理、操作技巧與注意事項(xiàng),并附帶外部jar包供讀者下載,需要的朋友可以參考下

本文實(shí)例講述了java實(shí)現(xiàn)切割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音頻文件,此類音頻文件的頭部信息占用長(zhǎng)度44字節(jié))
 * @author lwj
 *
 */
public class WavCut {
  /**
   * 截取wav音頻文件
   * @param sourcepath 源文件地址
   * @param targetpath 目標(biāo)文件地址
   * @param start 截取開(kāi)始時(shí)間(秒)
   * @param end 截取結(jié)束時(shí)間(秒)
   *
   * 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); //總時(shí)長(zhǎng)(秒)
      if(start<0 || end<=0 || start>=t1 || end>t1 || start>=end){
        return false;
      }
      FileInputStream fis = new FileInputStream(wav);
      long wavSize = wav.length()-44; //音頻數(shù)據(jù)大?。?4為128kbps比特率wav文件頭長(zhǎng)度)
      long splitSize = (wavSize/t1)*(end-start); //截取的音頻數(shù)據(jù)大小
      long skipSize = (wavSize/t1)*start; //截取時(shí)跳過(guò)的音頻數(shù)據(jù)大小
      int splitSizeInt = Integer.parseInt(String.valueOf(splitSize));
      int skipSizeInt = Integer.parseInt(String.valueOf(skipSize));
      ByteBuffer buf1 = ByteBuffer.allocate(4); //存放文件大小,4代表一個(gè)int占用字節(jié)數(shù)
      buf1.putInt(splitSizeInt+36); //放入文件長(zhǎng)度信息
      byte[] flen = buf1.array(); //代表文件長(zhǎng)度
      ByteBuffer buf2 = ByteBuffer.allocate(4); //存放音頻數(shù)據(jù)大小,4代表一個(gè)int占用字節(jié)數(shù)
      buf2.putInt(splitSizeInt); //放入數(shù)據(jù)長(zhǎng)度信息
      byte[] dlen = buf2.array(); //代表數(shù)據(jù)長(zhǎng)度
      flen = reverse(flen); //數(shù)組反轉(zhuǎn)
      dlen = reverse(dlen);
      byte[] head = new byte[44]; //定義wav頭部信息數(shù)組
      fis.read(head, 0, head.length); //讀取源wav文件頭部信息
      for(int i=0; i<4; i++){ //4代表一個(gè)int占用字節(jié)數(shù)
        head[i+4] = flen[i]; //替換原頭部信息里的文件長(zhǎng)度
        head[i+40] = dlen[i]; //替換原頭部信息里的數(shù)據(jù)長(zhǎng)度
      }
      byte[] fbyte = new byte[splitSizeInt+head.length]; //存放截取的音頻數(shù)據(jù)
      for(int i=0; i<head.length; i++){ //放入修改后的頭部信息
        fbyte[i] = head[i];
      }
      byte[] skipBytes = new byte[skipSizeInt]; //存放截取時(shí)跳過(guò)的音頻數(shù)據(jù)
      fis.read(skipBytes, 0, skipBytes.length); //跳過(guò)不需要截取的數(shù)據(jù)
      fis.read(fbyte, head.length, fbyte.length-head.length); //讀取要截取的數(shù)據(jù)到目標(biāo)數(shù)組
      fis.close();
      File target = new File(targetfile);
      if(target.exists()){ //如果目標(biāo)文件已存在,則刪除目標(biāo)文件
        target.delete();
      }
      FileOutputStream fos = new FileOutputStream(target);
      fos.write(fbyte);
      fos.flush();
      fos.close();
    }catch(IOException e){
      e.printStackTrace();
      return false;
    }
    return true;
  }
  /**
   * 獲取音頻文件總時(shí)長(zhǎng)
   * @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;
  }
  /**
  * 數(shù)組反轉(zhuǎn)
  * @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類型的音頻文件切割時(shí)必須注意頭信息,128kbps比特率的wave文件頭信息占用44字節(jié)。

可以把頭信息作為一個(gè)對(duì)象,用ByteBuffer獲取頭信息。

注意:wave文件的頭信息字節(jié)數(shù)組中每個(gè)屬性都進(jìn)行了數(shù)組反轉(zhuǎn)

wave頭信息對(duì)象模型如下:

/**
 * wave文件頭信息
 * @author lwj
 *
 */
public class Head {
  public int riff_id;      //4 byte , 'RIFF'
  public int file_size;     //4 byte , 文件長(zhǎng)度(數(shù)據(jù)長(zhǎng)度+36)
  public int riff_type;     //4 byte , 'WAVE'
  public int fmt_id;      //4 byte , 'fmt'
  public int fmt_size;     //4 byte , 數(shù)值為16或18,18則最后又附加信息
  public short fmt_tag;     //2 byte , 編碼方式,一般為0x0001
  public short fmt_channel;   //2 byte , 聲道數(shù)目,1--單聲道;2--雙聲道
  public int fmt_samplesPerSec;//4 byte , 采樣頻率
  public int avgBytesPerSec;  //4 byte , 每秒所需字節(jié)數(shù),記錄每秒的數(shù)據(jù)量
  public short blockAlign;   //2 byte , 數(shù)據(jù)塊對(duì)齊單位(每個(gè)采樣需要的字節(jié)數(shù))
  public short bitsPerSample;  //2 byte , 每個(gè)采樣需要的bit數(shù)
  public int data_id;      //4 byte , 字符data
  public int data_size;     //4 byte , 數(shù)據(jù)長(zhǎng)度
  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

更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • java方法重寫實(shí)例分析

    java方法重寫實(shí)例分析

    這篇文章主要介紹了java方法重寫,較為詳細(xì)的講述了Java方法重寫的注意事項(xiàng),并附帶實(shí)例加以說(shuō)明,需要的朋友可以參考下
    2014-09-09
  • Java函數(shù)式編程(十二):監(jiān)控文件修改

    Java函數(shù)式編程(十二):監(jiān)控文件修改

    這篇文章主要介紹了Java函數(shù)式編程(十二):監(jiān)控文件修改,本文是系列文章的第12篇,其它文章請(qǐng)參閱本文底部的相關(guān)文章,需要的朋友可以參考下
    2014-09-09
  • spring boot如何基于JWT實(shí)現(xiàn)單點(diǎn)登錄詳解

    spring boot如何基于JWT實(shí)現(xiàn)單點(diǎn)登錄詳解

    這篇文章主要介紹了spring boot如何基于JWT實(shí)現(xiàn)單點(diǎn)登錄詳解,用戶只需登錄一次就能夠在這兩個(gè)系統(tǒng)中進(jìn)行操作。很明顯這就是單點(diǎn)登錄(Single Sign-On)達(dá)到的效果,需要的朋友可以參考下
    2019-06-06
  • MybatisPlus條件查詢的具體使用

    MybatisPlus條件查詢的具體使用

    MybatisPlus通過(guò)條件構(gòu)造器可以組裝復(fù)雜的查詢條件,本文主要介紹了MybatisPlus條件查詢的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • Java filter中的chain.doFilter使用詳解

    Java filter中的chain.doFilter使用詳解

    這篇文章主要介紹了Java filter中的chain.doFilter使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 使用Logback日志保存到相對(duì)路徑的操作

    使用Logback日志保存到相對(duì)路徑的操作

    這篇文章主要介紹了使用Logback日志保存到相對(duì)路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鏈路追蹤功能

    SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鏈路追蹤功能

    Spring Boot默認(rèn)使用LogBack日志系統(tǒng),并且已經(jīng)引入了相關(guān)的jar包,所以我們無(wú)需任何配置便可以使用LogBack打印日志。這篇文章主要介紹了SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鏈路追蹤功能,需要的朋友可以參考下
    2019-10-10
  • 新手初學(xué)Java常見(jiàn)排序算法

    新手初學(xué)Java常見(jiàn)排序算法

    排序(Sorting) 是計(jì)算機(jī)程序設(shè)計(jì)中的一種重要操作,它的功能是將一個(gè)數(shù)據(jù)元素(或記錄)的任意序列,重新排列成一個(gè)關(guān)鍵字有序的序列
    2021-07-07
  • maven多模塊工程打包部署的方法步驟

    maven多模塊工程打包部署的方法步驟

    本篇文章主要介紹了maven多模塊工程打包部署的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • java設(shè)計(jì)模式-裝飾者模式詳解

    java設(shè)計(jì)模式-裝飾者模式詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式之裝飾者模式詳解和代碼實(shí)例,Decorator模式(別名Wrapper):動(dòng)態(tài)將職責(zé)附加到對(duì)象上,若要擴(kuò)展功能,裝飾者提供了比繼承更具彈性的代替方案,需要的朋友可以參考下
    2021-07-07

最新評(píng)論

垣曲县| 绥芬河市| 陵水| 梁河县| 宜黄县| 分宜县| 邵武市| 昌都县| 凤山县| 桐乡市| 清流县| 宿松县| 赤峰市| 大港区| 金川县| 衡阳县| 鄂托克旗| 涿鹿县| 米脂县| 嘉兴市| 玉门市| 明溪县| 岳普湖县| 醴陵市| 辽阳县| 夏河县| 鄄城县| 涟源市| 海南省| 左权县| 虎林市| 大方县| 沁源县| 绥化市| 赣州市| 睢宁县| 高雄市| 平乐县| 墨竹工卡县| 舒兰市| 灵山县|