~team1/+junk/devel

« back to all changes in this revision

Viewing changes to bsc-v2.py

  • Committer: grnsrv@rocketmail.com
  • Date: 2012-06-07 18:11:24 UTC
  • Revision ID: grnsrv@rocketmail.com-20120607181124-91s44i0a322kpk3p
Merged from other updates and added MP4 Audio support including Apple Lossless ALAC,M4A.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
# this script can installed to the current user account by running the following commands:
4
4
 
5
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
 
6
# mkdir ~/.local/share/nautilus-python/extensions/
 
7
# cp bsc-v2.py ~/.local/share/nautilus-python/extensions/
 
8
# chmod a+x ~/.local/share/nautilus-python/extensions/bsc-v2.py
9
9
 
10
10
# alternatively, you can be able to place the script in:
11
 
# /usr/lib/nautilus/extensions-2.0/python/
 
11
# /usr/share/nautilus-python/extensions/
12
12
 
13
13
# change log:
14
14
# geb666: original bsc.py, based on work by Giacomo Bordiga
27
27
# jmdsdf: added sample rate file support thru mutagen and kaa (thanks for the idea N'ko)
28
28
# jmdsdf: fix with tracknumber for FLAC, thanks l-x-l
29
29
# draxus: support for pdf files
 
30
# arun (engineerarun@gmail.com): made changes to work with naulitus 3.x
 
31
# Andrew@webupd8.org: get EXIF support to work with Nautilus 3
 
32
# cehlgn added MP4 Audio support (mp4,m4a,Apple lossles)
 
33
# added close file for exif
30
34
 
31
35
import os
32
36
import urllib
33
 
import nautilus
34
 
 
 
37
#import nautilus
 
38
from gi.repository import Nautilus, GObject, Gtk, GdkPixbuf
35
39
# for id3 support
36
40
from mutagen.easyid3 import EasyID3
37
41
from mutagen.mp3 import MPEGInfo
46
50
        from pyPdf import PdfFileReader
47
51
except:
48
52
        pass
49
 
 
50
 
class ColumnExtension(nautilus.ColumnProvider, nautilus.InfoProvider):
 
53
# for mp4 AUDIO handling (Including ALAC *.m4a Apple Lossless)
 
54
try:
 
55
        from mutagen.mp4 import MP4
 
56
        import mutagen.easymp4
 
57
        from mutagen.easymp4 import EasyMP4
 
58
except:
 
59
        pass
 
60
 
 
61
 
 
62
class ColumnExtension(GObject.GObject, Nautilus.ColumnProvider, Nautilus.InfoProvider):
51
63
        def __init__(self):
52
64
                pass
53
65
 
54
66
        def get_columns(self):
55
67
                return (
56
 
                        nautilus.Column("NautilusPython::title_column","title","Title","Song title"),
57
 
                        nautilus.Column("NautilusPython::album_column","album","Album","Album"),
58
 
                        nautilus.Column("NautilusPython::artist_column","artist","Artist","Artist"),
59
 
                        nautilus.Column("NautilusPython::tracknumber_column","tracknumber","Track","Track number"),
60
 
                        nautilus.Column("NautilusPython::genre_column","genre","Genre","Genre"),
61
 
                        nautilus.Column("NautilusPython::date_column","date","Date","Date"),
62
 
                        nautilus.Column("NautilusPython::bitrate_column","bitrate","Bitrate","Audio Bitrate in kilo bits per second"),
63
 
                        nautilus.Column("NautilusPython::samplerate_column","samplerate","Sample rate","Sample rate in Hz"),
64
 
                        nautilus.Column("NautilusPython::length_column","length","Length","Length of audio"),
65
 
                        nautilus.Column("NautilusPython::exif_datetime_original_column","exif_datetime_original","EXIF Dateshot ","Get the photo capture date from EXIF data"),
66
 
                        nautilus.Column("NautilusPython::exif_software_column","exif_software","EXIF Software","EXIF - software used to save image"),
67
 
                        nautilus.Column("NautilusPython::exif_flash_column","exif_flash","EXIF flash","EXIF - flash mode"),
68
 
                        nautilus.Column("NautilusPython::exif_pixeldimensions_column","exif_pixeldimensions","EXIF Image Size","Image size - pixel dimensions as reported by EXIF data"),
69
 
                        nautilus.Column("NautilusPython::pixeldimensions_column","pixeldimensions","Image Size","Image/video size - actual pixel dimensions"),
70
 
                        nautilus.Column("NautilusPython::format_column","format","Format","Mpeg Audio Format"),
71
 
                        nautilus.Column("NautilusPython::mp3version_column","version","Version","Version"),
72
 
                        nautilus.Column("NautilusPython::composer_column","composer","Composer","Composer"),
73
 
                        nautilus.Column("NautilusPython::liricist_column","liricist","Liricist","Liricist"),
74
 
                )
 
