4月14日个人博客
1.昨日完成了将MP3文件转换为pcm格式
2.难点:语音转文字只能对pcm,wav等格式,需要进行格式转换
3.今日完成长文件的剪切
4.
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | 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(); } */ |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!