将记录器数据分块发送到API

问题描述 投票:0回答:1

我希望在网站上运行一个记录器,每隔几秒就会将一个发布请求发送到 api 调用以进行转录。

但是第一次可以用,但是之后就会报这个错误:

"错误代码: 400 - {'error': {'message': '音频文件无法解码或其格式不受支持。', 'type': 'invalid_request_error', 'param': None, 'code ':无}}"

我每次都尝试给它一个临时文件名,但这不起作用。我尝试过延长或缩短间隔。

let mediaRecorder;
let audioChunks = [];
let sendInterval;

document.getElementById('recordBtn').addEventListener('click', async () => {
    if (mediaRecorder && mediaRecorder.state === "recording") {
        mediaRecorder.stop();
        clearInterval(sendInterval); // Clear the interval when stopping
        document.getElementById('recordBtn').textContent = 'Start Recording';
    } else {
        try {
            const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
            mediaRecorder = new MediaRecorder(stream);
            mediaRecorder.ondataavailable = event => {
                if (event.data.size > 0) {
                    audioChunks.push(event.data);
                    console.log('Chunk received:', event.data.size);
                }
            };

            // Start recording with timeslice to ensure chunks are generated at regular intervals
            mediaRecorder.start(3000);
            document.getElementById('recordBtn').textContent = 'Stop Recording';
            var x = 1;

            const sendAudioChunks = async () => {
                if (audioChunks.length > 0) {
                    console.log('Sending chunks:', audioChunks.length);
                    const audioBlob = new Blob(audioChunks, { 'type': 'audio/wav' });
                    audioChunks = []; // Clear chunks after sending
                    const formData = new FormData();
                    formData.append('audio_file', audioBlob, 'audio.wav');

                    try {
                        // Inside your try block within sendAudioChunks
                        const response = await fetch('/transcribe', {
                            method: 'POST',
                            body: formData,
                        });
                        if (!response.ok) {
                            // Log or handle non-200 responses here
                            console.error('Server responded with non-200 status:', response.status);
                        }

                        const data = await response.json();
                        console.log('Server response:', data);
                        document.getElementById('transcriptionContainer').textContent = data.transcription || 'Transcription failed or was empty.';


                    } catch (error) {
                        console.error('Failed to send audio chunks:', error);
                    }
                } else {
                    console.log('No chunks to send');
                }
            };

            clearInterval(sendInterval);
            sendInterval = setInterval(sendAudioChunks, 3000);

            mediaRecorder.onstop = async () => {
                clearInterval(sendInterval); // Clear the interval when stopping
                await sendAudioChunks(); // Send any remaining chunks
            };
        } catch (error) {
            console.error('Error accessing media devices:', error);
        }
    }
});
javascript blob
1个回答
0
投票

媒体文件不是这样工作的。要处理文件,您需要开头和结尾。

如果音频中的轻微中断是可以接受的,您可以重新开始每个块的录制。开始录音。 N秒后停止。存储/发送。开始录音。重复。

否则您需要更改 api,以便它在转录之前加入块。 Api 获取前 N 秒,转录它,获取下一个块,将其附加到现有块,转录最后 N 秒,重复。

© www.soinside.com 2019 - 2024. All rights reserved.