Guest User

DraXus

a guest
Jan 5th, 2011
521
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2.  
  3. # this script can installed to the current user account by running the following commands:
  4.  
  5. # sudo apt-get install python-nautilus python-mutagen python-pyexiv2 python-kaa-metadata
  6. # mkdir ~/.nautilus/python-extensions
  7. # cp bsc-v2.py ~/.nautilus/python-extensions
  8. # chmod a+x ~/.nautilus/python-extensions/bsc-v2.py
  9.  
  10. # alternatively, you can be able to place the script in:
  11. # /usr/lib/nautilus/extensions-2.0/python/
  12.  
  13. # change log:
  14. # geb666: original bsc.py, based on work by Giacomo Bordiga
  15. # jmdsdf: version 2 adds extra ID3 and EXIF tag support
  16. # jmdsdf: added better error handling for ID3 tags, added mp3 length support, distinguished
  17. #         between exif image size and true image size
  18. # SabreWolfy: set consistent hh:mm:ss format, fixed bug with no ID3 information
  19. #             throwing an unhandled exception
  20. # jmdsdf: fixed closing file handles with mpinfo (thanks gueba)
  21. # jmdsdf: fixed closing file handles when there's an exception (thanks Pitxyoki)
  22. # jmdsdf: added video parsing (work based on enbeto, thanks!)
  23. # jmdsdf: added FLAC audio parsing through kaa.metadata (thanks for the idea l-x-l)
  24. # jmdsdf: added trackno, added mkv file support (thanks ENigma885)
  25. # jmdsdf: added date/album for flac/video (thanks eldon.t)
  26. # jmdsdf: added wav file support thru pyexiv2
  27. # jmdsdf: added sample rate file support thru mutagen and kaa (thanks for the idea N'ko)
  28. # jmdsdf: fix with tracknumber for FLAC, thanks l-x-l
  29. # draxus: support for pdf files
  30.  
  31. import os
  32. import urllib
  33. import nautilus
  34. # for id3 support
  35. from mutagen.easyid3 import EasyID3
  36. from mutagen.mp3 import MPEGInfo
  37. # for exif support
  38. import pyexiv2
  39. # for reading videos. for future improvement, this can also read mp3!
  40. import kaa.metadata
  41. # for reading image dimensions
  42. import Image
  43. # for reading pdf
  44. try:
  45.     from pyPdf import PdfFileReader
  46. except:
  47.     pass
  48.  
  49. class ColumnExtension(nautilus.ColumnProvider, nautilus.InfoProvider):
  50.     def __init__(self):
  51.         pass
  52.  
  53.     def get_columns(self):
  54.         return (
  55.             nautilus.Column("NautilusPython::title_column","title","Title","Song title"),
  56.             nautilus.Column("NautilusPython::album_column","album","Album","Album"),
  57.             nautilus.Column("NautilusPython::artist_column","artist","Artist","Artist"),
  58.             nautilus.Column("NautilusPython::tracknumber_column","tracknumber","Track","Track number"),
  59.             nautilus.Column("NautilusPython::genre_column","genre","Genre","Genre"),
  60.             nautilus.Column("NautilusPython::date_column","date","Date","Date"),
  61.             nautilus.Column("NautilusPython::bitrate_column","bitrate","Bitrate","Audio Bitrate in kilo bits per second"),
  62.             nautilus.Column("NautilusPython::samplerate_column","samplerate","Sample rate","Sample rate in Hz"),
  63.             nautilus.Column("NautilusPython::length_column","length","Length","Length of audio"),
  64.             nautilus.Column("NautilusPython::exif_datetime_original_column","exif_datetime_original","EXIF Dateshot ","Get the photo capture date from EXIF data"),
  65.             nautilus.Column("NautilusPython::exif_software_column","exif_software","EXIF Software","EXIF - software used to save image"),
  66.             nautilus.Column("NautilusPython::exif_flash_column","exif_flash","EXIF flash","EXIF - flash mode"),
  67.             nautilus.Column("NautilusPython::exif_pixeldimensions_column","exif_pixeldimensions","EXIF Image Size","Image size - pixel dimensions as reported by EXIF data"),
  68.             nautilus.Column("NautilusPython::pixeldimensions_column","pixeldimensions","Image Size","Image/video size - actual pixel dimensions"),
  69.         )
  70.  
  71.     def update_file_info(self, file):
  72.         # set defaults to blank
  73.         file.add_string_attribute('title', '')
  74.         file.add_string_attribute('album', '')
  75.         file.add_string_attribute('artist', '')
  76.         file.add_string_attribute('tracknumber', '')
  77.         file.add_string_attribute('genre', '')
  78.         file.add_string_attribute('date', '')
  79.         file.add_string_attribute('bitrate', '')
  80.         file.add_string_attribute('samplerate', '')
  81.         file.add_string_attribute('length', '')
  82.         file.add_string_attribute('exif_datetime_original', '')
  83.         file.add_string_attribute('exif_software', '')
  84.         file.add_string_attribute('exif_flash', '')
  85.         file.add_string_attribute('exif_pixeldimensions', '')
  86.         file.add_string_attribute('pixeldimensions', '')
  87.  
  88.         if file.get_uri_scheme() != 'file':
  89.             return
  90.  
  91.         # strip file:// to get absolute path
  92.         filename = urllib.unquote(file.get_uri()[7:])
  93.        
  94.         # mp3 handling
  95.         if file.is_mime_type('audio/mpeg'):
  96.             # attempt to read ID3 tag
  97.             try:
  98.                 audio = EasyID3(filename)
  99.                 # sometimes the audio variable will not have one of these items defined, that's why
  100.                 # there is this long try / except attempt
  101.                 try: file.add_string_attribute('title', audio["title"][0])
  102.                 except: file.add_string_attribute('title', "[n/a]")
  103.                 try: file.add_string_attribute('album', audio["album"][0])
  104.                 except: file.add_string_attribute('album', "[n/a]")
  105.                 try: file.add_string_attribute('artist', audio["artist"][0])
  106.                 except: file.add_string_attribute('artist', "[n/a]")
  107.                 try: file.add_string_attribute('tracknumber', audio["tracknumber"][0])
  108.                 except: file.add_string_attribute('tracknumber', "[n/a]")
  109.                 try: file.add_string_attribute('genre', audio["genre"][0])
  110.                 except: file.add_string_attribute('genre', "[n/a]")
  111.                 try: file.add_string_attribute('date', audio["date"][0])
  112.                 except: file.add_string_attribute('date', "[n/a]")
  113.             except:
  114.                 # [SabreWolfy] some files have no ID3 tag and will throw this exception:
  115.                 file.add_string_attribute('title', "[no ID3]")
  116.                 file.add_string_attribute('album', "[no ID3]")
  117.                 file.add_string_attribute('artist', "[no ID3]")
  118.                 file.add_string_attribute('tracknumber', "[no ID3]")
  119.                 file.add_string_attribute('genre', "[no ID3]")
  120.                 file.add_string_attribute('date', "[no ID3]")
  121.                
  122.             # try to read MP3 information (bitrate, length, samplerate)
  123.             try:
  124.                 mpfile = open (filename)
  125.                 mpinfo = MPEGInfo (mpfile)
  126.                 file.add_string_attribute('bitrate', str(mpinfo.bitrate/1000) + " Kbps")
  127.                 file.add_string_attribute('samplerate', str(mpinfo.sample_rate) + " Hz")
  128.                 # [SabreWolfy] added consistent formatting of times in format hh:mm:ss
  129.                 # [SabreWolfy[ to allow for correct column sorting by length
  130.                 mp3length = "%02i:%02i:%02i" % ((int(mpinfo.length/3600)), (int(mpinfo.length/60%60)), (int(mpinfo.length%60)))
  131.                 mpfile.close()
  132.                 file.add_string_attribute('length', mp3length)
  133.             except:
  134.                 file.add_string_attribute('bitrate', "[n/a]")
  135.                 file.add_string_attribute('length', "[n/a]")
  136.                 file.add_string_attribute('samplerate', "[n/a]")
  137.                 try:
  138.                     mpfile.close()
  139.                 except: pass
  140.    
  141.         # image handling
  142.         if file.is_mime_type('image/jpeg') or file.is_mime_type('image/png') or file.is_mime_type('image/gif') or file.is_mime_type('image/bmp'):
  143.             # EXIF handling routines
  144.             try:
  145.                 img = pyexiv2.Image(filename)
  146.                 img.readMetadata()
  147.                 file.add_string_attribute('exif_datetime_original',str(img['Exif.Photo.DateTimeOriginal']))
  148.                 file.add_string_attribute('exif_software',str(img['Exif.Image.Software']))
  149.                 file.add_string_attribute('exif_flash',str(img['Exif.Photo.Flash']))
  150.                 file.add_string_attribute('exif_pixeldimensions',str(img['Exif.Photo.PixelXDimension'])+'x'+str(img['Exif.Photo.PixelYDimension']))
  151.             except:
  152.                 # no exif data?
  153.                 file.add_string_attribute('exif_datetime_original',"")
  154.                 file.add_string_attribute('exif_software',"")
  155.                 file.add_string_attribute('exif_flash',"")
  156.                 file.add_string_attribute('exif_pixeldimensions',"")
  157.             # try read image info directly
  158.             try:
  159.                 im = Image.open(filename)
  160.                 file.add_string_attribute('pixeldimensions',str(im.size[0])+'x'+str(im.size[1]))
  161.             except:
  162.                 file.add_string_attribute('pixeldimensions',"[image read error]")
  163.  
  164.         # video/flac handling
  165.         if file.is_mime_type('video/x-msvideo') | file.is_mime_type('video/mpeg') | file.is_mime_type('video/x-ms-wmv') | file.is_mime_type('video/mp4') | file.is_mime_type('audio/x-flac') | file.is_mime_type('video/x-flv') | file.is_mime_type('video/x-matroska') | file.is_mime_type('audio/x-wav'):
  166.             try:
  167.                 info=kaa.metadata.parse(filename)
  168.                 try: file.add_string_attribute('length',"%02i:%02i:%02i" % ((int(info.length/3600)), (int(info.length/60%60)), (int(info.length%60))))
  169.                 except: file.add_string_attribute('length','[n/a]')
  170.                 try: file.add_string_attribute('pixeldimensions', str(info.video[0].width) + 'x'+ str(info.video[0].height))
  171.                 except: file.add_string_attribute('pixeldimensions','[n/a]')
  172.                 try: file.add_string_attribute('bitrate',str(round(info.audio[0].bitrate/1000)))
  173.                 except: file.add_string_attribute('bitrate','[n/a]')
  174.                 try: file.add_string_attribute('samplerate',str(int(info.audio[0].samplerate))+' Hz')
  175.                 except: file.add_string_attribute('samplerate','[n/a]')
  176.                 try: file.add_string_attribute('title', info.title)
  177.                 except: file.add_string_attribute('title', '[n/a]')
  178.                 try: file.add_string_attribute('artist', info.artist)
  179.                 except: file.add_string_attribute('artist', '[n/a]')
  180.                 try: file.add_string_attribute('genre', info.genre)
  181.                 except: file.add_string_attribute('genre', '[n/a]')
  182.                 try: file.add_string_attribute('tracknumber',info.trackno)
  183.                 except: file.add_string_attribute('tracknumber', '[n/a]')
  184.                 try: file.add_string_attribute('date',info.userdate)
  185.                 except: file.add_string_attribute('date', '[n/a]')                 
  186.                 try: file.add_string_attribute('album',info.album)
  187.                 except: file.add_string_attribute('album', '[n/a]')
  188.             except:
  189.                 file.add_string_attribute('length','error')
  190.                 file.add_string_attribute('pixeldimensions','error')
  191.                 file.add_string_attribute('bitrate','error')
  192.                 file.add_string_attribute('samplerate','error')
  193.                 file.add_string_attribute('title','error')
  194.                 file.add_string_attribute('artist','error')
  195.                 file.add_string_attribute('genre','error')
  196.                 file.add_string_attribute('track','error')
  197.                 file.add_string_attribute('date','error')
  198.                 file.add_string_attribute('album','error')
  199.         # pdf handling
  200.         if file.is_mime_type('application/pdf'):
  201.             try:
  202.                 f = open(filename, "rb")
  203.                 pdf = PdfFileReader(f)
  204.                 try: file.add_string_attribute('title', pdf.getDocumentInfo().title)
  205.                 except: file.add_string_attribute('title', "[n/a]")
  206.                 try: file.add_string_attribute('artist', pdf.getDocumentInfo().author)
  207.                 except: file.add_string_attribute('artist', "[n/a]")
  208.                 f.close()
  209.             except:
  210.                 file.add_string_attribute('title', "[no info]")
  211.                 file.add_string_attribute('artist', "[no info]")
  212.                    
  213.         self.get_columns()
RAW Paste Data