4月14日个人博客
1.昨日完成了将MP3文件转换为pcm格式
2.难点:语音转文字只能对pcm,wav等格式,需要进行格式转换
3.今日完成长文件的剪切
4.
package org.example;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import javax.sound.sampled.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class ConvertMP32PCM {
/**
* MP3转换PCM文件方法
*
* @param mp3filepath
* 原始文件路径
* 转换文件的保存路径
* @throws Exception
*/
// public static void convertMP32PCM(String mp3filepath, String pcmfilepath) throws Exception {
// AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
// AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
// }
public static String convertMP32PCM(String mp3filepath) throws Exception {
AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
String pcmfilepath = mp3filepath.substring(0,mp3filepath.length()-3) +"pcm";
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, new File(pcmfilepath));
return pcmfilepath;
}
/**
* 播放MP3方法
*
* @param mp3filepath
* @throws Exception
*/
public static void playMP3(String mp3filepath) throws Exception {
File mp3 = new File(mp3filepath);
// 播放
int k = 0, length = 8192;
AudioInputStream audioInputStream = getPcmAudioInputStream(mp3filepath);
if (audioInputStream == null)
System.out.println("null audiostream");
AudioFormat targetFormat;
targetFormat = audioInputStream.getFormat();
byte[] data = new byte[length];
DataLine.Info dinfo = new DataLine.Info(SourceDataLine.class, targetFormat);
SourceDataLine line = null;
try {
line = (SourceDataLine) AudioSystem.getLine(dinfo);
line.open(targetFormat);
line.start();
int bytesRead = 0;
byte[] buffer = new byte[length];
while ((bytesRead = audioInputStream.read(buffer, 0, length)) != -1) {
line.write(buffer, 0, bytesRead);
}
audioInputStream.close();
line.stop();
line.close();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("audio problem " + ex);
}
}
private static AudioInputStream getPcmAudioInputStream(String mp3filepath) {
File mp3 = new File(mp3filepath);
AudioInputStream audioInputStream = null;
AudioFormat targetFormat = null;
try {
AudioInputStream in = null;
MpegAudioFileReader mp = new MpegAudioFileReader();
in = mp.getAudioInputStream(mp3);
AudioFormat baseFormat = in.getFormat();
targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
audioInputStream = AudioSystem.getAudioInputStream(targetFormat, in);
} catch (Exception e) {
e.printStackTrace();
}
return audioInputStream;
}
public static void mp3Convertpcm(String mp3filepath,String pcmfilepath) throws Exception{
File mp3 = new File(mp3filepath);
File pcm = new File(pcmfilepath);
//原MP3文件转AudioInputStream
AudioInputStream mp3audioStream = AudioSystem.getAudioInputStream(mp3);
//将AudioInputStream MP3文件 转换为PCM AudioInputStream
AudioInputStream pcmaudioStream = AudioSystem.getAudioInputStream(AudioFormat.Encoding.PCM_SIGNED, mp3audioStream);
//准备转换的流输出到OutputStream
OutputStream os = new FileOutputStream(pcm);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead=pcmaudioStream.read(buffer, 0, 8192))!=-1) {
os.write(buffer, 0, bytesRead);
}
os.close();
pcmaudioStream.close();
}
public static void main(String[] args) {
String mp3filepath = "D:\\mp3\\1.mp3";
String pcmfilepath = "D:\\mp3\\1.pcm";
try {
// ConvertMP32PCM.convertMP32PCM(mp3filepath, pcmfilepath);
// ConvertMP32PCM.playMP3(mp3filepath);
mp3Convertpcm(mp3filepath,pcmfilepath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
// 预先设置一个变量来存MediaRecorder实例
let mediaRecorder = null;
// 首先打开麦克风
function record() {
navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => {
let chunks = [];
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
// 录音开始事件监听(即调用 mediaRecorder.start()时会触发该事件)
mediaRecorder.onstart = () => {
console.log("record start");
};
// 录音可用事件监听,发生于mediaRecorder.stop()调用后,mediaRecorder.onstop 前
mediaRecorder.ondataavailable = (e) => {
console.log("dataavailable");
console.log(e);
chunks.push(e.data);
};
// 录音结束事件监听,发生在mediaRecorder.stop()和 mediaRecorder.ondataavailable 调用后
mediaRecorder.onstop = () => {
console.log("record end");
// 获取到录音的blob
let blob = new Blob(chunks, { type: "audio/webm;codecs=opus" });
console.log("🚀 ~ file: record.js:27 ~ navigator.mediaDevices.getUserMedia ~ blob:", blob);
// var saveDatas = getDataFromLocal("results");
// // 上面是拿到我自己的数据,数据的格式是Json字符串
// var newBlob = new Blob([JSON.stringify(saveDatas)], { type: "application/json" });
// 创建一个blob的对象,把Json转化为字符串作为我们的值
let url = window.URL.createObjectURL(blob);
// 上面这个是创建一个blob的对象连链接,
// 然后创建一个链接元素,是属于 a 标签的链接元素,所以括号里才是a,
let link = document.createElement("a");
link.href = url;
// 把上面获得的blob的对象链接赋值给新创建的这个 a 链接
link.setAttribute("download", "录音.wav");
// 设置下载的属性(所以使用的是download),这个是a 标签的一个属性
// 后面的是文件名字,可以更改
link.click();
// 使用js点击这个链接
// 将blob转换为file对象,名字可以自己改,一般用于需要将文件上传到后台的情况
// let file = new window.File([blob], "record.webm");
// 将blob转换为地址,一般用于页面上面的回显,这个url可以直接被 audio 标签使用
// let url = (window.URL || webkitURL).createObjectURL(blob);
};
});
}
// 录音结束事件,在需要结束录音时调用,录音结束后的操作请在 mediaRecorder.onstop 里面写
function stopRecord() {
mediaRecorder && mediaRecorder.stop();
}
function showLocale(objD) {
var str, colorhead, colorfoot;
var yy = objD.getYear();
if (yy < 1900)
yy = yy + 1900;
var MM = objD.getMonth() + 1;
if (MM < 10)
MM = '0' + MM;
var dd = objD.getDate();
if (dd < 10)
dd = '0' + dd;
var hh = objD.getHours();
if (hh < 10)
hh = '0' + hh;
var mm = objD.getMinutes();
if (mm < 10)
mm = '0' + mm;
var ss = objD.getSeconds();
if (ss < 10)
ss = '0' + ss;
var ww = objD.getDay();
colorhead = "<font color=\"#000000\">";
if (ww == 0)
ww = "星期日";
if (ww == 1)
ww = "星期一";
if (ww == 2)
ww = "星期二";
if (ww == 3)
ww = "星期三";
if (ww == 4)
ww = "星期四";
if (ww == 5)
ww = "星期五";
if (ww == 6)
ww = "星期六";
colorfoot = "</font>"
str = colorhead + yy + "-" + MM + "-" + dd + " " + hh + ":" + mm
+ ":" + ss + " " + ww + colorfoot;
return (str);
}
function tick() {
var today;
today = new Date();
document.getElementById("localtime").innerHTML = showLocale(today);
window.setTimeout("tick()", 1000);
}
window.onload = function() {
tick();
}
function showTab(tabHeadId,tabContentId)
{
var tabDiv = document.getElementById("tabDiv");
var taContents = tabDiv.childNodes;
for(i=0; i<taContents.length; i++)
{
if(taContents[i].id!=null && taContents[i].id != 'tabsHead')
{
taContents[i].style.display = 'none';
}
}
document.getElementById('tabContent1').style.display='none';
document.getElementById('tabContent2').style.display='none';
document.getElementById(tabContentId).style.display = 'block';
var tabHeads = document.getElementById('tabsHead').getElementsByTagName('a');
for(i=0; i<tabHeads.length; i++)
{
tabHeads[i].className='tabs';
}
document.getElementById(tabHeadId).className='curtab';
document.getElementById(tabHeadId).blur();
}
var audio_context;
var recorder;
function start(button) {
navigator.mediaDevices.getUserMedia({ audio: true }).then(function (stream) {
var input = new AudioContext().createMediaStreamSource(stream);
recorder = new Recorder(input);
recorder.record();
});
button.disabled = true;
button.nextElementSibling.disabled = false;
}
function stop(button) {
recorder && recorder.stop();
button.disabled = true;
button.previousElementSibling.disabled = false;
createDownloadLink();
recorder.clear();
}
*/
浙公网安备 33010602011771号