File size: 1,395 Bytes
f692912
524d49f
 
 
 
 
 
834ef3b
 
524d49f
 
 
 
 
 
 
 
 
 
 
 
 
f692912
 
 
 
 
 
 
 
524d49f
 
f692912
524d49f
 
 
 
21317c6
d9e7de9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from flask import Flask, request, jsonify, render_template, send_from_directory
import base64
import os

app = Flask(__name__)

@app.route('/')
def index():
    return send_from_directory(".", "index.html")

@app.route('/upload_audio', methods=['POST'])
def upload_audio():
    try:
        data = request.get_json()  # クライアントから送られてきたJSONデータ
        audio_data = data.get('audio_data')  # Base64エンコードされた音声データ

        if not audio_data:
            return jsonify({"error": "音声データが送信されていません"}), 400
        
        # Base64デコード
        audio_binary = base64.b64decode(audio_data)
        
        # 永続化用ディレクトリ "data" がなければ作成
        persist_dir = "data"
        if not os.path.exists(persist_dir):
            os.makedirs(persist_dir)
        
        # WAVファイルとして "data" フォルダに保存
        filepath = os.path.join(persist_dir, "recorded_audio.wav")
        with open(filepath, 'wb') as f:
            f.write(audio_binary)
        
        return jsonify({"message": "音声が正常に保存されました", "filepath": filepath}), 200
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 7860))
    app.run(debug=True, host="0.0.0.0", port=port)