package com.bytx.ai.service.base.controller.util;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo; 
 
import java.io.File; 
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; 
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.SequenceInputStream;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.bytx.ai.service.utility.common.CodeConstans;
 
@RestController
@RequestMapping(value = "/cut", method = RequestMethod.POST, produces = CodeConstans.JSON_UTF_8)
public class WavCut { 
   
    /**
     * 截取wav音频文件
     * @param sourcepath  源文件地址
     * @param targetpath  目标文件地址
     * @param start  截取开始时间(秒)
     * @param end  截取结束时间(秒)
     *  这个方法是截取选中某段音频开始之前的音频分割
     * return  截取成功返回true,否则返回false
     */ 
 
 @RequestMapping(value = "/cutUtil")
    public static boolean cut(String sourcefile, String targetfile, double start, double end) { 
        try{ 
            if(!sourcefile.toLowerCase().endsWith(".mp3") || !targetfile.toLowerCase().endsWith(".mp3")){ 
                return false; 
            } 
            File wav = new File(sourcefile); 
            if(!wav.exists()){ 
                return false; 
            } 
            double t1 = getTimeLen(wav);  //总时长(秒) 
            if(start<0 || end<=0 || start>=t1 || end>t1 || start>=end){ 
                return false; 
            } 
            cut1(sourcefile, targetfile, start, end);
           
            FileInputStream fis = new FileInputStream(wav); 
            long wavSize = wav.length()-44;  //音频数据大小(44为128kbps比特率wav文件头长度) 
            long splitSize = (long)((wavSize/t1)*start);  //截取的音频数据大小 
             
            long skipSize = (long)((wavSize/t1)*0);  //截取时跳过的音频数据大小 
         //   long skipSize = (long)((wavSize/t1)*(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(); 
            hebin();
        }catch(IOException e){ 
            e.printStackTrace(); 
            return false; 
        } 
     
        return true; 
      
    } 
  /**
   
     * 截取音频结束end后的音频部分
     */ 
    public static boolean cut1(String sourcefile, String targetfile, double start, double end) { 
        try{ 
           /* if(!sourcefile.toLowerCase().endsWith(".mp3") || !targetfile.toLowerCase().endsWith(".mp3")){ 
                return false; 
            } 
*/
         StringBuffer targetfile1 = new StringBuffer(targetfile);
         StringBuffer replace = targetfile1.replace(3, 4, "a");
         String replace11 = replace.toString();
         System.out.println(replace);
            File wav = new File(sourcefile); 
            if(!wav.exists()){ 
                return false; 
            } 
            double 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 = (long)((wavSize/t1)*(t1-end));  //截取的音频数据大小 
             
         //   long skipSize = (long)((wavSize/t1)*0);  //截取时跳过的音频数据大小 
            long skipSize = (long)((wavSize/t1)*end);  //截取时跳过的音频数据大小 
            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(replace11); 
            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 void hebin() throws IOException {
     FileInputStream fis1 = new FileInputStream("F:\\Rec00010.mp3");
  FileInputStream fis2 = new FileInputStream("F:\\6.mp3");//你需要添加音频的中间部分
  FileInputStream fis3 = new FileInputStream("F:\\1.mp3");//你需要添加音频的中间部分
  FileInputStream fis4 = new FileInputStream("F:\\8.mp3");//你需要添加音频的中间部分
  FileInputStream fis5 = new FileInputStream("F:\\6.mp3");//你需要添加音频的中间部分
  FileInputStream fis6 = new FileInputStream("F:\\aec00010.mp3");
 
  ArrayList<FileInputStream> c = new ArrayList<FileInputStream>();
  c.add(fis1);
  c.add(fis2);
  c.add(fis3);
  c.add(fis4);
  c.add(fis5);
  c.add(fis6);
  Enumeration<FileInputStream> e = Collections.enumeration(c);
  SequenceInputStream sis = new SequenceInputStream(e);
  
  //创建输出流---要把前三个文件的内容读出来并且合并到seq4.txt;
  FileOutputStream fos=new FileOutputStream("F:\\d5.mp3");
  int len=0;
  byte buf[] = new byte[10];
  while((len=sis.read(buf))!=-1){
   fos.write(buf, 0, len);
  }
  fos.close();
  sis.close();
    }
     
    /**
     * 获取音频文件总时长
     * @param filePath  文件路径
     * @return
     */ 
    public static double getTimeLen(File file){ 
        double tlen = 0; 
        if(file!=null && file.exists()){ 
            Encoder encoder = new Encoder(); 
            try { 
                 MultimediaInfo m = encoder.getInfo(file); 
                 double ls = m.getDuration(); 
                 tlen = ls/1000; 
            } 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; 
    } 
     
    public static void main(String[] args) throws ParseException, IOException{ 
     long startTime = System.currentTimeMillis(); //获取开始时间
      //he();
        System.out.println(cut("C:\\Users\\13671\\Desktop\\yin\\宽带新增开场白2.mp3","F:\\Rec00010.mp3",8.8,9.5)); 
     long endTime = System.currentTimeMillis(); //获取结束时间
     System.out.println("程序运行时间:" + (endTime - startTime) + "ms"); //输出程序运行时间
       // 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)); 
    /* String aString= "1.013";
     double a=Double.parseDouble(aString);
     System.out.println(a);
     double b=1000;
     double  c=(a*b);
     System.out.println(c);
     int i= (int)c;
        System.out.println(i);*/
       
    
    }
    } 
   

MP3音频文件的剪切合并方法的更多相关文章

  1. C# 使用NAudio合并mp3、wav音频文件

    1.什么是wav格式    WAV为微软公司(Microsoft)开发的一种声音文件格式,它符合RIFF(Resource Interchange File Format)文件规范,用于保存Windo ...

  2. VC++中MCI播放音频文件 【转】

    MCI播放mp3音频文件例程 源文件中需要包含头文件 Mmsystem.h,在Project->Settings->Link->Object/libray module中加入库 Wi ...

  3. HTML5 audio 如何实现播放多个MP3音频

    <audio>标签是HTML5中的新标签,定义声音用于嵌入音频内容,比如音乐或其他音频流.用的比较多音频格式是.mp3. <audio>标签常用属性如下表 属性 值 描述 au ...

  4. 吴裕雄--天生自然python学习笔记:python 用pygame模块处理音频文件

    除了对图片. Word 等普通格式的文件进行处理外, Python 还有强大的多媒体文件操作能力,如对音频.视频 文件的操作 . 如果要播放音乐,我们可以用 pygame 包中的 mixer 对 象. ...

  5. 获取google翻译的音频文件_合并音频文件的方法

    1. 把引文输入google 翻译,然后点击"朗读"

  6. .net amr格式文件转换成mp3格式文件的方法

    前言:winform端对于音频文件的格式多有限制,大多数不支持amr格式的文件的播放.但是,手机端传过来的音频文件大多数是amr格式的文件,所以,要想在winform客户端支持音频文件的播放,可以通过 ...

  7. PHP 将amr音频文件转换为mp3格式

    说下整体思路 1.服务器安装ffmpeg 2.使用ffmpeg -i 指令来转换amr为mp3格式(这个到时候写在PHP代码中,使用exec函数执行即可) 3.在网页端使用HTML5的audio标签来 ...

  8. CEF3 HTML5 audio标签为什么不能播放mp3格式的音频文件

    CEF3 HTML5 audio标签 为什么不能播放mp3格式的音频文件   原因略.   解决方法: 找一个最新版的chrome ,我用的是24版本.路径 C:\Documents and Sett ...

  9. 合并BIN文件的两种方法(转)

    源:http://blog.chinaunix.net/uid-20745340-id-1878803.html 合并BIN文件的两种方法 在单片机的开发过程中,经常需要将两个单独的BIN文件合并成一 ...

随机推荐

  1. NERDTree快捷键

    切换工作台和目录 ctrl + w + h 光标 focus 左侧树形目录ctrl + w + l 光标 focus 右侧文件显示窗口ctrl + w + w 光标自动在左右侧窗口切换ctrl + w ...

  2. C++与引用2

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  3. 9——PHP循环结构foreach用法

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  4. 大厂面试官问你META-INF/spring.factories要怎么实现自动扫描、自动装配?

    大厂面试官问你META-INF/spring.factories要怎么实现自动扫描.自动装配?   很多程序员想面试进互联网大厂,但是也有很多人不知道进入大厂需要具备哪些条件,以及面试官会问哪些问题, ...

  5. 从头认识js-基本概念(关键字,保留字,数据类型)

    语法 ECMAScript的语法大量借鉴了C及其他类C语言(如Java和Perl)的语法.因此,熟悉这些语言的开发人员在接受ECMSAScript更加宽松的语法时,一定会有一种轻松自在的感觉. 区分大 ...

  6. 7-6 jmu_python_最大公约数&最小公倍数 (10 分)

    本题要求从键盘输入两个整数(以逗号间隔),编程求出这两个数的最大公约数和最小公倍数 提示:求最大公约数可用辗转相除法,最小公倍数用两数的积除以最大公约数 输入格式: 在一行中输入两个整数,以逗号间隔 ...

  7. JDK 1.8 新特性之Date-Time API

    来源:请点击查看 1.8之前的日期类: 线程不安全:java.util.Date 这个类线程不安全,而且所有日期类都是可变的. 时间处理麻烦:默认的开始日期从1900年,不支持国际化,不提供时区支持, ...

  8. Canny检测算法与实现

    1.原理 图象边缘就是图像颜色快速变化的位置,对于灰度图像来说,也就是灰度值有明显变化的位置.图像边缘信息主要集中在高频段,图像锐化或检测边缘实质就是高通滤波.数值微分可以求变化率,在图像上离散值求梯 ...

  9. django 验证码图片生成视图函数

    def verify_code(request): import random # 定义验证码图片背景颜色 宽和高 bgcolor = (random.randrange(20,180),random ...

  10. 这些MongoDB的隐藏操作你真的都掌握了吗?反正我是刚知道

    背景 最近公司系统还原用户时偶尔会出现部分用户信息未还原成功的问题,最为开发人员,最头疼的不是代码存在bug,而是测试发现了bug,但一旦我去重现,它就不见了.Are you kidding me? ...