Spaces:
Sleeping
Sleeping
Create save.py
Browse filesrecord.htmlも使います
- record/save.py +31 -0
- record/templates/record.html +76 -0
record/save.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify,render_template
|
2 |
+
import base64
|
3 |
+
|
4 |
+
app = Flask(__name__)
|
5 |
+
|
6 |
+
@app.route('/')
|
7 |
+
def root():
|
8 |
+
return render_template("record.html")
|
9 |
+
|
10 |
+
@app.route('/upload_audio', methods=['POST'])
|
11 |
+
def upload_audio():
|
12 |
+
try:
|
13 |
+
data = request.get_json() # クライアントから送られてきたJSONデータ
|
14 |
+
audio_data = data.get('audio_data') # Base64エンコードされた音声データ
|
15 |
+
|
16 |
+
if not audio_data:
|
17 |
+
return jsonify({"error": "音声データが送信されていません"}), 400
|
18 |
+
|
19 |
+
# Base64デコード
|
20 |
+
audio_binary = base64.b64decode(audio_data)
|
21 |
+
|
22 |
+
# WAVファイルとして保存
|
23 |
+
with open('recorded_audio.wav', 'wb') as f:
|
24 |
+
f.write(audio_binary)
|
25 |
+
|
26 |
+
return jsonify({"message": "音声が正常に保存されました"}), 200
|
27 |
+
except Exception as e:
|
28 |
+
return jsonify({"error": str(e)}), 500
|
29 |
+
|
30 |
+
if __name__ == '__main__':
|
31 |
+
app.run(debug=True)
|
record/templates/record.html
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="ja">
|
3 |
+
<head>
|
4 |
+
<meta charset="UTF-8">
|
5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>音声録音</title>
|
7 |
+
</head>
|
8 |
+
<body>
|
9 |
+
<h1>音声録音</h1>
|
10 |
+
<button id="start-recording">録音開始</button>
|
11 |
+
<button id="stop-recording" disabled>録音停止</button>
|
12 |
+
<p>録音した音声を送信する準備ができました。</p>
|
13 |
+
<button id="send-to-server" disabled>音声を送信</button>
|
14 |
+
|
15 |
+
<script>
|
16 |
+
let mediaRecorder;
|
17 |
+
let audioChunks = [];
|
18 |
+
const startRecordingBtn = document.getElementById('start-recording');
|
19 |
+
const stopRecordingBtn = document.getElementById('stop-recording');
|
20 |
+
const sendToServerBtn = document.getElementById('send-to-server');
|
21 |
+
|
22 |
+
// 音声録音の開始
|
23 |
+
startRecordingBtn.addEventListener('click', async () => {
|
24 |
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
25 |
+
mediaRecorder = new MediaRecorder(stream);
|
26 |
+
|
27 |
+
mediaRecorder.ondataavailable = event => {
|
28 |
+
audioChunks.push(event.data);
|
29 |
+
};
|
30 |
+
|
31 |
+
mediaRecorder.onstop = () => {
|
32 |
+
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
|
33 |
+
const reader = new FileReader();
|
34 |
+
|
35 |
+
reader.onloadend = () => {
|
36 |
+
const base64String = reader.result.split(',')[1]; // Base64エンコードされた音声データを取得
|
37 |
+
sendToServerBtn.disabled = false;
|
38 |
+
|
39 |
+
sendToServerBtn.addEventListener('click', () => {
|
40 |
+
// 音声をバックエンドに送信
|
41 |
+
fetch('/upload_audio', {
|
42 |
+
method: 'POST',
|
43 |
+
headers: {
|
44 |
+
'Content-Type': 'application/json',
|
45 |
+
},
|
46 |
+
body: JSON.stringify({
|
47 |
+
audio_data: base64String,
|
48 |
+
}),
|
49 |
+
})
|
50 |
+
.then(response => response.json())
|
51 |
+
.then(data => {
|
52 |
+
alert('音声がバックエンドに送信されました。');
|
53 |
+
})
|
54 |
+
.catch(error => {
|
55 |
+
console.error('エラー:', error);
|
56 |
+
});
|
57 |
+
});
|
58 |
+
};
|
59 |
+
|
60 |
+
reader.readAsDataURL(audioBlob);
|
61 |
+
};
|
62 |
+
|
63 |
+
mediaRecorder.start();
|
64 |
+
startRecordingBtn.disabled = true;
|
65 |
+
stopRecordingBtn.disabled = false;
|
66 |
+
});
|
67 |
+
|
68 |
+
// 音声録音の停止
|
69 |
+
stopRecordingBtn.addEventListener('click', () => {
|
70 |
+
mediaRecorder.stop();
|
71 |
+
startRecordingBtn.disabled = false;
|
72 |
+
stopRecordingBtn.disabled = true;
|
73 |
+
});
|
74 |
+
</script>
|
75 |
+
</body>
|
76 |
+
</html>
|