68
                        Nautilus.Column(name="NautilusPython::title_column",attribute="title",label="Title",description="Song title"),
 
69
                        Nautilus.Column(name="NautilusPython::album_column",attribute="album",label="Album",description="Album"),
 
70
                        Nautilus.Column(name="NautilusPython::artist_column",attribute="artist",label="Artist",description="Artist"),
 
71
                        Nautilus.Column(name="NautilusPython::tracknumber_column",attribute="tracknumber",label="Track",description="Track number"),
 
72
                        Nautilus.Column(name="NautilusPython::genre_column",attribute="genre",label="Genre",description="Genre"),
 
73
                        Nautilus.Column(name="NautilusPython::date_column",attribute="date",label="Date",description="Date"),
 
74
                        Nautilus.Column(name="NautilusPython::bitrate_column",attribute="bitrate",label="Bitrate",description="Audio Bitrate in kilo bits per second"),
 
75
                        Nautilus.Column(name="NautilusPython::samplerate_column",attribute="samplerate",label="Sample rate",description="Sample rate in Hz"),
 
76
                        Nautilus.Column(name="NautilusPython::length_column",attribute="length",label="Length",description="Length of audio"),
 
77
                        Nautilus.Column(name="NautilusPython::exif_datetime_original_column",attribute="exif_datetime_original",label="EXIF Dateshot ",description="Get the photo capture date from EXIF data"),
 
78
                        Nautilus.Column(name="NautilusPython::exif_software_column",attribute="exif_software",label="EXIF Software",description="EXIF - software used to save image"),
 
79
                        Nautilus.Column(name="NautilusPython::exif_flash_column",attribute="exif_flash",label="EXIF flash",description="EXIF - flash mode"),
 
80
                        Nautilus.Column(name="NautilusPython::exif_pixeldimensions_column",attribute="exif_pixeldimensions",label="EXIF Image Size",description="Image size - pixel dimensions as reported by EXIF data"),
 
81
                        Nautilus.Column(name="NautilusPython::pixeldimensions_column",attribute="pixeldimensions",label="Image Size",description="Image/video size - actual pixel dimensions"),
 
82
                        Nautilus.Column(name="NautilusPython::format_column",attribute="format",label="Format",description="Mpeg Audio Format"),
 
83
                        Nautilus.Column(name="NautilusPython::mp3version_column",attribute="version",label="Version",description="Version"),
 
84
                        Nautilus.Column(name="NautilusPython::composer_column",attribute="composer",label="Composer",description="Composer"),
 
85
                        Nautilus.Column(name="NautilusPython::liricist_column",attribute="liricist",label="Liricist",description="Liricist"),
 
86
)
75
87
 
76
88
        def update_file_info(self, file):
77
89
                # set defaults to blank
78
90
                for x in EasyID3.valid_keys.keys():
79
91
                        file.add_string_attribute(x, '')
