# MediaInfo media columns # (c) 2015 Łukasz Jakubowski # # This is a script for Directory Opus. # See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information. # # # #-*-coding: mbcs-*- from MediaInfoDLL3 import * #MediaInfo version the script is written for SUPPORTED_MI_VER=0 SUPORTED_MI_REL=7 #Language to use for translations lang=DOpus.language #Global MediaInfo instance MI=None #Translations #You can just change Polski to what DOpus returns for your language #(DOpus.language) and edit Polish strings translations = { 'Polski' : { 'MIMovieSizeDescription' : { 'small':'mały', 'medium':'średni', 'big':'duży', }, 'OnInit' : { 'Dimensions':'Wymiary', 'Size description':'Format obrazu', 'Video info':'Wideo', 'Audio info':'Audio', 'Subtitle info':'Napisy', 'MediaInfo media columns':'Dane z MediaInfo', 'Columns fetching media info data via MediaInfo library':'Kolumny z informacjami o mediach pobierane za pomocą MediaInfo', }, 'OnMIColumnAdd' : { 'MediaInfo cannot open file: ','MediaInfo nie może otworzyć pliku: ', }, } } #Function fetching localised strings def nls(func, val): try: return translations[lang][func][val] except KeyError: return val #Function turning empty strings into '-' strings #meaning MediaInfo was queried and returned nothing/failed for a field def dashify(*args): return ['-' if type(x) is str and x=='' else x for x in args] #Get basic movie dimensions #Designed for only one video stream #Returns eg.: 1280 x 720 def MIMovieDimensions(): if not MI.Get(Stream.General,0,'VideoCount'): return '-' w = int(MI.Get(Stream.Video,0,'Width')) h = int(MI.Get(Stream.Video,0,'Height')) return str(w) + ' x ' + str(h) #Create movie format description #Designed for only one video stream #Returns eg.: FullHD (16:9) def MIMovieSizeDescription(): if not MI.Get(Stream.General,0,'VideoCount'): return '-' w = int(MI.Get(Stream.Video,0,"Width")) h = int(MI.Get(Stream.Video,0,"Height")) #You can easily add formats below if w == 1920 and h<=1080: res='FullHD' elif w == 1280 and h <=720: res='HD' elif w == 720 and h == 576: res='DVD PAL' elif w == 720 and h == 480: res='DVD NTSC' elif w == 352 and h == 240: res='VideoCD' elif w < 640 and h < 640: res=nls('MIMovieSizeDescription','small') elif w < 1280 and h < 1280: res=nls('MIMovieSizeDescription','medium') else: res=nls('MIMovieSizeDescription','big') #Add also format ratio in nice format return res + ' (' + dashify(MI.Get(Stream.Video,0,'DisplayAspectRatio/String'))[0] + ')' #Get details for video data - bitrate, format and codec #Designed for only one video stream #Returns eg.: 600 Kbps, VC-1 (WMV3) #Returns eg.: (9 117 Kbps), AVC (V_MPEG4/ISO/AVC) def MiMovieVideoInfo(): if not MI.Get(Stream.General,0,'VideoCount'): return '-' format = MI.Get(Stream.Video,0,'Format') codec = MI.Get(Stream.Video,0,'CodecID') br = MI.Get(Stream.Video,0,'BitRate/String') format, codec, br = dashify(format, codec, br) #If there is no information about stream bitrate, display whole file bitrate in () if br == '-': br = '(' + MI.Get(Stream.General,0,'OverallBitRate/String') + ')' return br + ', ' + format + ' (' + codec+ ')' #Get details for all audio streams - language, no. of channels, format, bitrate #Returns eg.: ja, 2, FLAC, - | en, 6, AC-3, 640 Kbps def MIMovieAudioInfo(): out = '' count = MI.Get(Stream.General,0,'AudioCount') if not count: return '-' for c in range(int(count)): lang = MI.Get(Stream.Audio,c,'Language') channel = MI.Get(Stream.Audio,c,'Channel(s)') format = MI.Get(Stream.Audio,c,'Format') br = MI.Get(Stream.Audio,c,'BitRate/String') out += (' | ' if out else '') + ', '.join(dashify(lang, channel, format, br)) return out #Return language for all subtitles in the file #Returns eg.: es, en, de def MiMovieSubInfo(): count = MI.Get(Stream.General,0,'TextCount') if not count: return '-' return dashify( ', '.join( [ dashify(MI.Get(Stream.Text,c,'Language'))[0] for c in range(int(count)) ] ))[0] #Init script and define columns def OnInit(init_data): init_data.name = nls('OnInit', 'MediaInfo media columns') init_data.desc = nls('OnInit', 'Columns fetching media info data via MediaInfo library') init_data.copyright = '(c) 2014 Łukasz Jakubowski' init_data.default_enable = True init_data.version = "1.0" #helper - workaround for DO not flagging all methods properly as methods init_data._FlagAsMethod('AddColumn') col = init_data.AddColumn() col.name = 'MIMovieDimensions' col.method = 'OnMIColumnAdd' col.label = nls('OnInit', 'Dimensions') col.justify = 'right' col.multicol = True col = init_data.AddColumn() col.name = 'MIMovieSizeDescription' col.method = 'OnMIColumnAdd' col.label = nls('OnInit', 'Size description') col.justify = 'left' col.multicol = True col = init_data.AddColumn() col.name = 'MiMovieVideoInfo' col.method = 'OnMIColumnAdd' col.label = nls('OnInit', 'Video info') col.justify = 'left' col.multicol = True col = init_data.AddColumn() col.name = 'MIMovieAudioInfo' col.method = 'OnMIColumnAdd' col.label = nls('OnInit', 'Audio info') col.justify = 'left' col.multicol = True col = init_data.AddColumn() col.name = 'MiMovieSubInfo' col.method = 'OnMIColumnAdd' col.label = nls('OnInit', 'Subtitle info') col.justify = 'center' col.multicol = True #Welcome message DOpus.Output('Column script "MediaInfo media columns" is starting...') #MediaInfo version check vers_str = MediaInfo().Option('Info_Version') vers, rel, dummy= vers_str[16:].split('.') if int(vers) != SUPPORTED_MI_VER or int(rel) != SUPORTED_MI_REL: DOpus.Output(nls('OnInit', 'MediaInfo media columns: dll version is other than 0.7, script may not function correctly!')) def OnMIColumnAdd(script_col_data): global MI #Process only files and only in Movies file type group if 'Movies' not in script_col_data.item.groups or script_col_data.item.is_dir: return #Open MI library MI = MediaInfo() #Report error if MediaInfo cannot open file if not MI.Open(str(script_col_data.item.realpath)): DOpus.Output(nls('OnMIColumnAdd','MediaInfo cannot open file: ') + str(script_col_data.item.realpath)) return #Fill all the columns we know about with data try: for col in script_col_data.columns: #Columns' names (col.name) are the same as function names, so we can do this: if col in globals(): #Indexing columns with () is normal in COM script_col_data.columns(col).value = eval(col+'()') finally: MI.Close()