fd
Browse files- fixed_app.py +25 -3
fixed_app.py
CHANGED
|
@@ -99,8 +99,30 @@ def decode_audio(audio_base64: str) -> str:
|
|
| 99 |
logger.error(f"Base64解码失败: {e}")
|
| 100 |
raise HTTPException(status_code=400, detail=f"Invalid base64 data: {str(e)}")
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
temp_file.write(audio_data)
|
| 105 |
temp_path = temp_file.name
|
| 106 |
|
|
@@ -111,7 +133,7 @@ def decode_audio(audio_base64: str) -> str:
|
|
| 111 |
if not os.path.exists(temp_path):
|
| 112 |
raise HTTPException(status_code=500, detail="Failed to create temporary audio file")
|
| 113 |
|
| 114 |
-
logger.info(f"音频文件已保存到: {temp_path}, 大小: {os.path.getsize(temp_path)} 字节")
|
| 115 |
return temp_path
|
| 116 |
except HTTPException:
|
| 117 |
raise
|
|
|
|
| 99 |
logger.error(f"Base64解码失败: {e}")
|
| 100 |
raise HTTPException(status_code=400, detail=f"Invalid base64 data: {str(e)}")
|
| 101 |
|
| 102 |
+
# 检测音频格式
|
| 103 |
+
file_extension = ".wav" # 默认
|
| 104 |
+
if len(audio_data) >= 12:
|
| 105 |
+
header = audio_data[:12]
|
| 106 |
+
if header[:4] == b'RIFF' and header[8:12] == b'WAVE':
|
| 107 |
+
file_extension = ".wav"
|
| 108 |
+
logger.info("检测到WAV格式")
|
| 109 |
+
elif b'ftyp' in header and b'M4A' in header:
|
| 110 |
+
file_extension = ".m4a"
|
| 111 |
+
logger.info("检测到M4A格式")
|
| 112 |
+
elif header[:3] == b'ID3' or header[:2] == b'\xff\xfb':
|
| 113 |
+
file_extension = ".mp3"
|
| 114 |
+
logger.info("检测到MP3格式")
|
| 115 |
+
elif header[:4] == b'OggS':
|
| 116 |
+
file_extension = ".ogg"
|
| 117 |
+
logger.info("检测到OGG格式")
|
| 118 |
+
elif header[:4] == b'fLaC':
|
| 119 |
+
file_extension = ".flac"
|
| 120 |
+
logger.info("检测到FLAC格式")
|
| 121 |
+
else:
|
| 122 |
+
logger.warning(f"未知音频格式,文件头: {header.hex()}")
|
| 123 |
+
|
| 124 |
+
# 创建临时文件,使用检测到的扩展名
|
| 125 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=file_extension, mode="wb") as temp_file:
|
| 126 |
temp_file.write(audio_data)
|
| 127 |
temp_path = temp_file.name
|
| 128 |
|
|
|
|
| 133 |
if not os.path.exists(temp_path):
|
| 134 |
raise HTTPException(status_code=500, detail="Failed to create temporary audio file")
|
| 135 |
|
| 136 |
+
logger.info(f"音频文件已保存到: {temp_path}, 大小: {os.path.getsize(temp_path)} 字节, 格式: {file_extension}")
|
| 137 |
return temp_path
|
| 138 |
except HTTPException:
|
| 139 |
raise
|