convert audio in celery task

disabled for now
This commit is contained in:
BennyThink 2022-01-08 22:05:52 +08:00
parent 82d9374ce6
commit 78e5002818
No known key found for this signature in database
GPG key ID: 6CD0DBDA5235D481
2 changed files with 39 additions and 21 deletions

View file

@ -21,7 +21,7 @@ from client_init import create_app
from config import BROKER, ENABLE_CELERY, OWNER, WORKERS
from constant import BotText
from db import Redis
from downloader import sizeof_fmt, upload_hook, ytdl_download
from downloader import convert_flac, sizeof_fmt, upload_hook, ytdl_download
from utils import (apply_log_formatter, customize_logger, get_metadata,
get_user_settings)
@ -46,6 +46,14 @@ def download_task(chat_id, message_id, url):
logging.info("celery tasks ended.")
@app.task()
def audio_task(chat_id, message_id):
logging.info("Audio celery tasks started for %s-%s", chat_id, message_id)
bot_msg = celery_client.get_messages(chat_id, message_id)
normal_audio(bot_msg)
logging.info("Audio celery tasks ended.")
def download_entrance(bot_msg, client, url):
if ENABLE_CELERY:
download_task.delay(bot_msg.chat.id, bot_msg.message_id, url)
@ -53,6 +61,34 @@ def download_entrance(bot_msg, client, url):
normal_download(bot_msg, client, url)
def audio_entrance(bot_msg):
if ENABLE_CELERY:
normal_audio(bot_msg)
# disable celery audio conversion for now
# audio_task.delay(bot_msg.chat.id, bot_msg.message_id)
else:
normal_audio(bot_msg)
def normal_audio(bot_msg):
chat_id = bot_msg.chat.id
mp4_name = bot_msg.video.file_name # 'youtube-dl_test_video_a.mp4'
flac_name = mp4_name.replace("mp4", "m4a")
with tempfile.NamedTemporaryFile() as tmp:
logging.info("downloading to %s", tmp.name)
celery_client.send_chat_action(chat_id, 'record_video_note')
celery_client.download_media(bot_msg, tmp.name)
logging.info("downloading complete %s", tmp.name)
# execute ffmpeg
celery_client.send_chat_action(chat_id, 'record_audio')
flac_tmp = convert_flac(flac_name, tmp)
celery_client.send_chat_action(chat_id, 'upload_audio')
celery_client.send_audio(chat_id, flac_tmp)
Redis().update_metrics("audio_success")
os.unlink(flac_tmp)
def get_worker_status(username):
worker_name = os.getenv("WORKER_NAME")
me = celery_client.get_me()

View file

@ -10,7 +10,6 @@ __author__ = "Benny <benny.think@gmail.com>"
import logging
import os
import re
import tempfile
import typing
from apscheduler.schedulers.background import BackgroundScheduler
@ -24,9 +23,8 @@ from config import (AUTHORIZED_USER, ENABLE_CELERY, ENABLE_VIP, OWNER,
REQUIRED_MEMBERSHIP)
from constant import BotText
from db import MySQL, Redis
from downloader import convert_flac
from limit import verify_payment
from tasks import download_entrance
from tasks import audio_entrance, download_entrance
from utils import (customize_logger, get_revision, get_user_settings,
set_user_settings)
@ -202,23 +200,7 @@ def audio_callback(client: "Client", callback_query: types.CallbackQuery):
Redis().update_metrics("audio_request")
msg = callback_query.message
chat_id = msg.chat.id
mp4_name = msg.video.file_name # 'youtube-dl_test_video_a.mp4'
flac_name = mp4_name.replace("mp4", "m4a")
with tempfile.NamedTemporaryFile() as tmp:
logging.info("downloading to %s", tmp.name)
client.send_chat_action(chat_id, 'record_video_note')
client.download_media(msg, tmp.name)
logging.info("downloading complete %s", tmp.name)
# execute ffmpeg
client.send_chat_action(chat_id, 'record_audio')
flac_tmp = convert_flac(flac_name, tmp)
client.send_chat_action(chat_id, 'upload_audio')
client.send_audio(chat_id, flac_tmp)
Redis().update_metrics("audio_success")
os.unlink(flac_tmp)
audio_entrance(msg)
if __name__ == '__main__':