Commit 6b2f2ce9 authored by 周亚武's avatar 周亚武

修改合并mp3文件规则

parent b2c7abe9
...@@ -698,7 +698,8 @@ public class MainWindow { ...@@ -698,7 +698,8 @@ public class MainWindow {
} }
audioList.add(people+"kk.mp3"); audioList.add(people+"kk.mp3");
String url = SpliceMp3Util.heBingMp3(audioList); // String url = SpliceMp3Util.heBingMp3(audioList);
String url = AddMp3Util.mergeMp3s(audioList);
if(url != null){ if(url != null){
if(speaker.equals("en")){ if(speaker.equals("en")){
...@@ -848,7 +849,8 @@ public class MainWindow { ...@@ -848,7 +849,8 @@ public class MainWindow {
} }
audioList.add(people+"kk.mp3"); audioList.add(people+"kk.mp3");
String url = SpliceMp3Util.heBingMp3(audioList); // String url = SpliceMp3Util.heBingMp3(audioList);
String url = AddMp3Util.mergeMp3s(audioList);
if(url != null){ if(url != null){
if(english){ if(english){
......
package com.mortals.xhx.tts.utils;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
* @author ZYW
* @date 2024-08-23 9:19
*/
public class AddMp3Util {
public static String mergeMp3s(List<String> inputFiles) {
String luJing = "/tmp/";
if(System.getProperty("os.name").startsWith("Windows")){ //windows电脑测试路径
luJing= "F:\\\\";
}
File f=new File(luJing);
f.mkdirs();
//生成处理后的文件
String outputFile = luJing+"new.mp3";
ProcessBuilder pb = new ProcessBuilder("sox");
pb.command().addAll(inputFiles);
pb.command().add(outputFile);
try {
Process process = pb.start();
process.waitFor();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return outputFile;
}
}
...@@ -99,115 +99,7 @@ public class SpliceMp3Util { ...@@ -99,115 +99,7 @@ public class SpliceMp3Util {
return file2.getAbsolutePath(); return file2.getAbsolutePath();
} }
/**
* 分离出数据帧每一帧的大小并存在list数组里面
*失败则返回空
* @param path
* @return
* @throws IOException
*/
public static List<Integer> initMP3Frame(String path) {
File file = new File(path);
List<Integer> list = new ArrayList<>();
/* int framSize=0;
RandomAccessFile rad = new RandomAccessFile(file, "rw");
byte[] head = new byte[4];
rad.seek(framSize);
rad.read(head);
int bitRate = getBitRate((head[2] >> 4) & 0x0f) * 1000;
int sampleRate = getsampleRate((head[2] >> 2) & 0x03);
int paing = (head[2] >> 1) & 0x01;
int len = 144 * bitRate / sampleRate + paing;
for(int i=0,lens=(int)(file.length())/len;i<lens;i++){
list.add(len);// 将数据帧的长度添加进来
}*/
int framSize = 0;
RandomAccessFile rad = null;
try {
rad = new RandomAccessFile(file, "rw");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (framSize < file.length()) {
byte[] head = new byte[4];
try {
rad.seek(framSize);
} catch (IOException e) {
e.printStackTrace();
}
try {
rad.read(head);
} catch (IOException e) {
e.printStackTrace();
}
int bitRate = getBitRate((head[2] >> 4) & 0x0f) * 1000;
int sampleRate = getsampleRate((head[2] >> 2) & 0x03);
int paing = (head[2] >> 1) & 0x01;
if(bitRate==0||sampleRate==0)return null;
int len = 144 * bitRate / sampleRate + paing;
list.add(len);// 将数据帧的长度添加进来
framSize += len;
}
return list;
}
/**
* 返回切割后的MP3文件的路径 返回null则切割失败 开始时间和结束时间的整数部分都是秒,以秒为单位
*
*
* @param list
* @param startTime
* @param stopTime
* @return
* @throws IOException
*/
public static String CutingMp3(String path, String name,
List<Integer> list, double startTime, double stopTime)
throws IOException {
File file = new File(path);
String luJing="/storage/emulated/0/"+"HH音乐播放器/切割/";
File f=new File(luJing);
f.mkdirs();
int start = (int) (startTime / 0.026);
int stop = (int) (stopTime / 0.026);
if ((start > stop) || (start < 0) || (stop < 0) || (stop > list.size())) {
return null;
} else {
long seekStart = 0;// 开始剪切的字节的位置
for (int i = 0; i < start; i++) {
seekStart += list.get(i);
}
long seekStop = 0;// 结束剪切的的字节的位置
for (int i = 0; i < stop; i++) {
seekStop += list.get(i);
}
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(seekStart);
File file1 = new File(luJing + name + "(HH切割).mp3");
FileOutputStream out = new FileOutputStream(file1);
byte[] bs=new byte[(int)(seekStop-seekStart)];
raf.read(bs);
out.write(bs);
raf.close();
out.close();
File filed=new File(path);
if(filed.exists())
filed.delete();
return file1.getAbsolutePath();
}
}
private static int getBitRate(int i) {
int a[] = {0,32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224,
256, 320,0 };
return a[i];
}
private static int getsampleRate(int i) {
int a[] = { 44100, 48000, 32000,0 };
return a[i];
}
/** /**
* 返回合并后的文件的路径名,默认放在第一个文件的目录下 * 返回合并后的文件的路径名,默认放在第一个文件的目录下
* @return * @return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment