音频流转mp3格式

  1. function convertToMp3(wavDataView) {
  2. // 获取wav头信息
  3. const wav = lamejs.WavHeader.readHeader(wavDataView);
  4. const { channels, sampleRate } = wav;
  5. const mp3enc = new lamejs.Mp3Encoder(channels, sampleRate, 128);
  6. // 获取左右通道数据
  7. const result = recorder.getChannelData();
  8. const buffer = [];
  9. const leftData = result.left && new Int16Array(result.left.buffer, 0, result.left.byteLength / 2);
  10. const rightData = result.right && new Int16Array(result.right.buffer, 0, result.right.byteLength / 2);
  11. const remaining = leftData.length + (rightData ? rightData.length : 0);
  12. const maxSamples = 1152;
  13. for (let i = 0; i < remaining; i += maxSamples) {
  14. const left = leftData.subarray(i, i + maxSamples);
  15. let right = null;
  16. let mp3buf = null;
  17. if (channels === 2) {
  18. right = rightData.subarray(i, i + maxSamples);
  19. mp3buf = mp3enc.encodeBuffer(left, right);
  20. } else {
  21. mp3buf = mp3enc.encodeBuffer(left);
  22. }
  23. if (mp3buf.length > 0) {
  24. buffer.push(mp3buf);
  25. }
  26. }
  27. const enc = mp3enc.flush();
  28. if (enc.length > 0) {
  29. buffer.push(enc);
  30. }
  31. let blob = new Blob(buffer, {
  32. type: "audio/mp3",
  33. });
  34. return blob;
  35. }

获取 Gop 大小(关键帧间隔)

  1. <script src="https://j.weizan.cn/zhibo/js/jquery-2.1.4.min.js"></script>
  2. $.ajax({
  3. url: video_src, // 视频播放地址
  4. dataType: "TEXT",
  5. success: (res) => {
  6. // 解析 m3u8
  7. var m3u8list = res.replace(/\n/g, '<br>').split('<br>');
  8. // 获取 Gop 大小
  9. var gopSize = parseInt(m3u8list[3].split(':').pop());
  10. console.log('gop size:' + gopSize);
  11. }
  12. })