-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathv2a.py
144 lines (125 loc) · 4.66 KB
/
v2a.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
# █▀█ █ █ █ █▀█ █▀▄ █
# © Copyright 2022
# https://t.me/hikariatama
#
# 🔒 Licensed under the GNU AGPLv3
# 🌐 https://www.gnu.org/licenses/agpl-3.0.html
# meta pic: https://static.dan.tatar/v2a_icon.png
# meta developer: @hikarimods
# meta banner: https://mods.hikariatama.ru/badges/v2a.jpg
# scope: ffmpeg
# scope: hikka_only
# scope: hikka_min 1.3.0
import asyncio
import io
import logging
import os
import tempfile
import telethon.utils as tlutils
from telethon.tl.types import DocumentAttributeAudio, Message
from .. import loader, utils
logger = logging.getLogger(__name__)
@loader.tds
class Video2Audio(loader.Module):
"""Converts video \ round messages to audio \ voice messages"""
strings = {
"name": "Video2Audio",
"no_video": "🚫 <b>Reply to video required</b>",
"converting": "🧚♀️ <b>Converting...</b>",
"error": "🚫 <b>Error while converting</b>",
}
strings_ru = {
"no_video": "🚫 <b>Ответь на видео</b>",
"converting": "🧚♀️ <b>Конвертирую...</b>",
"_cls_doc": "Конвертирует видео в аудио",
"error": "🚫 <b>Ошибка при конвертировании</b>",
}
async def client_ready(self):
self.v2a = await self.import_lib(
"https://libs.hikariatama.ru/v2a.py",
suspend_on_error=True,
)
@loader.command(
ru_doc=(
"<ответ на видео> [-vm] [-b] - конвертировать видео в аудио\n-vm -"
" Отправить голосовое сообщение"
)
)
async def v2acmd(self, message: Message):
"""<reply> [-vm] [-b] - Convert video to audio
-vm - Use voice message instead"""
use_voicemessage = "-vm" in utils.get_args_raw(message)
reply = await message.get_reply_message()
if not reply or not reply.video:
await utils.answer(message, self.strings("no_video"))
return
message = await utils.answer(message, self.strings("converting"))
video = await self._client.download_media(reply, bytes)
out = f"audio.{'ogg' if use_voicemessage else 'mp3'}"
try:
audio = await self.v2a.convert(video, out)
except Exception:
await utils.answer(message, self.strings("error"))
return
audiofile = io.BytesIO(audio)
audiofile.name = out
await self._client.send_file(
message.peer_id,
audiofile,
voice_note=use_voicemessage,
reply_to=reply.id,
attributes=[
DocumentAttributeAudio(
duration=next(
(
attr.duration
for attr in reply.document.attributes
if hasattr(attr, "duration")
),
0,
),
voice=use_voicemessage,
**(
{"waveform": tlutils.encode_waveform(audio)}
if use_voicemessage
else {}
),
)
],
)
if message.out:
await message.delete()
@loader.command(ru_doc="<reply> - Создать банованный вейвформ")
async def waveform(self, message: Message):
"""<reply to voice> - Create buggy waveform"""
reply = await message.get_reply_message()
if not reply or not reply.media:
await utils.answer(message, self.strings("no_video"))
return
message = await utils.answer(message, self.strings("converting"))
document = io.BytesIO(await reply.download_media(bytes))
document.name = "audio.ogg"
await self._client.send_file(
message.peer_id,
document,
voice_note=True,
reply_to=reply.id,
attributes=[
DocumentAttributeAudio(
duration=2147483647,
voice=True,
waveform=tlutils.encode_waveform(
bytes(
(
*tuple(range(0, 30, 5)),
*reversed(tuple(range(0, 30, 5))),
)
)
* 20
),
)
],
)
if message.out:
await message.delete()