1.Java切割录音,目前我用到的是java的原生Jar包,并没有用到框架,接下来我就把代码分享一下 首先了解整体过程:如果你的音频文件是wav就直接切割,如果你的音频文件是mp3,先转换成wav格式然后进行切割:

1: Maven 依赖
<dependency><groupId>it.sauronsoftware</groupId><artifactId>jave</artifactId><version>1.0.2</version></dependency><dependency><groupId>org</groupId><artifactId>jaudiotagger</artifactId><version>2.0.3</version></dependency><dependency><groupId>com.googlecode.soundlibs</groupId><artifactId>mp3spi</artifactId><version>1.9.5.4</version></dependency>

用到的方法工具类(下面有main方法测试)

@Component
@Slf4j
public class FileTimeUtils {public static Double getFileTime(String fileName) {//获取录音文件总时长File file = null;String netUrl = fileName;if (netUrl.startsWith("https://")) {file = FileUtils.getNetUrlHttp(netUrl);} else {file = FileUtils.getNetUrlHttp(netUrl);}Encoder encoder = new Encoder();MultimediaInfo m = null;try {m = encoder.getInfo(file);} catch (EncoderException e) {log.error(e + "获取录音文件时间长度报错!请检查");e.printStackTrace();}long duration = m.getDuration();long s = duration / 1000;return (double) s;}/*** 截取wav音频文件** @param source 源文件地址* @param targetFile 目标文件地址* @param start  截取开始时间(秒)* @param end    截取结束时间(秒)*               <p>*               return  截取成功返回true,否则返回false*/public static boolean  cut(String source, String targetFile, int start, int end) {try {if (!source.toLowerCase().endsWith(".wav") || !targetFile.toLowerCase().endsWith(".wav")) {return false;}File wav = new File(source);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);//音频数据大小(44为128kbps比特率wav文件头长度)long wavSize = wav.length() - 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));//存放文件大小,4代表一个int占用字节数ByteBuffer buf1 = ByteBuffer.allocate(4);//放入文件长度信息buf1.putInt(splitSizeInt + 36);//代表文件长度byte[] flen = buf1.array();//存放音频数据大小,4代表一个int占用字节数ByteBuffer buf2 = ByteBuffer.allocate(4);//放入数据长度信息buf2.putInt(splitSizeInt);//代表数据长度byte[] dlen = buf2.array();//数组反转flen = reverse(flen);dlen = reverse(dlen);//定义wav头部信息数组byte[] head = new byte[44];//读取源wav文件头部信息fis.read(head, 0, head.length);/*** 4代表一个int占用字节数* 替换原头部信息里的文件长度* 替换原头部信息里的数据长度*/for (int i = 0; i < 4; i++) {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;}/*** 获取音频文件总时长** @param file 文件路径* @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 ;} catch (Exception e) {e.printStackTrace();}}return tlen;}/*** 数组反转** @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;}/*** mp3的字节数组生成wav文件** @param sourceBytes* @param targetPath*/public static boolean byteToWav(byte[] sourceBytes, String targetPath) {if (sourceBytes == null || sourceBytes.length == 0) {System.out.println("Illegal Argument passed to this method");return false;}try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes); final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) {AudioFormat sourceFormat = sourceAIS.getFormat();// 设置MP3的语音格式,并设置16bitAudioFormat mp3tFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false);// 设置百度语音识别的音频格式AudioFormat pcmFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16000, 16, 1, 2, 16000, false);try (// 先通过MP3转一次,使音频流能的格式完整final AudioInputStream mp3AIS = AudioSystem.getAudioInputStream(mp3tFormat, sourceAIS);// 转成百度需要的流final AudioInputStream pcmAIS = AudioSystem.getAudioInputStream(pcmFormat, mp3AIS)) {// 根据路径生成wav文件AudioSystem.write(pcmAIS, AudioFileFormat.Type.WAVE, new File(targetPath));}return true;} catch (IOException e) {System.out.println("文件转换异常:" + e.getMessage());return false;} catch (UnsupportedAudioFileException e) {System.out.println("文件转换异常:" + e.getMessage());return false;}}/*** 将文件转成字节流** @param filePath* @return*/public static byte[] getBytes(String filePath) {byte[] buffer = null;try {File file = new File(filePath);FileInputStream fis = new FileInputStream(file);ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);byte[] b = new byte[1000];int n;while ((n = fis.read(b)) != -1) {bos.write(b, 0, n);}fis.close();bos.close();buffer = bos.toByteArray();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return buffer;}public static void main(String[] args) {/*String source = "D:/test/test.mp3";String targetPath = "D:/test/11111.wav";byteToWav(getBytes(source), targetPath);boolean cut = cut(targetPath, "D:/test/22222.wav", 0, 10);System.out.println(cut);*//* File file = new File("D:/test/11111.wav");String path = file.getPath();String[] split =path.split("/");System.out.println(split);System.out.println(path);*/String s = "[0,54930]";String[] split = s.split(",");String substring = split[1].substring(0, split[1].length() - 1);System.out.println(split[1]);System.out.println(substring);} ```//接下来是转换的代码。这里的切割方法只能针对wav格式,但是现在有很多音频文件是mp3个格式的;/*** mp3的字节数组生成wav文件** @param sourceBytes* @param targetPath*/public static boolean byteToWav(byte[] sourceBytes, String targetPath) {if (sourceBytes == null || sourceBytes.length == 0) {System.out.println("Illegal Argument passed to this method");return false;}try (final ByteArrayInputStream bais = new ByteArrayInputStream(sourceBytes); final AudioInputStream sourceAIS = AudioSystem.getAudioInputStream(bais)) {AudioFormat sourceFormat = sourceAIS.getFormat();// 设置MP3的语音格式,并设置16bitAudioFormat mp3tFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(), sourceFormat.getChannels() * 2, sourceFormat.getSampleRate(), false);// 设置百度语音识别的音频格式AudioFormat pcmFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 16000, 16, 1, 2, 16000, false);try (// 先通过MP3转一次,使音频流能的格式完整final AudioInputStream mp3AIS = AudioSystem.getAudioInputStream(mp3tFormat, sourceAIS);// 转成百度需要的流final AudioInputStream pcmAIS = AudioSystem.getAudioInputStream(pcmFormat, mp3AIS)) {// 根据路径生成wav文件AudioSystem.write(pcmAIS, AudioFileFormat.Type.WAVE, new File(targetPath));}return true;} catch (IOException e) {System.out.println("文件转换异常:" + e.getMessage());return false;} catch (UnsupportedAudioFileException e) {System.out.println("文件转换异常:" + e.getMessage());return false;}}

Java切割录音文件相关推荐

