簡體   English   中英

將音頻從瀏覽器保存到后端 (Django)

[英]Saving audio from the browser to the backend (Django)

我經歷了很多已經給出但不明白的答案。

任務:我必須從用戶那里獲取應該不到一分鍾的音頻,然后將其保存在后端並將其發送到 Google 的語音識別 API 以獲取文本。

我嘗試使用 MediaRecorder API 在瀏覽器中錄制,在這里https://mido22.github.io/MediaRecorder-sample/使用此演示。

我想將錄制的音頻保存在我的 Django 后端,以便我們可以對其進行一些后期處理。

EDIT1:媒體記錄器 api 的 Github 代碼

POST將所得BlobmakeLink功能服務器作為財產FormData對象

function makeLink() {
  let blob = new Blob(chunks, {type: media.type });
  let fd = new FormData;
  fd.append("audioRecording", blob);
  let request = new XMLHttpRequest();
  request.open("POST", "/path/to/server", true);
  request.onload = function() {
    // do stuff
  }
  request.onerror = function() {
   // handle error
  }
  request.send(fd);
}

function makeLink() {
  let blob = new Blob(chunks, {type: media.type });
  let fd = new FormData;
  fd.append("audioRecording", blob);
  fetch("/path/to/server", {method:"POST", body:fd})
  .then(response => response.ok)
  .then(res => console.log(res))
  .catch(err => console.error(err));
}

我在這里創建了一個簡單的項目: https : //github.com/Keramatfar/django_sound_file

對於后端,我使用以下函數:

def main(request):
        
    if request.method == "POST":
        audio_data = request.FILES.get('data')
        path = default_storage.save('file' + '.wav', ContentFile(audio_data.read()))
        return render(request, 'web-recorder.html')
    else:
        return render(request, 'web-recorder.html')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM