Home > Java > Java Tutorial > body text

Sample code for how Java uses IO streams to implement audio cutting and splicing

黄舟
Release: 2017-06-04 09:21:17
Original
3548 people have browsed it

这篇文章主要为大家详细介绍了Java使用IO流实现音频的剪切和拼接,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

思路(以两首歌为例):

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

实现代码:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * 音乐剪切和拼接(音乐串烧)
 * @author 
 *
 */
public class CutMusic {

 public static void main(String[] args) {
  //f1,f2分别为需要剪切的歌曲路径
   File f1 = new File("E:\\CutMusicTest\\残酷月光(Cover:林宥嘉).mp3");
  File f2 = new File("E:\\CutMusicTest\\慢慢.mp3");
  //f为合并的歌曲
  File f = new File("E:\\CutMusicTest\\MergeMusic.mp3");
  cut1(f1,f2,f);
 }
 
 public static void cut1(File f1,File f2,File f){
  BufferedInputStream bis1 = null;
  BufferedInputStream bis2 = null;
  BufferedOutputStream bos = null;
  //第一首歌剪切部分起始字节
  int start1 = 2375680;//320kbps(比特率)*58s*1024/8=2375680 比特率可以查看音频属性获知
  int end1 = 4915200;//320kbps*120s*1024/8=4915200
  
  //第二首歌剪切部分起始字节,计算方式同上
  int start2 = 3686400;
  int end2 = 5324800;
  
  int tatol1 = 0;
  int tatol2 = 0;
  try {
   //两个输入流
   bis1 = new BufferedInputStream(new FileInputStream(f1));
   bis2 = new BufferedInputStream(new FileInputStream(f2));
   //缓冲字节输出流(true表示可以在流的后面追加数据,而不是覆盖!!)
   bos = new BufferedOutputStream(new FileOutputStream(f,true));
   
   //第一首歌剪切、写入
   byte[] b1= new byte[512];
   int len1 = 0;
   while((len1 = bis1.read(b1))!=-1){
    tatol1+=len1;   //累积tatol
    if(tatol1=end1 ){  //当tatol的值超过预先设定的范围,则立刻刷新bos流对象,并结束循环
     bos.flush();
     break;
    }
    
   }
   System.out.println("第一首歌剪切完成!");
   
   //第二首歌剪切、写入,原理同上
   byte[] b2= new byte[512];
   int len2 = 0;
   while((len2 = bis2.read(b2))!=-1){
    tatol2 += len2; 
    if(tatol2 < start2){ 
     continue;
    }
    bos.write(b2);  
    if(tatol2>=end2){ 
     bos.flush();
     break;
    }
    
   }
   System.out.println("第二首歌剪切完成!");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
    try {//切记要关闭流!!
     if(bis1!=null) bis1.close();
     if(bis2!=null) bis2.close();
     if(bos!=null) bos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  }
 }

}
Copy after login

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

运行结果:

The above is the detailed content of Sample code for how Java uses IO streams to implement audio cutting and splicing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!