azils3/bash
0
1# Ultroid - UserBot2# Copyright (C) 2021-2025 TeamUltroid3#4# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >5# PLease read the GNU Affero General Public License in6# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.7 8 9import os10import time11from datetime import datetime as dt12 13from pyUltroid.fns.tools import set_attributes14 15from . import (16 LOGS,17 ULTConfig,18 bash,19 downloader,20 eod,21 eor,22 genss,23 get_help,24 get_string,25 humanbytes,26 mediainfo,27 stdr,28 time_formatter,29 ultroid_cmd30)31 32__doc__ = get_help("help_audiotools")33 34 35@ultroid_cmd(pattern="makevoice$")36async def vnc(e):37 if not e.reply_to:38 return await eod(e, get_string("audiotools_1"))39 r = await e.get_reply_message()40 if not mediainfo(r.media).startswith(("audio", "video")):41 return await eod(e, get_string("spcltool_1"))42 xxx = await e.eor(get_string("com_1"))43 file, _ = await e.client.fast_downloader(44 r.document,45 )46 await xxx.edit(get_string("audiotools_2"))47 await bash(48 f"ffmpeg -i '{file.name}' -map 0:a -codec:a libopus -b:a 100k -vbr on out.opus"49 )50 try:51 await e.client.send_message(52 e.chat_id, file="out.opus", force_document=False, reply_to=r53 )54 except Exception as er:55 LOGS.exception(er)56 return await xxx.edit("`Failed to convert in Voice...`")57 await xxx.delete()58 os.remove(file.name)59 os.remove("out.opus")60 61 62@ultroid_cmd(pattern="atrim( (.*)|$)")63async def trim_aud(e):64 sec = e.pattern_match.group(1).strip()65 if not sec or "-" not in sec:66 return await eod(e, get_string("audiotools_3"))67 a, b = sec.split("-")68 if int(a) >= int(b):69 return await eod(e, get_string("audiotools_4"))70 vido = await e.get_reply_message()71 if vido and vido.media and mediainfo(vido.media).startswith(("video", "audio")):72 if hasattr(vido.media, "document"):73 vfile = vido.media.document74 name = vido.file.name75 else:76 vfile = vido.media77 name = ""78 if not name:79 name = dt.now().isoformat("_", "seconds") + ".mp4"80 xxx = await e.eor(get_string("audiotools_5"))81 c_time = time.time()82 file = await downloader(83 f"resources/downloads/{name}",84 vfile,85 xxx,86 c_time,87 f"Downloading {name}...",88 )89 90 o_size = os.path.getsize(file.name)91 d_time = time.time()92 diff = time_formatter((d_time - c_time) * 1000)93 file_name = (file.name).split("/")[-1]94 out = file_name.replace(file_name.split(".")[-1], "_trimmed.aac")95 if int(b) > int(await genss(file.name)):96 os.remove(file.name)97 return await eod(xxx, get_string("audiotools_6"))98 ss, dd = stdr(int(a)), stdr(int(b))99 xxx = await xxx.edit(100 f"Downloaded `{file.name}` of `{humanbytes(o_size)}` in `{diff}`.\n\nNow Trimming Audio from `{ss}` to `{dd}`..."101 )102 cmd = f'ffmpeg -i "{file.name}" -preset ultrafast -ss {ss} -to {dd} -vn -acodec copy "{out}" -y'103 await bash(cmd)104 os.remove(file.name)105 f_time = time.time()106 n_file, _ = await e.client.fast_uploader(107 out, show_progress=True, event=e, message="Uploading...", to_delete=True108 )109 attributes = await set_attributes(out)110 111 caption = get_string("audiotools_7").format(ss, dd)112 await e.client.send_file(113 e.chat_id,114 n_file,115 thumb=ULTConfig.thumb,116 caption=caption,117 attributes=attributes,118 force_document=False,119 reply_to=e.reply_to_msg_id,120 )121 await xxx.delete()122 else:123 await e.eor(get_string("audiotools_1"), time=5)124 125 126@ultroid_cmd(pattern="extractaudio$")127async def ex_aud(e):128 reply = await e.get_reply_message()129 if not (reply and reply.media and mediainfo(reply.media).startswith("video")):130 return await e.eor(get_string("audiotools_8"))131 name = reply.file.name or "video.mp4"132 vfile = reply.media.document133 msg = await e.eor(get_string("com_1"))134 c_time = time.time()135 file = await downloader(136 f"resources/downloads/{name}",137 vfile,138 msg,139 c_time,140 f"Downloading {name}...",141 )142 143 out_file = f"{file.name}.aac"144 cmd = f"ffmpeg -i {file.name} -vn -acodec copy {out_file}"145 o, err = await bash(cmd)146 os.remove(file.name)147 attributes = await set_attributes(out_file)148 149 f_time = time.time()150 try:151 n_file, _ = await e.client.fast_uploader(152 out_file, show_progress=True, event=e, message="Uploading...", to_delete=True153 )154 155 except FileNotFoundError:156 return await eor(msg, get_string("audiotools_9"))157 await e.reply(158 get_string("audiotools_10"),159 file=n_file,160 thumb=ULTConfig.thumb,161 attributes=attributes,162 )163 await msg.delete()164 