forked from TheHamkerCat/Telegram_VC_Bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.py
121 lines (104 loc) · 3.87 KB
/
functions.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
import os
import aiohttp
import aiofiles
import ffmpeg
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def transcode(filename):
ffmpeg.input(filename).output("input.raw", format='s16le', acodec='pcm_s16le', ac=2, ar='48k').overwrite_output().run()
os.remove(filename)
#Download song
async def download_and_transcode_song(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
if resp.status == 200:
f = await aiofiles.open('song.mp3', mode='wb')
await f.write(await resp.read())
await f.close()
transcode("song.mp3")
# Convert seconds to mm:ss
def convert_seconds(seconds):
seconds = seconds % (24 * 3600)
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%02d:%02d" % (minutes, seconds)
# Convert hh:mm:ss to seconds
def time_to_seconds(time):
stringt = str(time)
return sum(int(x) * 60 ** i for i, x in enumerate(reversed(stringt.split(':'))))
# Change image size
def changeImageSize(maxWidth, maxHeight, image):
widthRatio = maxWidth / image.size[0]
heightRatio = maxHeight / image.size[1]
newWidth = int(widthRatio * image.size[0])
newHeight = int(heightRatio * image.size[1])
newImage = image.resize((newWidth, newHeight))
return newImage
# Generate cover for jiosaavn and deezer
async def generate_cover_square(requested_by, title, artist, duration, thumbnail):
async with aiohttp.ClientSession() as session:
async with session.get(thumbnail) as resp:
if resp.status == 200:
f = await aiofiles.open("background.png", mode="wb")
await f.write(await resp.read())
await f.close()
image1 = Image.open("./background.png")
image2 = Image.open("etc/foreground_square.png")
image3 = changeImageSize(600, 500, image1)
image4 = changeImageSize(600, 500, image2)
image5 = image3.convert("RGBA")
image6 = image4.convert("RGBA")
Image.alpha_composite(image5, image6).save("temp.png")
img = Image.open("temp.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("etc/font.otf", 20)
draw.text((150, 380), f"Title: {title}", (255, 255, 255), font=font)
draw.text((150, 405), f"Artist: {artist}", (255, 255, 255), font=font)
draw.text(
(150, 430),
f"Duration: {duration} Seconds",
(255, 255, 255),
font=font,
)
draw.text(
(150, 455),
f"Played By: {requested_by}",
(255, 255, 255),
font=font,
)
img.save("final.png")
os.remove("temp.png")
os.remove("background.png")
# Generate cover for youtube
async def generate_cover(requested_by, title, views, duration, thumbnail):
async with aiohttp.ClientSession() as session:
async with session.get(thumbnail) as resp:
if resp.status == 200:
f = await aiofiles.open("background.png", mode="wb")
await f.write(await resp.read())
await f.close()
image1 = Image.open("./background.png")
image2 = Image.open("etc/foreground.png")
image3 = changeImageSize(1280, 720, image1)
image4 = changeImageSize(1280, 720, image2)
image5 = image3.convert("RGBA")
image6 = image4.convert("RGBA")
Image.alpha_composite(image5, image6).save("temp.png")
img = Image.open("temp.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("etc/font.otf", 32)
draw.text((190, 550), f"Title: {title}", (255, 255, 255), font=font)
draw.text(
(190, 590), f"Duration: {duration}", (255, 255, 255), font=font
)
draw.text((190, 630), f"Views: {views}", (255, 255, 255), font=font)
draw.text((190, 670),
f"Played By: {requested_by}",
(255, 255, 255),
font=font,
)
img.save("final.png")
os.remove("temp.png")
os.remove("background.png")