-
Notifications
You must be signed in to change notification settings - Fork 33
/
autocaption.py
79 lines (73 loc) · 2.75 KB
/
autocaption.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
from pagermaid import version
from pagermaid.listener import listener
from telethon.tl.custom.message import Message
from telethon.tl.types import MessageMediaDocument, DocumentAttributeFilename, DocumentAttributeImageSize, \
DocumentAttributeAudio, DocumentAttributeSticker, DocumentAttributeVideo, DocumentAttributeAnimated
def unit_convert(byte):
""" Converts byte into readable formats. """
power = 1024
zero = 0
units = {
0: 'B',
1: 'KB',
2: 'MB',
3: 'GB'}
while byte > power:
byte /= power
zero += 1
return f"{round(byte, 2)} {units[zero]}"
def duration_convert(duration: int):
""" Converts duration into readable formats. """
minutes = duration // 60
seconds = duration % 60
hours = minutes // 60
minutes %= 60
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
@listener(is_plugin=True, outgoing=True, incoming=False, ignore_edited=True)
async def auto_caption_file(context: Message):
if not context.media:
return
if not isinstance(context.media, MessageMediaDocument):
return
if context.text:
text = f"{context.text}\n`=============`\n"
else:
text = ""
for i in context.media.document.attributes:
# 文件名
if isinstance(i, DocumentAttributeFilename):
text += f"`文件名:{i.file_name}`\n"
# 图片尺寸
if isinstance(i, DocumentAttributeImageSize):
text += f"`图片尺寸:{i.w}x{i.h}`\n"
# 音乐时长、歌手、歌曲名
if isinstance(i, DocumentAttributeAudio):
if i.title:
text += f"`歌曲名:{i.title}`\n"
if i.performer:
text += f"`歌手:{i.performer}`\n"
if not i.voice:
text += f"`音乐时长:{duration_convert(i.duration)}`\n"
else:
text += f"`语音时长:{duration_convert(i.duration)}`\n"
# 视频时长、尺寸
if isinstance(i, DocumentAttributeVideo):
text += f"`视频尺寸:{i.w}x{i.h}`\n"
text += f"`视频时长:{duration_convert(i.duration)}`\n"
text += f"`应用内播放:{'是' if i.supports_streaming else '否'}`\n"
# 过滤○视频
if i.round_message:
return
# 过滤 sticker
if isinstance(i, DocumentAttributeSticker):
return
if isinstance(i, DocumentAttributeAnimated):
return
# 文件类型
text += f"`文件类型:{context.media.document.mime_type}`\n"
# 文件大小
text += f"`文件大小:{unit_convert(context.media.document.size)}`\n"
# DC
text += f"`DC:{context.media.document.dc_id}`\n"
# 编辑
await context.edit(text)