java根據時間切割音訊
阿新 • • 發佈:2018-10-31
import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.EncoderException; import it.sauronsoftware.jave.MultimediaInfo; import java.io.*; import java.nio.ByteBuffer; /** * FileName: WavCut * Author: xuan zongjun * Date: 2018/10/18 10:45 * Description: wav視訊切割 */ public class WavCut { /** * @param sourcefile * @param targetfile * @param start * @param end * @return */ 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; } 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; } 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) { String fileName ="D:\\wav\\pcmpcmpcm.wav"; String targetName1 ="D:\\wav2\\pcmpcmpcm1.wav"; String targetName2 ="D:\\wav2\\pcmpcmpcm2.wav"; boolean result1 = cut(fileName,targetName1,0,5); boolean result3 = cut(fileName,targetName2,5,9); System.out.println(result1); System.out.println(result3); }
}