80
 
                #file.add_string_attribute('title', '')
81
 
                #file.add_string_attribute('album', '')
82
 
                #file.add_string_attribute('artist', '')
83
 
                #file.add_string_attribute('tracknumber', '')
84
 
                #file.add_string_attribute('genre', '')
85
 
                #file.add_string_attribute('date', '')
86
92
                file.add_string_attribute('bitrate', '')
87
93
                file.add_string_attribute('samplerate', '')
88
94
                file.add_string_attribute('length', '')
112
118
                                                val = audio[x][0]
113
119
                                                if x == "tracknumber": val = "%3s" % (val)
114
120
                                        file.add_string_attribute(x, val)
115
 
                                #try: file.add_string_attribute('title', audio["title"][0])
116
 
                                #except: file.add_string_attribute('title', "[n/a]")
117
 
                                #try: file.add_string_attribute('album', audio["album"][0])
118
 
                                #except: file.add_string_attribute('album', "[n/a]")
119
 
                                #try: file.add_string_attribute('artist', audio["artist"][0])
120
 
                                #except: file.add_string_attribute('artist', "[n/a]")
121
 
                                #try: file.add_string_attribute('tracknumber', audio["tracknumber"][0])
122
 
                                #except: file.add_string_attribute('tracknumber', "[n/a]")
123
 
                                #try: file.add_string_attribute('genre', audio["genre"][0])
124
 
                                #except: file.add_string_attribute('genre', "[n/a]")
125
 
                                #try: file.add_string_attribute('date', audio["date"][0])
126
 
                                #except: file.add_string_attribute('date', "[n/a]")
127
121
                        except:
128
122
                                # [SabreWolfy] some files have no ID3 tag and will throw this exception:
129
123
                                for x in EasyID3.valid_keys.keys():
130
124
                                        file.add_string_attribute(x, "[no ID3]")
131
 
                                #file.add_string_attribute('title', "[no ID3]")
132
 
                                #file.add_string_attribute('album', "[no ID3]")
133
 
                                #file.add_string_attribute('artist', "[no ID3]")
134
 
                                #file.add_string_attribute('tracknumber', "[no ID3]")
135
 
                                #file.add_string_attribute('genre', "[no ID3]")
136
 
                                #file.add_string_attribute('date', "[no ID3]")
137
125
                                
138
126
                        # try to read MP3 information (bitrate, length, samplerate)
139
127
                        try:
164
152
                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'):
165
153
                        # EXIF handling routines
166
154
                        try:
167
 
                                img = pyexiv2.Image(filename)
168
 
                                img.readMetadata()
169
 
                                file.add_string_attribute('exif_datetime_original',str(img['Exif.Photo.DateTimeOriginal']))
170
 
                                file.add_string_attribute('exif_software',str(img['Exif.Image.Software']))
171
 
                                file.add_string_attribute('exif_flash',str(img['Exif.Photo.Flash']))
172
 
                                file.add_string_attribute('exif_pixeldimensions',str(img['Exif.Photo.PixelXDimension'])+'x'+str(img['Exif.Photo.PixelYDimension']))
 
155
                                metadata = pyexiv2.ImageMetadata(filename)
 
156
                                metadata.read()
 
157
                                exif_datetimeoriginal = metadata['Exif.Photo.DateTimeOriginal']
 
158
                                exif_imagesoftware = metadata['Exif.Image.Software']
 
159
                                exif_photoflash = metadata['Exif.Photo.Flash']
 
160
                                exif_pixelydimension = metadata['Exif.Photo.PixelYDimension']
 
161
                                exif_pixelxdimension = metadata['Exif.Photo.PixelXDimension']
 
162
                                file.add_string_attribute('exif_datetime_original',str(exif_datetimeoriginal.raw_value))
 
163
                                file.add_string_attribute('exif_software',str(exif_imagesoftware.raw_value))
 
