Compare commits

...

3 Commits

Author SHA1 Message Date
blank X 2b399d4bd5
Add thumbnails 2021-10-23 11:34:55 +07:00
blank X 25d9251208
Fix albums 2021-10-23 11:24:44 +07:00
blank X f2ce155d90
Don't calculate mimetype if file is None 2021-10-20 21:12:39 +07:00
1 changed files with 29 additions and 2 deletions

View File

@ -207,7 +207,12 @@ async def main():
j = j[0]
k = k[0]
attributes = []
mimetype = await _get_file_mimetype(k)
try:
mimetype = (await _get_file_mimetype(k)) if k else ''
except TypeError:
# (for now) telethon doesn't easily support attributes for grouped media
mimetype = ''
thumb = None
if mimetype.startswith('video/'):
try:
data = await _get_video_data(k)
@ -223,7 +228,14 @@ async def main():
logging.exception('Exception when getting video data')
else:
attributes.append(DocumentAttributeVideo(duration, w, h, supports_streaming=mimetype == 'video/mp4' or None))
await client.send_message(chat, j, file=k, link_preview=False, attributes=attributes)
dn, _ = os.path.split(k)
try:
nthumb = os.path.join(dn, f'{time.time()}.jpg')
if await _make_thumbnail(nthumb, k):
thumb = nthumb
except Exception:
logging.exception('Exception while making thumbnail')
await client.send_message(chat, j, file=k, link_preview=False, attributes=attributes, thumb=thumb)
async def _get_video_data(filename):
proc = await asyncio.create_subprocess_exec('ffprobe', '-show_format', '-show_streams', '-print_format', 'json', filename, stdout=asyncio.subprocess.PIPE)
@ -238,6 +250,21 @@ async def main():
data['format']['duration'] = ndata['format']['duration']
return data
async def _make_thumbnail(filename, video):
data = await _get_video_data(video)
if not data.get('format'):
return False
if data['format'].get('duration') is None:
return False
for i in (0, 5, 10, 15):
if i and data['format']['duration'] > i:
continue
proc = await asyncio.create_subprocess_exec('ffmpeg', '-an', '-sn', '-ss', str(i), '-i', video, '-frames:v', '1', filename)
await proc.communicate()
if not proc.returncode:
return True
return False
async def _download_file(filename, url):
print(url)
async with session.get(url) as resp: