Use ffprobe to try to get file extension

This commit is contained in:
blank X 2020-11-02 13:12:11 +07:00
parent 4865b970e3
commit 1caef9097b
1 changed files with 16 additions and 0 deletions

View File

@ -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'