LLM(3) : 浏览器录制16K的音频并上传到后端

可被阿里云[qwen-audio-asr]大模型识别

HTML

复制代码
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>录音并上传</title>
	</head>
	<body>

		<button id="recordButton">开始/停止录音</button>
		<audio id="audioPlayback" controls style="display:none;"></audio>

		<script>
			let mediaRecorder;
			let audioChunks = [];
			let recording = false;

			document.getElementById('recordButton').addEventListener('click', () => {
				if (recording) {
					// 停止录音
					stopRecording();
				} else {
					// 开始录音
					startRecording();
				}
			});

			async function startRecording() {
				try {
					const stream = await navigator.mediaDevices.getUserMedia({
						audio: true
					});

					// 创建 MediaRecorder 实例,但不连接到任何音频输出
					mediaRecorder = new MediaRecorder(stream, {
						mimeType: 'audio/webm; codecs=opus'
					});

					mediaRecorder.ondataavailable = event => {
						audioChunks.push(event.data);
					};

					mediaRecorder.onstop = async () => {
						const audioBlob = new Blob(audioChunks, {
							type: 'audio/webm; codecs=opus'
						});
						const audioUrl = URL.createObjectURL(audioBlob);
						const audio = document.getElementById('audioPlayback');
						audio.src = audioUrl;
						audio.style.display = 'block';

						// 自动上传录音文件到后端接口
						await uploadAudioFile(audioBlob);

						// 清空 audioChunks 以便下次录音
						audioChunks = [];
					};

					mediaRecorder.start();
					recording = true;
					document.getElementById('recordButton').textContent = '停止录音';
				} catch (err) {
					console.error('Error accessing media devices.', err);
				}
			}

			function stopRecording() {
				if (mediaRecorder && mediaRecorder.state !== 'inactive') {
					mediaRecorder.stop();
					mediaRecorder.stream.getTracks().forEach(track => track.stop()); // 停止媒体流轨道
				}
				recording = false;
				document.getElementById('recordButton').textContent = '开始录音';
			}

			async function uploadAudioFile(blob) {
				const formData = new FormData();
				formData.append('file', blob, 'recording.webm');

				try {
					const response = await fetch('http://127.0.0.1:30025/sound', {
						method: 'POST',
						body: formData,
					});

					if (!response.ok) {
						throw new Error(`HTTP error! status: ${response.status}`);
					}

					const result = await response.json();
					console.log('Server response:', result);
				} catch (error) {
					console.error('There was a problem with the fetch operation:', error);
				}
			}
		</script>

	</body>
</html>

python接口

python 复制代码
@app.route('/sound', methods=['POST'])
def sound():
    file = request.files['file']
    # ... 处理文件
    return "SUCCESS"
相关推荐
沙振宇7 分钟前
【Python】使用YOLO8识别视频中的车与人物
python·yolo·音视频·状态模式·识别
开开心心就好1 小时前
支持添加网址的资源快速打开工具
人工智能·学习·游戏·音视频·hbase·语音识别·storm
AI搅拌机14 小时前
LTX2.3 IC-LORA动作迁移,通过depth、POSE、Canny精准控制生成的视频!
人工智能·音视频
蒋胜山15 小时前
PowerPoint插入音频报错
windows·经验分享·音视频
byte轻骑兵15 小时前
【LE Audio】CAP精讲[5]: 导演上线!Initiator音频协同全流程合规指南
人工智能·音视频·低功耗·le audio
知识领航员18 小时前
2026年精选4款音频处理软件:Adobe Audition领衔,蘑兔AI音乐紧随其后
adobe·音视频
云天AI实战派18 小时前
ChatGPT/API 调用故障排查指南:Realtime 音频、智能体浏览器操作与 AI 编码代理全流程修复手册
人工智能·chatgpt·音视频
celebrateyang19 小时前
一个功能比较完整的在线视频工具站:freesavevideo.online 使用体验
音视频·下载
EasyDSS20 小时前
私有化视频会议系统/视频高清直播点播EasyDSS构筑智慧校园安全可控全场景音视频中枢
安全·音视频
irpywp20 小时前
平台禁下载、格式不兼容、剪辑太麻烦,Media Downloader:mac 原生媒体下载工具,一站式解决视频下载、转码、裁剪、管理难题
macos·开源·github·音视频·媒体