Use ffprobe to try to get file extension
This commit is contained in:
parent
4865b970e3
commit
1caef9097b
16
redditbot.py
16
redditbot.py
|
@ -230,6 +230,22 @@ async def main():
|
||||||
proc = await asyncio.create_subprocess_exec('file', '--brief', '--extension', filename, stdout=asyncio.subprocess.PIPE)
|
proc = await asyncio.create_subprocess_exec('file', '--brief', '--extension', filename, stdout=asyncio.subprocess.PIPE)
|
||||||
stdout, _ = await proc.communicate()
|
stdout, _ = await proc.communicate()
|
||||||
ext = stdout.decode().strip().split('/', maxsplit=1)[0]
|
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 == '???':
|
if not ext or ext == '???':
|
||||||
mimetype = await _get_file_mimetype(filename)
|
mimetype = await _get_file_mimetype(filename)
|
||||||
ext = mimetypes.guess_extension(mimetype) or '.bin'
|
ext = mimetypes.guess_extension(mimetype) or '.bin'
|
||||||
|
|
Loading…
Reference in New Issue