-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyg_vm.py
85 lines (64 loc) · 3.39 KB
/
yg_vm.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
__version__ = (1, 4, 8, 8)
# This file is a part of Hikka Userbot
# Code is NOT licensed under CC-BY-NC-ND 4.0 unless otherwise specified.
# 🌐 https://github.com/hikariatama/Hikka
# You CAN edit this file without direct permission from the author.
# You can redistribute this file with any modifications.
# meta developer: @yg_modules
# scope: hikka_only
# scope: hikka_min 1.6.3
# scope: ffmpeg
# █▄█ █░█ █▀▄▀█ █▀▄▀█ █▄█ █▀▄▀█ █▀█ █▀▄ █▀
# ░█░ █▄█ █░▀░█ █░▀░█ ░█░ █░▀░█ █▄█ █▄▀ ▄█
import os
import uuid
from .. import loader, utils
@loader.tds
class VoiceModule(loader.Module):
"""Converts music and video (required ffmpeg)"""
strings = {"name": "yg_vm"}
async def m2vcmd(self, message):
"""Convert music to voice message"""
reply = await message.get_reply_message()
if not reply or not reply.file:
await utils.answer(message, "<emoji document_id=5210952531676504517>❌</emoji> <b>Reply to audio file</b>")
return
media = reply.file
mime_type = media.mime_type.split('/')[0] if '/' in media.mime_type else 'audio'
if mime_type == 'audio':
await utils.answer(message, "<emoji document_id=4988080790286894217>🫥</emoji> <b>Converting audio...</b>")
voice_message = await self.convert_audio(reply)
else:
await utils.answer(message, "<emoji document_id=5210952531676504517>❌</emoji> <b>Unsupported file type</b>")
return
await message.delete()
await message.client.send_file(message.to_id, voice_message, voice_note=True, reply_to=reply)
async def convert_audio(self, message):
tmp_filename = "tmp_audio.ogg"
await message.download_media(file=tmp_filename)
os.system(f"ffmpeg -y -i {tmp_filename} -c:a libopus {tmp_filename}.ogg")
os.remove(tmp_filename)
return f"{tmp_filename}.ogg"
async def v2acmd(self, message):
"""Convert video to audio"""
reply = await message.get_reply_message()
if not reply or not reply.file:
await utils.answer(message, "<emoji document_id=5210952531676504517>❌</emoji> <b>Reply to video file</b>")
return
media = reply.file
mime_type = media.mime_type.split('/')[0] if '/' in media.mime_type else 'video'
if mime_type == 'video':
await utils.answer(message, "<emoji document_id=4988080790286894217>🫥</emoji> <b>Converting video to audio...</b>")
audio_message = await self.convert_video_to_mp3(reply)
else:
await utils.answer(message, "<emoji document_id=5210952531676504517>❌</emoji> <b>Unsupported file type</b>")
return
await message.delete()
await message.client.send_file(message.to_id, audio_message, reply_to=reply)
async def convert_video_to_mp3(self, message):
tmp_filename = f"{uuid.uuid4().hex}.mp4"
await message.download_media(file=tmp_filename)
audio_filename = f"{uuid.uuid4().hex}.mp3"
os.system(f"ffmpeg -y -i {tmp_filename} -vn -acodec libmp3lame -ab 192k -ar 44100 -ac 2 {audio_filename}")
os.remove(tmp_filename)
return audio_filename