telegram send webp as sticker, need to convert it to jpg

This commit is contained in:
Benny 2023-04-17 20:07:56 +02:00
parent 3afb82d319
commit 783d1af3ba
No known key found for this signature in database
GPG key ID: 6CD0DBDA5235D481
3 changed files with 13 additions and 7 deletions

View file

@ -122,7 +122,7 @@ def convert_to_mp4(resp: dict, bot_msg):
edit_text(bot_msg, f"{current_time()}: Converting {path.name} to mp4. Please wait.")
new_file_path = path.with_suffix(".mp4")
logging.info("Detected %s, converting to mp4...", mime)
run_ffmpeg(["ffmpeg", "-y", "-i", path, new_file_path], bot_msg)
run_ffmpeg_progressbar(["ffmpeg", "-y", "-i", path, new_file_path], bot_msg)
index = resp["filepath"].index(path)
resp["filepath"][index] = new_file_path
@ -142,7 +142,7 @@ class ProgressBar(tqdm):
edit_text(self.bot_msg, t)
def run_ffmpeg(cmd_list: list, bm):
def run_ffmpeg_progressbar(cmd_list: list, bm):
cmd_list = cmd_list.copy()[1:]
ProgressBar.b = bm
ffpb.main(cmd_list, tqdm=ProgressBar)
@ -241,14 +241,14 @@ def convert_audio_format(resp: dict, bm):
break
ext = audio_stream["codec_name"]
new_path = path.with_suffix(f".{ext}")
run_ffmpeg(["ffmpeg", "-y", "-i", path, "-vn", "-acodec", "copy", new_path], bm)
run_ffmpeg_progressbar(["ffmpeg", "-y", "-i", path, "-vn", "-acodec", "copy", new_path], bm)
path.unlink()
index = resp["filepath"].index(path)
resp["filepath"][index] = new_path
else:
logging.info("Not default format, converting %s to %s", path, AUDIO_FORMAT)
new_path = path.with_suffix(f".{AUDIO_FORMAT}")
run_ffmpeg(["ffmpeg", "-y", "-i", path, new_path], bm)
run_ffmpeg_progressbar(["ffmpeg", "-y", "-i", path, new_path], bm)
path.unlink()
index = resp["filepath"].index(path)
resp["filepath"][index] = new_path
@ -267,6 +267,12 @@ def download_instagram(url: str, tempdir: str):
save_path = pathlib.Path(tempdir, f"{id(link)}.{ext}")
with open(save_path, "wb") as f:
f.write(requests.get(link, stream=True).content)
# telegram send webp as sticker, so we'll convert it to png
for path in pathlib.Path(tempdir).glob("*.webp"):
logging.info("Converting %s to png", path)
new_path = path.with_suffix(".jpg")
ffmpeg.input(path).output(new_path.as_posix()).run()
path.unlink()
return True

View file

@ -165,12 +165,12 @@ def direct_download_entrance(client: Client, bot_msg: typing.Union[types.Message
direct_normal_download(client, bot_msg, url)
def audio_entrance(bot_msg, client):
def audio_entrance(client, bot_msg):
if ENABLE_CELERY:
async_task(audio_task, bot_msg.chat.id, bot_msg.message_id)
# audio_task.delay(bot_msg.chat.id, bot_msg.message_id)
else:
normal_audio(bot_msg, client)
normal_audio(client, bot_msg)
def direct_normal_download(client: Client, bot_msg: typing.Union[types.Message, typing.Coroutine], url: str):

View file

@ -415,7 +415,7 @@ def audio_callback(client: Client, callback_query: types.CallbackQuery):
callback_query.answer(f"Converting to audio...please wait patiently")
redis.update_metrics("audio_request")
vmsg = callback_query.message
audio_entrance(vmsg, client)
audio_entrance(client, vmsg)
@app.on_callback_query(filters.regex(r"Local|Celery"))