mirror of
https://github.com/open-webui/open-webui
synced 2025-06-08 15:37:22 +00:00
refac: audio recording
This commit is contained in:
parent
2946f5fcce
commit
62366ad020
@ -125,12 +125,16 @@ def upload_file(
|
|||||||
)
|
)
|
||||||
if process:
|
if process:
|
||||||
try:
|
try:
|
||||||
if file.content_type in [
|
|
||||||
"audio/mpeg",
|
if file.content_type.startswith(
|
||||||
"audio/wav",
|
(
|
||||||
"audio/ogg",
|
"audio/mpeg",
|
||||||
"audio/x-m4a",
|
"audio/wav",
|
||||||
]:
|
"audio/ogg",
|
||||||
|
"audio/x-m4a",
|
||||||
|
"audio/webm",
|
||||||
|
)
|
||||||
|
):
|
||||||
file_path = Storage.get_file(file_path)
|
file_path = Storage.get_file(file_path)
|
||||||
result = transcribe(request, file_path)
|
result = transcribe(request, file_path)
|
||||||
|
|
||||||
|
@ -6,6 +6,10 @@
|
|||||||
|
|
||||||
import { transcribeAudio } from '$lib/apis/audio';
|
import { transcribeAudio } from '$lib/apis/audio';
|
||||||
|
|
||||||
|
import dayjs from 'dayjs';
|
||||||
|
import LocalizedFormat from 'dayjs/plugin/localizedFormat';
|
||||||
|
dayjs.extend(LocalizedFormat);
|
||||||
|
|
||||||
const i18n = getContext('i18n');
|
const i18n = getContext('i18n');
|
||||||
|
|
||||||
export let recording = false;
|
export let recording = false;
|
||||||
@ -134,11 +138,11 @@
|
|||||||
detectSound();
|
detectSound();
|
||||||
};
|
};
|
||||||
|
|
||||||
const onStopHandler = async (audioBlob) => {
|
const onStopHandler = async (audioBlob, ext: string = 'wav') => {
|
||||||
// Create a blob from the audio chunks
|
// Create a blob from the audio chunks
|
||||||
|
|
||||||
await tick();
|
await tick();
|
||||||
const file = blobToFile(audioBlob, 'recording.wav');
|
const file = blobToFile(audioBlob, `Recording-${dayjs().format('L LT')}.${ext}`);
|
||||||
|
|
||||||
if (transcribe) {
|
if (transcribe) {
|
||||||
if ($config.audio.stt.engine === 'web' || ($settings?.audio?.stt?.engine ?? '') === 'web') {
|
if ($config.audio.stt.engine === 'web' || ($settings?.audio?.stt?.engine ?? '') === 'web') {
|
||||||
@ -163,32 +167,23 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveRecording = (blob) => {
|
|
||||||
const url = URL.createObjectURL(blob);
|
|
||||||
const a = document.createElement('a');
|
|
||||||
document.body.appendChild(a);
|
|
||||||
a.style = 'display: none';
|
|
||||||
a.href = url;
|
|
||||||
a.download = 'recording.wav';
|
|
||||||
a.click();
|
|
||||||
window.URL.revokeObjectURL(url);
|
|
||||||
};
|
|
||||||
|
|
||||||
const startRecording = async () => {
|
const startRecording = async () => {
|
||||||
loading = true;
|
loading = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (displayMedia) {
|
if (displayMedia) {
|
||||||
stream = await navigator.mediaDevices.getDisplayMedia({
|
const mediaStream = await navigator.mediaDevices.getDisplayMedia({
|
||||||
video: {
|
audio: true
|
||||||
mediaSource: 'screen'
|
|
||||||
},
|
|
||||||
audio: {
|
|
||||||
echoCancellation: true,
|
|
||||||
noiseSuppression: true,
|
|
||||||
autoGainControl: true
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
stream = new MediaStream();
|
||||||
|
for (const track of mediaStream.getAudioTracks()) {
|
||||||
|
stream.addTrack(track);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const track of mediaStream.getVideoTracks()) {
|
||||||
|
track.stop();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
stream = await navigator.mediaDevices.getUserMedia({
|
stream = await navigator.mediaDevices.getUserMedia({
|
||||||
audio: {
|
audio: {
|
||||||
@ -206,7 +201,10 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mediaRecorder = new MediaRecorder(stream);
|
mediaRecorder = new MediaRecorder(stream, {
|
||||||
|
mimeType: 'audio/webm; codecs=opus'
|
||||||
|
});
|
||||||
|
|
||||||
mediaRecorder.onstart = () => {
|
mediaRecorder.onstart = () => {
|
||||||
console.log('Recording started');
|
console.log('Recording started');
|
||||||
loading = false;
|
loading = false;
|
||||||
@ -220,8 +218,19 @@
|
|||||||
console.log('Recording stopped');
|
console.log('Recording stopped');
|
||||||
|
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
// Use the actual type provided by MediaRecorder
|
||||||
await onStopHandler(audioBlob);
|
let type = audioChunks[0]?.type || mediaRecorder.mimeType || 'audio/webm';
|
||||||
|
|
||||||
|
// split `/` and `;` to get the extension
|
||||||
|
let ext = type.split('/')[1].split(';')[0] || 'webm';
|
||||||
|
|
||||||
|
// If not audio, default to audio/webm
|
||||||
|
if (!type.startsWith('audio/')) {
|
||||||
|
ext = 'webm';
|
||||||
|
}
|
||||||
|
|
||||||
|
const audioBlob = new Blob(audioChunks, { type: type });
|
||||||
|
await onStopHandler(audioBlob, ext);
|
||||||
|
|
||||||
confirmed = false;
|
confirmed = false;
|
||||||
loading = false;
|
loading = false;
|
||||||
|
@ -29,7 +29,8 @@
|
|||||||
(item?.name && item?.name.toLowerCase().endsWith('.mp3')) ||
|
(item?.name && item?.name.toLowerCase().endsWith('.mp3')) ||
|
||||||
(item?.name && item?.name.toLowerCase().endsWith('.wav')) ||
|
(item?.name && item?.name.toLowerCase().endsWith('.wav')) ||
|
||||||
(item?.name && item?.name.toLowerCase().endsWith('.ogg')) ||
|
(item?.name && item?.name.toLowerCase().endsWith('.ogg')) ||
|
||||||
(item?.name && item?.name.toLowerCase().endsWith('.m4a'));
|
(item?.name && item?.name.toLowerCase().endsWith('.m4a')) ||
|
||||||
|
(item?.name && item?.name.toLowerCase().endsWith('.webm'));
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
console.log(item);
|
console.log(item);
|
||||||
|
Loading…
Reference in New Issue
Block a user