Add thumbnails
This commit is contained in:
parent
25d9251208
commit
2b399d4bd5
25
redditbot.py
25
redditbot.py
|
@ -212,6 +212,7 @@ async def main():
|
|||
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)
|
||||
|
@ -227,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)
|
||||
|
@ -242,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:
|
||||
|
|
Loading…
Reference in New Issue