zoukankan      html  css  js  c++  java
  • 使用IO流实现音频的剪切和拼接

    需求: 使用IO流将指定目录下的若干个音频文件的高潮部分,进行剪切,并重新拼接成一首新的音频文件


    思路(以两首歌为例):

      第一首歌有一个输入流对象bis1。第二首歌有一个输入流对象bis2,他们公用一条输出流对象bos(在选择构造方法的时候选择含有布尔类型参数的那个),待第一首歌剪切完成后,在此基础上追加第二首歌的“高潮部分”。


    实现代码:


     1 import java.io.BufferedInputStream;
     2 import java.io.BufferedOutputStream;
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 import java.io.InputStream;
     9 /**
    10  * 音乐剪切和拼接(音乐串烧)
    11  * @author 
    12  *
    13  */
    14 public class CutMusic {
    15 
    16     public static void main(String[] args) {
    17         //f1,f2分别为需要剪切的歌曲路径
    18          File f1 = new File("E:\CutMusicTest\残酷月光(Cover:林宥嘉).mp3");
    19         File f2 = new File("E:\CutMusicTest\慢慢.mp3");
    20         //f为合并的歌曲
    21         File f = new File("E:\CutMusicTest\MergeMusic.mp3");
    22         cut1(f1,f2,f);
    23     }
    24     
    25     public static void cut1(File f1,File f2,File f){
    26         BufferedInputStream bis1 = null;
    27         BufferedInputStream bis2 = null;
    28         BufferedOutputStream bos = null;
    29         //第一首歌剪切部分起始字节
    30         int start1 = 2375680;//320kbps(比特率)*58s*1024/8=2375680   比特率可以查看音频属性获知
    31         int end1 = 4915200;//320kbps*120s*1024/8=4915200
    32         
    33         //第二首歌剪切部分起始字节,计算方式同上
    34         int start2 = 3686400;
    35         int end2 = 5324800;
    36         
    37         int tatol1 = 0;
    38         int tatol2 = 0;
    39         try {
    40             //两个输入流
    41             bis1 = new BufferedInputStream(new FileInputStream(f1));
    42             bis2 = new BufferedInputStream(new FileInputStream(f2));
    43             //缓冲字节输出流(true表示可以在流的后面追加数据,而不是覆盖!!)
    44             bos = new BufferedOutputStream(new FileOutputStream(f,true));
    45             
    46             //第一首歌剪切、写入
    47             byte[] b1= new byte[512];
    48             int len1 = 0;
    49             while((len1 = bis1.read(b1))!=-1){
    50                 tatol1+=len1;            //累积tatol
    51                 if(tatol1<start1 ){        //tatol小于起始值则跳出本次循环
    52                     continue;
    53                 }
    54                 bos.write(b1);            //写入的都是在我们预先指定的字节范围之内
    55                 if(tatol1>=end1 ){        //当tatol的值超过预先设定的范围,则立刻刷新bos流对象,并结束循环
    56                     bos.flush();
    57                     break;
    58                 }
    59                 
    60             }
    61             System.out.println("第一首歌剪切完成!");
    62             
    63             //第二首歌剪切、写入,原理同上
    64             byte[] b2= new byte[512];
    65             int len2 = 0;
    66             while((len2 = bis2.read(b2))!=-1){
    67                 tatol2 += len2;    
    68                 if(tatol2 < start2){    
    69                     continue;
    70                 }
    71                 bos.write(b2);        
    72                 if(tatol2>=end2){    
    73                     bos.flush();
    74                     break;
    75                 }
    76                 
    77             }
    78             System.out.println("第二首歌剪切完成!");
    79         } catch (IOException e) {
    80             // TODO Auto-generated catch block
    81             e.printStackTrace();
    82         }finally{
    83                 try {//切记要关闭流!!
    84                     if(bis1!=null) bis1.close();
    85                     if(bis2!=null) bis2.close();
    86                     if(bos!=null) bos.close();
    87                 } catch (IOException e) {
    88                     // TODO Auto-generated catch block
    89                     e.printStackTrace();
    90                 }
    91         }
    92     }
    93 
    94 }

    获取音频文件比特率的方式:


    运行结果:

  • 相关阅读:
    output在delete中的应用
    静态什么时候用?
    Main函数解析
    构造函数
    Main函数解析
    java类类型
    静态使用的注意事项
    Main函数剖析
    成员变量和局部变量的区别
    static的特点
  • 原文地址:https://www.cnblogs.com/Mus-Li/p/6683373.html
Copyright © 2011-2022 走看看