Java使用IO流实现音频的剪切和拼接
作者:沐易笙 时间:2023-09-11 08:47:46
需求: 使用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<start1 ){ //tatol小于起始值则跳出本次循环
continue;
}
bos.write(b1); //写入的都是在我们预先指定的字节范围之内
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();
}
}
}
}
获取音频文件比特率的方式:
运行结果:
标签:java,IO流,音频
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解DES加密算法的原理与Java实现
2021-06-22 06:56:19
![](https://img.aspxhome.com/file/2023/4/110904_0s.png)
java 读写Parquet格式的数据的示例代码
2022-09-16 11:09:47
![](https://img.aspxhome.com/file/2023/1/98121_0s.png)
Android仿腾讯视频实现悬浮窗效果
2021-06-16 00:49:23
![](https://img.aspxhome.com/file/2023/8/110728_0s.gif)
java 中sendredirect()和forward()方法的区别
2021-11-07 18:39:28
java实现简单的俄罗斯方块
2021-08-02 18:13:11
![](https://img.aspxhome.com/file/2023/4/71184_0s.jpg)
Struts2中异常处理机制分析
2023-11-17 21:54:32
Android编程基于距离传感器控制手机屏幕熄灭的方法详解
2022-02-20 10:49:05
Spring Data Jpa的四种查询方式详解
2021-10-10 10:35:26
![](https://img.aspxhome.com/file/2023/3/81293_0s.png)
Jackson的用法实例分析
2021-11-13 09:13:40
C# PictureBox图片控件实现图片交换
2022-12-05 17:09:10
![](https://img.aspxhome.com/file/2023/5/126025_0s.png)
java 发送邮件的实例代码(可移植)
2022-09-23 15:53:44
MyBatis查询结果resultType返回值类型的说明
2021-10-25 21:37:39
![](https://img.aspxhome.com/file/2023/9/79169_0s.jpg)
Kotlin 协程与挂起函数及suspend关键字深入理解
2023-09-19 01:04:25
![](https://img.aspxhome.com/file/2023/5/117105_0s.jpg)
Android kotlin使用注解实现防按钮连点功能的示例
2023-07-02 11:58:06
AndroidStduio3.0 使用gradle将module打包jar文件的方法
2023-07-01 06:57:20
![](https://img.aspxhome.com/file/2023/4/84224_0s.png)
修改idea的这些启动参数,令你的idea健步如飞
2021-11-14 14:48:11
![](https://img.aspxhome.com/file/2023/2/77402_0s.jpg)
Android 游戏开发中绘制游戏触摸轨迹的曲线图
2023-02-26 08:50:30
Android编程实现下载图片及在手机中展示的方法
2023-10-26 12:30:59
![](https://img.aspxhome.com/file/2023/8/139158_0s.png)
Java关键字finally_动力节点Java学院整理
2022-09-25 11:15:30
浅谈C# StringBuilder内存碎片对性能的影响
2023-12-15 08:55:47