164
                                file.add_string_attribute('exif_flash',str(exif_photoflash.raw_value))
 
165
                                file.add_string_attribute('exif_pixeldimensions',str(exif_pixelydimension.raw_value)+'x'+str(exif_pixelxdimension.raw_value))
173
166
                        except:
174
167
                                # no exif data?
175
168
                                file.add_string_attribute('exif_datetime_original',"")
180
173
                        try:
181
174
                                im = Image.open(filename)
182
175
                                file.add_string_attribute('pixeldimensions',str(im.size[0])+'x'+str(im.size[1]))
 
176
                                try: im.close()
 
177
                                except: pass
183
178
                        except:
184
179
                                file.add_string_attribute('pixeldimensions',"[image read error]")
185
180
 
233
228
                                file.add_string_attribute('title', "[no info]")
234
229
                                file.add_string_attribute('artist', "[no info]")
235
230
 
236
 
                self.get_columns() 
 
231
                # mp4 AUDIO handling (Including ALAC *.m4a Apple Lossless)
 
232
                if file.is_mime_type('audio/mp4'):
 
233
                        # attempt to read MP4 tag
 
234
                        try:
 
235
                                audio = EasyMP4(filename)
 
236
                                # sometimes the audio variable will not have one of these items defined, that's why
 
237
                                # there is this long try / except attempt
 
238
                                try: file.add_string_attribute('title', "".join(audio["title"]))
 
239
                                except: file.add_string_attribute('title', [n/a])
 
240
                                try: file.add_string_attribute('album', "".join(audio["album"]))
 
241
                                except: file.add_string_attribute('album', "[n/a]")
 
242
                                try: file.add_string_attribute('artist', "".join(audio["artist"]))
 
243
                                except: file.add_string_attribute('artist', "[n/a]")
 
244
                                try: file.add_string_attribute('tracknumber', "".join(audio["tracknumber"]))
 
245
                                except: file.add_string_attribute('tracknumber', "[n/a]")
 
246
                                try: file.add_string_attribute('genre', "".join(audio["genre"]))
 
247
                                except: file.add_string_attribute('genre', "[n/a]")
 
248
                                try: file.add_string_attribute('date', "".join(audio["date"]))
 
249
                                except: file.add_string_attribute('date', "[n/a]")
 
250
                        except:
 
251
                                # some files have no MP4 tag and will throw this exception:
 
252
                                file.add_string_attribute('title', "[no MP4 Tag]")
 
253
                                file.add_string_attribute('album', "[no MP4 Tag]")
 
254
                                file.add_string_attribute('artist', "[no MP4 Tag]")
 
255
                                file.add_string_attribute('tracknumber', "[no MP4 Tag]")
 
256
                                file.add_string_attribute('genre', "[no MP4 Tag]")
 
257
                                file.add_string_attribute('date', "[no MP4 Tag]")
 
258
 
 
259
 
 
260
                #try to read MP4 information (bitrate, length, samplerate)
 
261
                try:
 
262
                        m4info=MP4(filename)
 
263
                        try: file.add_string_attribute('length', "%02i:%02i:%02i" % ((int(m4info.info.length/3600)), (int(m4info.info.length/60%60)), (int(m4info.info.length%60))))
 
264
                        except: file.add_string_attribute('length', "[n/a]")    
 
265
                        try: file.add_string_attribute('bitrate', str(m4info.info.bitrate))
 
266
                        except: file.add_string_attribute('bitrate', "[n/a]")
 
267
                        try: file.add_string_attribute('samplerate', str(m4info.info.sample_rate))
 
268
                        except: file.add_string_attribute('samplerate', "[n/a]")
 
269
                except:
 
270
                        file.add_string_attribute('bitrate', "error")
 
271
                        file.add_string_attribute('length', "error")
 
272
                        file.add_string_attribute('samplerate', "error")
 
273
                                        
 
274
                self.get_columns()