Java使用IO流实现音频的剪切和拼接

2025-05-29 0 68

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

思路(以两首歌为例):

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

实现代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94
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流实现音频的剪切和拼接

运行结果:

Java使用IO流实现音频的剪切和拼接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java使用IO流实现音频的剪切和拼接 https://www.kuaiidc.com/116028.html

相关文章

发表评论
暂无评论