  1. java切割音频文件

    java切割音频文件:https://blog.csdn.net/weixin_34023863/article/details/93792055

  2. java 上传mp3文件大小,Java获取音频文件(MP3)的播放时长

    最近的一个项目需要按照时间播放mp3文件,例如,播放10分钟的不同音乐. 这就意味着我得事先知道mp3文件的播放时长,以决定播放几遍这个文件. 方案一:Java的方式 找第三方的库,真的感谢这些提供j ...

  3. java切割输入流_java IO流之文件切割两例(含Properties 用法)

    package cn.itcast.io.p1.splitfile; import java.io.File; import java.io.FileInputStream; import java. ...

  4. java 对音频文件降噪_如何有效的对录音文件进行降噪处理?

    原标题:如何有效的对录音文件进行降噪处理? 在电脑上录音的时候,总会不小心录入一些乱七八糟的声音,那么应该如何处理这些杂音呢?今天小编在这里重点跟大家讨论讨论,分享一个即简单又快捷的方法给到大家,走过 ...

  5. php切割音频文件,我想将一段录音中部分剪出来, 如何剪辑(截取)音频文件

    如果想把一段录音截取后保留自己想要的部分该怎么做? 如何剪辑(截取)音频文件?也可以引申用来做自己的个性铃声,电脑开关机音乐.现在编辑软件很多,这里给大家分享一个音频剪辑(截取)软件,眼见为实,下面就 ...

  6. java audiorecord_Android 录音实现(AudioRecord)

    上一篇文章介绍了使用 MediaRecorder 实现录音功能 Android录音实现(MediaRecorder) ,下面我们继续看看使用 AudioRecord 实现录音功能. AudioReco ...

  7. JAVA写的文件分割与文件合并程序

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 原来觉得 ...

  8. Java输出PPT文件(三) - 饼图数据替换

    Java输出PPT文件(三) - 饼图数据替换 文章目录 Java输出PPT文件(三) - 饼图数据替换 0. 前言 1. 依赖 2. 代码 3. 测试 3.1 饼图数据 3.2 模板准备 3.3 替 ...

  9. JAVA实现CSV文件转JSON。

    JAVA实现CSV文件转JSON. CSV文件一般是以逗号为分隔值的文件(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存 ...

最新文章

  1. cnblogs不愧为cnblogs
  2. c语言判断2 1000素数,2是不是素数(C语言判断一个数为素数)
  3. sse php,sse.php · Gitee 极速下载/modphp - Gitee.com
  4. T-SQL 编程之结果集循环处理
  5. Dapper.Common基于Dapper的开源LINQ超轻量扩展
  6. vant按需引入没样式_vue vant-ui样式出不来的问题
  7. WEB可以调节的框架页
  8. LINUX环境搭建:安装中文定制版UBUNTU 10.10
  9. php+fpm+apache
  10. WordPress资源站点推荐
  11. 全球DEM下载 90米、30米、12.5米等各种精度DEM数据
  12. python3d立体相册代码_Python 30 行代码画各种 3D 图形
  13. ESXI7.0与6.7官网下载地址
  14. 鸭子应用--策略模式
  15. 微博商业数据挖掘方法
  16. 简易http服务器的实现(实现)httpserver.c
  17. 四旋翼无人机动力学模型及控制
  18. Python+uiautomator2指定区域截图
  19. Django搭建简单网站
  20. 文件传输工具, 手机电脑都能用 - 收集

热门文章

  1. 安装Broadcom Linux hybrid 无线网卡驱动总结
  2. flash cache tier下放flush实验
  3. 锐龙R5 3400G配什么主板
  4. oracle培训教材
  5. ftk的python binding
  6. 怎么在exc中用计算机,如何在Excel中使用计数功能
  7. UR机械臂学习(5-2):使用Universal_Robots_ROS_Driver驱动真实机械臂
  8. 数据结构三级项目c++代码,基于有向有权图的信息管理系统
  9. 13 微积分——级数
  10. hdu6060斯坦纳树