Java切割录音文件

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的语音格式,并设置16bit
            AudioFormat 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的语音格式,并设置16bit
            AudioFormat 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;
        }
    }




  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java中,可以使用Java Sound API来切割音频文件。下面是一个简单的示例代码,演示如何使用Java Sound API来切割音频文件: ```java import javax.sound.sampled.*; import java.io.File; import java.io.IOException; public class AudioCutter { public static void main(String[] args) { String inputFile = "input.wav"; // 输入音频文件路径 String outputFile = "output.wav"; // 输出音频文件路径 int startSecond = 10; // 起始时间(秒) int endSecond = 20; // 结束时间(秒) try { AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(inputFile)); AudioFormat format = audioInputStream.getFormat(); long frames = audioInputStream.getFrameLength(); double durationInSeconds = (frames + 0.0) / format.getFrameRate(); // 计算起始和结束帧数 int startFrame = (int) (startSecond * format.getFrameRate()); int endFrame = (int) (endSecond * format.getFrameRate()); // 限制起始和结束帧数在有效范围内 startFrame = Math.max(0, startFrame); endFrame = Math.min((int) frames, endFrame); // 计算切割后的帧数 int cutLength = endFrame - startFrame; // 创建切割后的音频流 AudioInputStream cutAudioInputStream = new AudioInputStream(audioInputStream, format, cutLength); AudioSystem.write(cutAudioInputStream, AudioFileFormat.Type.WAVE, new File(outputFile)); System.out.println("音频切割成功!"); } catch (UnsupportedAudioFileException | IOException e) { e.printStackTrace(); } } } ``` 上述代码中,我们首先使用`AudioSystem.getAudioInputStream()`方法获取音频文件的输入流。然后,我们获取音频的格式和帧数,并根据起始和结束时间计算出对应的起始和结束帧数。接下来,我们使用`AudioInputStream`的构造函数创建一个切割后的音频流,并使用`AudioSystem.write()`方法将切割后的音频流写入到输出文件中。 请注意,上述代码仅适用于切割WAV格式的音频文件。如果要切割其他格式的音频文件,需要相应地调整代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值