oxdbarchive/oxdbarchive/midentify.py

97 lines
3.4 KiB
Python

# -*- coding: utf-8 -*-
# -*- Mode: Python; -*-
# vi:si:et:sw=2:sts=2:ts=2
import os
import sys
_audio_codec_map = {
'ffmp3': 'mp3',
}
_video_codec_map = {
'xvid': 'XviD',
'dx50': 'DivX',
'dvsd': 'DV',
'divx': 'DivX',
}
def oggzinfo(fname):
cmd = 'oggzinfo -b -l "%s"' % fname
f = os.popen(cmd.encode('utf-8'))
data = f.read().strip()
f.close()
raw_dict = {}
for row in data.split('\n'):
try:
t = row.split(':')
key = t[0]
value = ":".join(t[1:])
raw_dict[key.strip()] = value.strip()
except:
pass
oxdb_dict = {}
duration = raw_dict.get('Content-Duration',-1)
d = duration.split(':')
duration = 0
while d:
duration = float(d.pop(0)) + duration * 60
oxdb_dict['length'] = int(duration * 1000)
oxdb_dict['height'] = int(raw_dict.get('Video-Height',0))
oxdb_dict['width'] = int(raw_dict.get('Video-Width',0))
oxdb_dict['fps'] = float(raw_dict.get('Video-Framerate', '0').replace('fps',''))
if float(oxdb_dict['height']):
oxdb_dict['aspect'] = float(oxdb_dict['width']) / float(oxdb_dict['height'])
else:
oxdb_dict['aspect'] = -1.0
oxdb_dict['video_bitrate'] = int(float(raw_dict.get('Content-Bitrate-Average','0').replace('kbps','')) * 1024)
oxdb_dict['video_codec'] = 'Theora'
oxdb_dict['audio_bitrate'] = int(float(raw_dict.get('Content-Bitrate-Average','0').replace('kbps','')) * 1024)
oxdb_dict['audio_codec'] = 'Vorbis'
oxdb_dict['audio_rate'] = int(raw_dict.get('Audio-Samplerate', '0').replace('Hz', ''))
oxdb_dict['audio_channels'] = int(raw_dict.get('Audio-Channels',1))
return oxdb_dict
def identify(fname):
if fname.endswith('sub') or fname.endswith('srt') or fname.endswith('idx'):
return dict(
length=0, height=0, width=0, fps=0,
video_bitrate=0, audio_bitrate=0, audio_rate=0, audio_channels=0,
audio_codec='', video_codec='', aspect=-1
)
if fname.endswith('ogg') or fname.endswith('ogv'):
return oggzinfo(fname)
cmd = 'midentify "%s"' % fname
f = os.popen(cmd.encode('utf-8'))
data = f.read().strip()
f.close()
raw_dict = {}
for row in data.split('\n'):
try:
key, value = row.split('=')
raw_dict[key] = value.strip()
except:
pass
oxdb_dict = {}
oxdb_dict['length'] = int(float(raw_dict.get('ID_LENGTH',-1)) * 1000)
oxdb_dict['height'] = int(raw_dict.get('ID_VIDEO_HEIGHT',0))
oxdb_dict['width'] = int(raw_dict.get('ID_VIDEO_WIDTH',0))
oxdb_dict['fps'] = float(raw_dict.get('ID_VIDEO_FPS',0))
oxdb_dict['aspect'] = float(raw_dict.get('ID_VIDEO_ASPECT',0))
if not oxdb_dict['aspect'] and float(oxdb_dict['height']):
oxdb_dict['aspect'] = float(oxdb_dict['width']) / float(oxdb_dict['height'])
else:
oxdb_dict['aspect'] = -1.0
oxdb_dict['video_bitrate'] = int(raw_dict.get('ID_VIDEO_BITRATE',0))
oxdb_dict['video_codec'] = raw_dict.get('ID_VIDEO_FORMAT','unknown').lower()
oxdb_dict['audio_bitrate'] = int(raw_dict.get('ID_AUDIO_BITRATE',0))
oxdb_dict['audio_codec'] = raw_dict.get('ID_AUDIO_CODEC','unknown').lower()
oxdb_dict['audio_rate'] = int(raw_dict.get('ID_AUDIO_RATE',0))
oxdb_dict['audio_channels'] = int(raw_dict.get('ID_AUDIO_NCH',1))
oxdb_dict['audio_codec'] = _audio_codec_map.get(oxdb_dict['audio_codec'], oxdb_dict['audio_codec'])
oxdb_dict['video_codec'] = _video_codec_map.get(oxdb_dict['video_codec'], oxdb_dict['video_codec'])
if oxdb_dict['length'] < 0: oxdb_dict['length'] = 0
return oxdb_dict