File size: 6,686 Bytes
78b07ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
import time

from pyrogram.types import Message

from Hellbot.functions.convert import convert_to_gif
from Hellbot.functions.tools import runcmd

from . import HelpMenu, hellbot, on_message, Config


@on_message("stog", allow_stan=True)
async def sticker_to_gif(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.sticker:
        return await hellbot.delete(
            message, "Reply to an animated/video sticker to convert it to gif."
        )

    hell = await hellbot.edit(message, "Converting ...")

    replied_sticker = message.reply_to_message.sticker

    if replied_sticker.is_animated:
        is_video = False
    elif replied_sticker.is_video:
        is_video = True
    else:
        return await hellbot.delete(hell, "Reply to an animated/video sticker.")

    dwl_path = await message.reply_to_message.download(Config.TEMP_DIR)
    gif_path = await convert_to_gif(dwl_path, is_video)

    await message.reply_animation(gif_path)
    await hellbot.delete(hell, "Converted to gif successfully!")

    os.remove(dwl_path)
    os.remove(gif_path)


@on_message("stoi", allow_stan=True)
async def sticker_to_image(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.sticker:
        return await hellbot.delete(
            message, "Reply to an sticker to convert it to image."
        )

    hell = await hellbot.edit(message, "Converting ...")
    fileName = f"image_{round(time.time())}.png"
    dwl_path = await message.reply_to_message.download(f"{Config.TEMP_DIR}{fileName}")

    await message.reply_photo(dwl_path)
    await hellbot.delete(hell, "Converted to image successfully!")

    os.remove(dwl_path)


@on_message("itos", allow_stan=True)
async def image_to_sticker(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.photo:
        return await hellbot.delete(
            message, "Reply to an image to convert it to sticker."
        )

    hell = await hellbot.edit(message, "Converting ...")
    fileName = f"sticker_{round(time.time())}.webp"
    dwl_path = await message.reply_to_message.download(f"{Config.TEMP_DIR}{fileName}")

    await message.reply_sticker(dwl_path)
    await hellbot.delete(hell, "Converted to sticker successfully!")

    os.remove(dwl_path)


@on_message("ftoi", allow_stan=True)
async def file_to_image(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.document:
        return await hellbot.delete(message, "Reply to a file to convert it to image.")

    if message.reply_to_message.document.mime_type.split("/")[0] != "image":
        return await hellbot.delete(message, "Reply to an image file.")

    hell = await hellbot.edit(message, "Converting ...")
    fileName = f"image_{round(time.time())}.png"
    dwl_path = await message.reply_to_message.download(f"{Config.TEMP_DIR}{fileName}")

    await message.reply_photo(dwl_path)
    await hellbot.delete(hell, "Converted to image successfully!")

    os.remove(dwl_path)


@on_message("itof", allow_stan=True)
async def image_to_file(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.photo:
        return await hellbot.delete(message, "Reply to an image to convert it to file.")

    hell = await hellbot.edit(message, "Converting ...")
    fileName = f"file_{round(time.time())}.png"
    dwl_path = await message.reply_to_message.download(f"{Config.TEMP_DIR}{fileName}")

    await message.reply_document(dwl_path)
    await hellbot.delete(hell, "Converted to file successfully!")

    os.remove(dwl_path)


@on_message("tovoice", allow_stan=True)
async def media_to_voice(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.media:
        return await hellbot.delete(message, "Reply to a media to convert it to voice.")

    hell = await hellbot.edit(message, "Converting ...")
    dwl_path = await message.reply_to_message.download(f"{Config.TEMP_DIR}")
    voice_path = f"{round(time.time())}.ogg"

    cmd_list = [
        "ffmpeg",
        "-i",
        dwl_path,
        "-map",
        "0:a",
        "-codec:a",
        "libopus",
        "-b:a",
        "100k",
        "-vbr",
        "on",
        voice_path,
    ]

    _, err, _, _ = await runcmd(" ".join(cmd_list))

    if os.path.exists(voice_path):
        await message.reply_voice(voice_path)
        await hellbot.delete(hell, "Converted to voice successfully!")
        os.remove(voice_path)
    else:
        await hellbot.error(hell, f"`{err}`")

    os.remove(dwl_path)


@on_message("tomp3", allow_stan=True)
async def media_to_mp3(_, message: Message):
    if not message.reply_to_message or not message.reply_to_message.media:
        return await hellbot.delete(message, "Reply to a media to convert it to mp3.")

    hell = await hellbot.edit(message, "Converting ...")
    dwl_path = await message.reply_to_message.download(f"{Config.TEMP_DIR}")
    mp3_path = f"{round(time.time())}.mp3"

    cmd_list = [
        "ffmpeg",
        "-i",
        dwl_path,
        "-vn",
        mp3_path,
    ]

    _, stderr, _, _ = await runcmd(" ".join(cmd_list))

    if os.path.exists(mp3_path):
        await message.reply_audio(mp3_path)
        await hellbot.delete(hell, "Converted to mp3 successfully!")
        os.remove(mp3_path)
    else:
        await hellbot.error(hell, f"`{stderr}`")

    os.remove(dwl_path)


HelpMenu("convert").add(
    "stog", #Bugged: to-be-fixed
    "<reply>",
    "Converts animated sticker to gif.",
    None,
    "Only animated sticker and video sticker can be converted to gif.",
).add(
    "stoi",
    "<reply>",
    "Converts sticker to image.",
    None,
    "Only static stickers can be converted to image.",
).add(
    "itos",
    "<reply>",
    "Converts image to sticker.",
    None,
    "Only images can be converted to sticker.",
).add(
    "ftoi",
    "<reply>",
    "Converts file to image.",
    None,
    "Only image files can be converted to image.",
).add(
    "itof",
    "<reply>",
    "Converts image to file.",
    None,
    "Only images can be converted to file.",
).add(
    "tovoice",
    "<reply>",
    "Converts media to voice.",
    None,
    "Only video/audio can be converted to voice.",
).add(
    "tomp3",
    "<reply>",
    "Converts media to mp3.",
    None,
    "Only video/audio can be converted to mp3.",
).info(
    "Converts media to other formats."
).done()