Java 實現音訊新增自定義時長靜音(附程式碼) | Java工具類
前言 wav音訊新增自定義時長靜音的工具類 Maven依賴 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>30.1.1-jre</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.5.2</version> </dependency> 程式碼 package ai.guiji.csdn.tools; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.URLUtil; import com.google.common.base.Joiner; import com.google.common.primitives.Bytes; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.LongStream; /** java專案 fhadmin.cn **/ public class WavAddSilenceUtils { /** * 根據PCM檔案構建wav的header欄位 * * @param srate Sample rate - 8000, 16000, etc. * @param channel Number of channels - Mono = 1, Stereo = 2, etc.. * @param format Number of bits per sample (16 here) * @throws IOException */ public static byte[] buildWavHeader(int dataLength, int srate, int channel, int format) throws IOException { byte[] header = new byte[44]; long totalDataLen = dataLength + 36; long bitrate = srate * channel * format; header[0] = 'R'; header[1] = 'I'; header[2] = 'F'; header[3] = 'F'; header[4] = (byte) (totalDataLen & 0xff); header[5] = (byte) ((totalDataLen >> 8) & 0xff); header[6] = (byte) ((totalDataLen >> 16) & 0xff); header[7] = (byte) ((totalDataLen >> 24) & 0xff); header[8] = 'W'; header[9] = 'A'; header[10] = 'V'; header[11] = 'E'; header[12] = 'f'; header[13] = 'm'; header[14] = 't'; header[15] = ' '; header[16] = (byte) format; header[17] = 0; header[18] = 0; header[19] = 0; header[20] = 1; header[21] = 0; header[22] = (byte) channel; header[23] = 0; header[24] = (byte) (srate & 0xff); header[25] = (byte) ((srate >> 8) & 0xff); header[26] = (byte) ((srate >> 16) & 0xff); header[27] = (byte) ((srate >> 24) & 0xff); header[28] = (byte) ((bitrate / 8) & 0xff); header[29] = (byte) (((bitrate / 8) >> 8) & 0xff); header[30] = (byte) (((bitrate / 8) >> 16) & 0xff); header[31] = (byte) (((bitrate / 8) >> 24) & 0xff); header[32] = (byte) ((channel * format) / 8); header[33] = 0; header[34] = 16; header[35] = 0; header[36] = 'd'; header[37] = 'a'; header[38] = 't'; header[39] = 'a'; header[40] = (byte) (dataLength & 0xff); header[41] = (byte) ((dataLength >> 8) & 0xff); header[42] = (byte) ((dataLength >> 16) & 0xff); header[43] = (byte) ((dataLength >> 24) & 0xff); return header; } /** * 預設寫入的pcm資料是16000取樣率,16bit,可以按照需要修改 * * @param filePath * @param pcmData */ public static boolean writeToFile(String filePath, byte[] pcmData) { BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] header = buildWavHeader(pcmData.length, 16000, 1, 16); bos.write(header, 0, 44); bos.write(pcmData); bos.close(); return true; } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } /** * 增加延時靜音 * * @param filePath 音訊地址 * @param tmpDirPath 臨時目錄地址 * @param delayTime 延時時長,單位毫秒 * @return 最終檔案地址 * @throws Exception 異常 */ public static String delayWav(String filePath, String tmpDirPath, Long delayTime) throws Exception { byte[] bytes; if (filePath.startsWith("http")) { bytes = IoUtil.readBytes(URLUtil.getStream(new URL(filePath))); } else { bytes = FileUtil.readBytes(filePath); } List<Byte> resultByte = new ArrayList<>(Bytes.asList(Arrays.copyOfRange(bytes, 44, bytes.length))); LongStream.range(0, (long) (delayTime * 32)).forEach(x -> resultByte.add((byte) 0)); String resultPath = Joiner.on(File.separator).join(Arrays.asList(tmpDirPath, IdUtil.simpleUUID() + ".wav")); writeToFile(resultPath, Bytes.toArray(resultByte)); return resultPath; } public static void main(String[] args) throws Exception { System.out.println(delayWav("\\Users\\huyi\\Desktop\\", 10000L)); } } 程式碼說明: 1、delayWav方法引數分別為wav音訊檔案地址(可以支援http的url地址)、臨時檔案目錄地址、延時靜音時長(單位毫秒) 2、對wav的要求預設為:取樣率16K、單聲道。 對需要處理的音訊引數調整。 3、生成uuid的隨機檔名,避免重複。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31558068/viewspace-2854291/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 穿越介面的5元6次齊次方程式
- Java高階語法之反射
- 程式設計師去哪接私活?分享10個兼職平臺,人已賺麻
- 口袋清理Pocket cleaner Pro Mac - 電腦垃圾清理工具
- Linux詳解 --- 程式管理
- 那些年我玩過的程式語言(三)
- ModStartCMS 模組化萬能建站系統 v3.1.0 API 介面支援,小程式上線
- 直播系統程式碼,選擇分類時,顯示更多下拉框效果的內容
- SQL 程式設計思想:一切皆關係
- Java安全之C3P0利用與分析
- Java JUC ReentrantLock解析
- 一款免費使用的PDF檔案批量合併工具,可操作pdf檔案順序,按順序合併pdf檔案。
- 位元組碼層面深入分析Java列舉類
- 聯合體在微控制器程式設計中的應用
- sketchup pro 2022,3D建築設計工具
- JavaScript 之 Proxy