惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
V
V2EX
S
Security Affairs
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
J
Java Code Geeks
The Register - Security
The Register - Security
U
Unit 42
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Project Zero
Project Zero
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
博客园 - 司徒正美
V
Vulnerabilities – Threatpost
T
Tor Project blog
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Palo Alto Networks Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta

博客园 - 进军的蜗牛

浏览器音频入门:录音生成 PCM、单声道/立体声、采样率、转 MP3 Web 语音场景下多路麦克风采集与音量异常:成因与两种工程取舍 从一次权限错误,彻底搞懂Mac权限管理那些事 js实现svga编辑器 Chrome浏览器普通模式访问页面报错 ERR_CONNECTION_REFUSED 问题排查 Web语音聊天室中录音无声问题分析与解决方案 蓝湖SVG上传Iconfont阿里图标库部分元素缺失问题分析与解决方案 前端测试新体验:Cypress让UI测试变得如此简单 iOS H5页面高度闪烁问题与100vh兼容性详解 为什么录音生成的音频文件有时长但没声音?—— 一次音频采集问题的排查与解决 前端实现采集图片某个区域的颜色色值 浏览器多Tab间通信方法全面总结:从WebView需求出发的技术方案 github https方式 push无法提交代码的处理方法 前端如何计算js代码执行时长 基于swiper和animate.css的全屏H5动效和花瓣飘落效果 混合开发H5页面的数字字体在IOS 安卓上显示不一样 node基于自签名证书搭建https服务 ios系统上h5页面点击播放audio标签声音有延迟问题处理 基于svelte webpack开发umd格式的npm包 CSS运动路径offset-path之path的语法解析
pcm文件转为mp3并播放
进军的蜗牛 · 2025-05-06 · via 博客园 - 进军的蜗牛

把pcm文件转为mp3并播放,代码如下:

<!DOCTYPE html>
<html>
<head>
    <title>PCM转MP3转换器</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lamejs/1.2.1/lame.min.js"></script>
    <!-- <script src="./lame.min.js"></script> -->
    <style>
        .audio-player {
            margin-top: 20px;
            display: none;
        }
    </style>
</head>
<body>
    <h1>PCM转MP3转换器</h1>
    <input type="file" id="pcmFile" accept=".pcm">
    <button onclick="convertPcmToMp3()">Convert to MP3</button>
    
    <div class="audio-player" id="audioPlayer">
        <h3>转换后的MP3:</h3>
        <audio id="audio" controls></audio>
    </div>

    <script>
        function convertPcmToMp3() {
            const fileInput = document.getElementById('pcmFile');
            const file = fileInput.files[0];
            
            if (!file) {
                alert('Please select a PCM file first');
                return;
            }

            const reader = new FileReader();
            reader.onload = function(e) {
                const pcmData = new Int16Array(e.target.result);
                
                // Create MP3 encoder
                // Mp3Encoder 参数说明:
                // 参数1: 声道数 (1=单声道, 2=立体声)
                // 参数2: 采样率 (单位: Hz, 常用值: 44100=CD音质, 48000=专业音频)
                // 参数3: 比特率 (单位: kbps, 常用值: 128=标准音质, 192=高质量, 320=最高质量)
                const mp3encoder = new lamejs.Mp3Encoder(1, 48000, 128);
                const mp3Data = [];

                // Convert PCM to MP3
                // sampleBlockSize 设置为 1152 的原因:
                // 1. MP3 编码的基本单位是帧(frame),每帧包含 1152 个采样点
                // 2. 1152 是 576 的倍数(1152 = 576 * 2)
                // 3. 576 是 MP3 编码中的一个重要数值,它代表一个子带(subband)的采样点数
                // 4. 这个数值是 MP3 标准规定的,不能随意更改
                const sampleBlockSize = 1152;
                const mp3buf = new Int8Array(1152);

                for (let i = 0; i < pcmData.length; i += sampleBlockSize) {
                    const sampleChunk = pcmData.subarray(i, i + sampleBlockSize);
                    const mp3buf = mp3encoder.encodeBuffer(sampleChunk);
                    if (mp3buf.length > 0) {
                        mp3Data.push(mp3buf);
                    }
                }

                // Add end of stream
                const end = mp3encoder.flush();
                if (end.length > 0) {
                    mp3Data.push(end);
                }

                // Create blob and play
                const blob = new Blob(mp3Data, { type: 'audio/mp3' });
                const url = URL.createObjectURL(blob);
                
                // Show audio player and set source
                const audioPlayer = document.getElementById('audioPlayer');
                const audio = document.getElementById('audio');
                audio.src = url;
                audioPlayer.style.display = 'block';
            };

            reader.readAsArrayBuffer(file);
        }
    </script>
</body>
</html>

=== 5月23日新增===
新增加了 分析PCM文件数据,分析是否有声音数据,已经生成声音波形图的功能,如下图所示:

源码地址: https://github.com/YalongYan/pcm2mp3
源码里有pcm文件 可以进行测试