pcm转mp3
你好! 大家好~上次发布了一篇JAVA SpringBoot接科大讯飞TTS语音合成
的文章,然后好多小伙伴发现科大讯飞生成的pcm文件,无法直接用并不是大家熟知的mp3,4这类格式,于是应大家要求我这边发布一篇pcm转mp3的工具类。
废话不说上代码
package com.stla.callCenter.utility;
import com.stla.callCenter.bean.WaveHeader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* PCM 转 MP3
*
* @author laowang
* @since 2020-04-02
*/
public class PcmToMp3 {
/**
* @param src 待转换文件路径
* @param target 目标文件路径(这个工具是在源文件的基础上再形成一个文件,target样例:D:/landscape/music/25745664665456.mp3,必须写全否则报io‘无法访问错误’)
* @throws IOException 抛出异常
*/
public static String convertAudioFiles(String src, String target) throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(target);
//计算长度
byte[] buf = new byte[1024 * 4];
int size = fis.read(buf);
int PCMSize = 0;
while (size != -1) {
PCMSize += size;
size = fis.read(buf);
}
fis.close();
//填入参数,比特率等等。这里用的是16位单声道 8000 hz
WaveHeader header = new WaveHeader();
//长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节)
header.fileLength = PCMSize + (44 - 8);
header.FmtHdrLeth = 16;
header.BitsPerSample = 16;
header.Channels = 1;
header.FormatTag = 0x0001;
header.SamplesPerSec = 16000;
header.BlockAlign = (short) (header.Channels * header.BitsPerSample / 8);
header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec;
header.DataHdrLeth = PCMSize;
byte[] h = header.getHeader();
assert h.length == 44; //WAV标准,头部应该是44字节
//write header
fos.write(h, 0, h.length);
//write data stream
fis = new FileInputStream(src);
size = fis.read(buf);
while (size != -1) {
fos.write(buf, 0, size);
size = fis.read(buf);
}
fis.close();
fos.close();
System.out.println("PCM Convert to MP3 OK!");
return "ok";
}
public static void main(String[] args) throws IOException {
convertAudioFiles("D:\\123123.pcm","D:\\tts_test3.mp3");
}
}
接口层
PcmToMp3.convertAudioFiles(“xxx.pcm”,“xxx.mp3”);