выводит ошибку ValueError: Audio file could not be read as PCM WAV
from aiogram import Bot, Dispatcher, executor, types
from aiogram.types import Message, ContentType
import os
import requests
import speech_recognition as sr
# Initialize bot and dispatcher
bot = Bot(token="TOKEN")
dp = Dispatcher(bot)
# Define a handler for the /start command
@dp.message_handler(commands='start')
async def cmd_start(message: Message):
# Create a reply keyboard
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, selective=True)
markup.add("Voice message", "Other command")
# Send message with keyboard
await message.answer("Hi there! Choose a command:", reply_markup=markup)
# Define a handler for the "Voice message" button
@dp.message_handler(lambda message: message.text == "Voice message")
async def cmd_voice(message: Message):
await message.answer("Please send your voice message")
# Add a handler for voice messages
@dp.message_handler(content_types=ContentType.VOICE)
async def process_voice(message: Message):
user_id = message.from_user.id
file_id = message.voice.file_id
file_url = bot.get_file_url(file_id)
response = requests.get(file_url)
# Save the voice message to PCM WAV file
with open(f"{user_id}/voice_{file_id}.wav", "wb") as f:
f.write(response.content)
# Load the PCM WAV file
r = sr.Recognizer()
with sr.AudioFile(f"{user_id}/voice_{file_id}.wav") as source:
audio = r.record(source)
try:
# Recognize the speech
text = r.recognize_google(audio, language="ru-RU")
await message.answer(f"Recognized text: {text}")
except sr.UnknownValueError:
await message.answer("Sorry, I couldn't understand the speech.")
except sr.RequestError as e:
await message.answer(f"Sorry, there was an error: {e}")
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
выводит такую ошибку ValueError: Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format