forked from ErichDanikenOfficial/TG-Rename-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_to_audio.py
More file actions
143 lines (134 loc) · 5.36 KB
/
Copy pathconvert_to_audio.py
File metadata and controls
143 lines (134 loc) · 5.36 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# (c) Shrimadhav U K
# the logging things
import logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
import os
import time
# the secret configuration specific things
if bool(os.environ.get("WEBHOOK", False)):
from sample_config import Config
else:
from config import Config
# the Strings used for this "thing"
from translation import Translation
import pyrogram
logging.getLogger("pyrogram").setLevel(logging.WARNING)
from helper_funcs.chat_base import TRChatBase
from helper_funcs.display_progress import progress_for_pyrogram
from hachoir.metadata import extractMetadata
from hachoir.parser import createParser
# https://stackoverflow.com/a/37631799/4723940
from PIL import Image
@pyrogram.Client.on_message(pyrogram.Filters.command(["converttoaudio"]))
async def convert_to_audio(bot, update):
if update.from_user.id not in Config.AUTH_USERS:
await bot.delete_messages(
chat_id=update.chat.id,
message_ids=update.message_id,
revoke=True
)
return
TRChatBase(update.from_user.id, update.text, "converttoaudio")
if (update.reply_to_message is not None) and (update.reply_to_message.media is not None) :
description = Translation.CUSTOM_CAPTION_UL_FILE
download_location = Config.DOWNLOAD_LOCATION + "/"
a = await bot.send_message(
chat_id=update.chat.id,
text=Translation.DOWNLOAD_START,
reply_to_message_id=update.message_id
)
c_time = time.time()
the_real_download_location = await bot.download_media(
message=update.reply_to_message,
file_name=download_location,
progress=progress_for_pyrogram,
progress_args=(
Translation.DOWNLOAD_START,
a,
c_time
)
)
if the_real_download_location is not None:
await bot.edit_message_text(
text=Translation.SAVED_RECVD_DOC_FILE,
chat_id=update.chat.id,
message_id=a.message_id
)
# don't care about the extension
# convert video to audio format
audio_file_location_path = the_real_download_location
await bot.edit_message_text(
text=Translation.UPLOAD_START,
chat_id=update.chat.id,
message_id=a.message_id
)
logger.info(the_real_download_location)
# get the correct width, height, and duration for videos greater than 10MB
# ref: message from @BotSupport
width = 0
height = 0
duration = 0
metadata = extractMetadata(createParser(the_real_download_location))
if metadata.has("duration"):
duration = metadata.get('duration').seconds
thumb_image_path = Config.DOWNLOAD_LOCATION + "/" + str(update.from_user.id) + ".jpg"
if not os.path.exists(thumb_image_path):
thumb_image_path = None
else:
metadata = extractMetadata(createParser(thumb_image_path))
if metadata.has("width"):
width = metadata.get("width")
if metadata.has("height"):
height = metadata.get("height")
# get the correct width, height, and duration for videos greater than 10MB
# resize image
# ref: https://t.me/PyrogramChat/44663
# https://stackoverflow.com/a/21669827/4723940
Image.open(thumb_image_path).convert("RGB").save(thumb_image_path)
img = Image.open(thumb_image_path)
# https://stackoverflow.com/a/37631799/4723940
# img.thumbnail((90, 90))
img.resize((90, height))
img.save(thumb_image_path, "JPEG")
# https://pillow.readthedocs.io/en/3.1.x/reference/Image.html#create-thumbnails
# try to upload file
c_time = time.time()
await bot.send_audio(
chat_id=update.chat.id,
audio=audio_file_location_path,
caption=description,
duration=duration,
# performer="",
# title="",
# reply_markup=reply_markup,
thumb=thumb_image_path,
reply_to_message_id=update.reply_to_message.message_id,
progress=progress_for_pyrogram,
progress_args=(
Translation.UPLOAD_START,
a,
c_time
)
)
try:
os.remove(thumb_image_path)
os.remove(the_real_download_location)
os.remove(audio_file_location_path)
except:
pass
await bot.edit_message_text(
text=Translation.AFTER_SUCCESSFUL_UPLOAD_MSG,
chat_id=update.chat.id,
message_id=a.message_id,
disable_web_page_preview=True
)
else:
await bot.send_message(
chat_id=update.chat.id,
text=Translation.REPLY_TO_DOC_FOR_C2V,
reply_to_message_id=update.message_id
)