From 1caef9097bc25f0fa33416e17c75ffab32323ba3 Mon Sep 17 00:00:00 2001 From: blank X Date: Mon, 2 Nov 2020 13:12:11 +0700 Subject: [PATCH] Use ffprobe to try to get file extension --- redditbot.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/redditbot.py b/redditbot.py index 126ea88..b3484df 100644 --- a/redditbot.py +++ b/redditbot.py @@ -230,6 +230,22 @@ async def main(): proc = await asyncio.create_subprocess_exec('file', '--brief', '--extension', filename, stdout=asyncio.subprocess.PIPE) stdout, _ = await proc.communicate() ext = stdout.decode().strip().split('/', maxsplit=1)[0] + if not ext or ext == '???': + ffprobe_exists = any(True for i in os.environ.get('PATH', '').split(':') if os.path.exists(os.path.join(i, 'ffprobe'))) + if ffprobe_exists: + proc = await asyncio.create_subprocess_exec('ffprobe', '-print_format', 'json', '-show_format', filename, stdout=asyncio.subprocess.PIPE) + stdout, _ = await proc.communicate() + format = json.loads(stdout).get('format') + if format: + format_name = format.get('format_name') + if format_name: + proc = await asyncio.create_subprocess_exec('ffprobe', '-h', f'demuxer={format_name}', stdout=asyncio.subprocess.PIPE) + stdout, _ = await proc.communicate() + text = stdout.decode().split('\n', 2) + if len(text) > 1: + text = text[1].strip().rstrip('.') + if text.startswith('Common extensions: '): + ext = text[19:].split(',', 1)[0] if not ext or ext == '???': mimetype = await _get_file_mimetype(filename) ext = mimetypes.guess_extension(mimetype) or '.bin'