fix
This commit is contained in:
parent
7686adbaa3
commit
868b274917
|
@ -1,6 +1,6 @@
|
||||||
FROM nvidia/cuda:12.2.0-runtime-ubuntu22.04
|
FROM nvidia/cuda:12.2.0-runtime-ubuntu22.04
|
||||||
|
|
||||||
RUN apt update && apt install -y python3.10 python3-pip curl pciutils lshw && \
|
RUN apt update && apt install -y python3.10 python3-pip curl pciutils lshw ffmpeg && \
|
||||||
rm -rf /var/lib/apt/lists/*
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
|
@ -3,16 +3,6 @@ import subprocess
|
||||||
import uuid
|
import uuid
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from aiogram import types, Dispatcher, F
|
from aiogram import types, Dispatcher, F
|
||||||
import ffmpeg
|
|
||||||
|
|
||||||
import logging
|
|
||||||
|
|
||||||
logging.basicConfig(
|
|
||||||
filename="/shared_storage/bot_log.txt",
|
|
||||||
level=logging.DEBUG,
|
|
||||||
format="%(asctime)s - %(levelname)s - %(message)s"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def handle_voice_and_video(message: types.Message, redis_service, storage_path: str):
|
async def handle_voice_and_video(message: types.Message, redis_service, storage_path: str):
|
||||||
file_id = None
|
file_id = None
|
||||||
|
@ -24,9 +14,6 @@ async def handle_voice_and_video(message: types.Message, redis_service, storage_
|
||||||
if not file_id:
|
if not file_id:
|
||||||
return
|
return
|
||||||
|
|
||||||
if not os.path.exists(storage_path):
|
|
||||||
logging.error(f"❌ storage_path ({storage_path}) не существует!")
|
|
||||||
|
|
||||||
file = await message.bot.get_file(file_id)
|
file = await message.bot.get_file(file_id)
|
||||||
file_path = file.file_path
|
file_path = file.file_path
|
||||||
|
|
||||||
|
@ -40,21 +27,11 @@ async def handle_voice_and_video(message: types.Message, redis_service, storage_
|
||||||
|
|
||||||
await message.bot.download_file(file_path, temp_destination)
|
await message.bot.download_file(file_path, temp_destination)
|
||||||
|
|
||||||
if not os.path.exists(temp_destination):
|
|
||||||
logging.error(f"❌ Файл {temp_destination} не был загружен!")
|
|
||||||
else:
|
|
||||||
logging.error(f"✅ Файл {temp_destination} был загружен!")
|
|
||||||
|
|
||||||
wav_filename = f"{file_uuid}.wav"
|
wav_filename = f"{file_uuid}.wav"
|
||||||
wav_destination = os.path.join(storage_path, wav_filename)
|
wav_destination = os.path.join(storage_path, wav_filename)
|
||||||
|
|
||||||
convert_to_wav(temp_destination, wav_destination)
|
convert_to_wav(temp_destination, wav_destination)
|
||||||
|
|
||||||
if not os.path.exists(wav_destination):
|
|
||||||
logging.error(f"❌ WAV-файл {wav_destination} не был создан!")
|
|
||||||
else:
|
|
||||||
logging.error(f"✅ WAV-файл успешно создан: {wav_destination}")
|
|
||||||
|
|
||||||
os.remove(temp_destination)
|
os.remove(temp_destination)
|
||||||
|
|
||||||
task_data = {
|
task_data = {
|
||||||
|
@ -78,47 +55,19 @@ async def handle_voice_and_video(message: types.Message, redis_service, storage_
|
||||||
else:
|
else:
|
||||||
await message.reply("Sorry, transcription result was not received within the timeout.")
|
await message.reply("Sorry, transcription result was not received within the timeout.")
|
||||||
|
|
||||||
def convert_to_wav1(input_file: str, output_file: str):
|
|
||||||
"""
|
|
||||||
Конвертирует любой аудио/видеофайл в .wav с частотой 16kHz, 1 канал (моно).
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
ffmpeg.input(input_file).output(
|
|
||||||
output_file,
|
|
||||||
format="wav",
|
|
||||||
acodec="pcm_s16le",
|
|
||||||
ac=1,
|
|
||||||
ar="16000"
|
|
||||||
).run(overwrite_output=True)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error converting {input_file} to WAV: {e}")
|
|
||||||
|
|
||||||
def convert_to_wav(input_file: str, output_file: str):
|
def convert_to_wav(input_file: str, output_file: str):
|
||||||
"""
|
"""
|
||||||
Конвертирует любой аудиофайл в WAV с частотой 16kHz, 1 канал (моно).
|
Конвертирует любой аудиофайл в WAV с частотой 16kHz, 1 канал (моно).
|
||||||
Логирует ошибки FFmpeg.
|
Логирует ошибки FFmpeg.
|
||||||
"""
|
"""
|
||||||
try:
|
command = [
|
||||||
logging.error(f"🔄 Конвертация: {input_file} -> {output_file}")
|
|
||||||
|
|
||||||
command = [
|
|
||||||
"ffmpeg", "-y", "-i", input_file,
|
"ffmpeg", "-y", "-i", input_file,
|
||||||
"-ar", "16000", "-ac", "1", "-c:a", "pcm_s16le",
|
"-ar", "16000", "-ac", "1", "-c:a", "pcm_s16le",
|
||||||
output_file
|
output_file
|
||||||
]
|
]
|
||||||
|
subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
||||||
|
|
||||||
if result.returncode != 0:
|
|
||||||
logging.error(f"❌ Ошибка FFmpeg:\n{result.stderr.decode()}")
|
|
||||||
else:
|
|
||||||
logging.error(f"✅ FFmpeg успешно создал файл {output_file}")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"❌ Исключение при конвертации {input_file}: {e}")
|
|
||||||
|
|
||||||
def register_audio_handlers(dp: Dispatcher, redis_service, storage_path: str):
|
def register_audio_handlers(dp: Dispatcher, redis_service, storage_path: str):
|
||||||
# Оборачиваем callback для передачи дополнительных аргументов
|
|
||||||
handler_callback = partial(handle_voice_and_video, redis_service=redis_service, storage_path=storage_path)
|
handler_callback = partial(handle_voice_and_video, redis_service=redis_service, storage_path=storage_path)
|
||||||
# Регистрируем хэндлер с фильтром по content_type
|
|
||||||
dp.message.register(handler_callback, F.content_type.in_({types.ContentType.VOICE, types.ContentType.VIDEO_NOTE}))
|
dp.message.register(handler_callback, F.content_type.in_({types.ContentType.VOICE, types.ContentType.VIDEO_NOTE}))
|
||||||
|
|
Loading…
Reference in New Issue