1
|
|
|
""" |
2
|
|
|
The media module provides a base :class:`Media` class for media objects that |
3
|
|
|
are tracked by Elodie. The Media class provides some base functionality used |
4
|
|
|
by all the media types, but isn't itself used to represent anything. Its |
5
|
|
|
sub-classes (:class:`~elodie.media.audio.Audio`, |
6
|
|
|
:class:`~elodie.media.photo.Photo`, and :class:`~elodie.media.video.Video`) |
7
|
|
|
are used to represent the actual files. |
8
|
|
|
|
9
|
|
|
.. moduleauthor:: Jaisen Mathai <[email protected]> |
10
|
|
|
""" |
11
|
|
|
from __future__ import print_function |
12
|
|
|
|
13
|
|
|
import os |
14
|
|
|
import six |
15
|
|
|
|
16
|
|
|
# load modules |
17
|
|
|
from elodie.external.pyexiftool import ExifTool |
18
|
|
|
from elodie.media.base import Base |
19
|
|
|
|
20
|
|
|
class Media(Base): |
21
|
|
|
|
22
|
|
|
"""The base class for all media objects. |
23
|
|
|
|
24
|
|
|
:param str source: The fully qualified path to the video file. |
25
|
|
|
""" |
26
|
|
|
|
27
|
|
|
__name__ = 'Media' |
28
|
|
|
|
29
|
|
|
d_coordinates = { |
30
|
|
|
'latitude': 'latitude_ref', |
31
|
|
|
'longitude': 'longitude_ref' |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
def __init__(self, source=None): |
35
|
|
|
super(Media, self).__init__(source) |
36
|
|
|
self.exif_map = { |
37
|
|
|
'date_taken': [ |
38
|
|
|
'EXIF:DateTimeOriginal', |
39
|
|
|
'EXIF:CreateDate', |
40
|
|
|
'EXIF:ModifyDate' |
41
|
|
|
] |
42
|
|
|
} |
43
|
|
|
self.camera_make_keys = ['EXIF:Make', 'QuickTime:Make'] |
44
|
|
|
self.camera_model_keys = ['EXIF:Model', 'QuickTime:Model'] |
45
|
|
|
self.album_keys = ['XMP-xmpDM:Album', 'XMP:Album'] |
46
|
|
|
self.title_key = 'XMP:Title' |
47
|
|
|
self.latitude_keys = ['EXIF:GPSLatitude'] |
48
|
|
|
self.longitude_keys = ['EXIF:GPSLongitude'] |
49
|
|
|
self.latitude_ref_key = 'EXIF:GPSLatitudeRef' |
50
|
|
|
self.longitude_ref_key = 'EXIF:GPSLongitudeRef' |
51
|
|
|
self.original_name_key = 'XMP:OriginalFileName' |
52
|
|
|
self.set_gps_ref = True |
53
|
|
|
self.exif_metadata = None |
54
|
|
|
|
55
|
|
|
def get_album(self): |
56
|
|
|
"""Get album from EXIF |
57
|
|
|
|
58
|
|
|
:returns: None or string |
59
|
|
|
""" |
60
|
|
|
if(not self.is_valid()): |
61
|
|
|
return None |
62
|
|
|
|
63
|
|
|
exiftool_attributes = self.get_exiftool_attributes() |
64
|
|
|
if exiftool_attributes is None: |
65
|
|
|
return None |
66
|
|
|
|
67
|
|
|
for album_key in self.album_keys: |
68
|
|
|
if album_key in exiftool_attributes: |
69
|
|
|
return exiftool_attributes[album_key] |
70
|
|
|
|
71
|
|
|
return None |
72
|
|
|
|
73
|
|
|
def get_coordinate(self, type='latitude'): |
74
|
|
|
"""Get latitude or longitude of media from EXIF |
75
|
|
|
|
76
|
|
|
:param str type: Type of coordinate to get. Either "latitude" or |
77
|
|
|
"longitude". |
78
|
|
|
:returns: float or None if not present in EXIF or a non-photo file |
79
|
|
|
""" |
80
|
|
|
|
81
|
|
|
exif = self.get_exiftool_attributes() |
82
|
|
|
if not exif: |
83
|
|
|
return None |
84
|
|
|
|
85
|
|
|
# The lat/lon _keys array has an order of precedence. |
86
|
|
|
# The first key is writable and we will give the writable |
87
|
|
|
# key precence when reading. |
88
|
|
|
direction_multiplier = 1.0 |
89
|
|
|
for key in self.latitude_keys + self.longitude_keys: |
90
|
|
|
if key not in exif: |
91
|
|
|
continue |
92
|
|
|
if isinstance(exif[key], six.string_types) and len(exif[key]) == 0: |
93
|
|
|
# If exiftool GPS output is empty, the data returned will be a str |
94
|
|
|
# with 0 length. |
95
|
|
|
# https://github.com/jmathai/elodie/issues/354 |
96
|
|
|
continue |
97
|
|
|
|
98
|
|
|
# Cast coordinate to a float due to a bug in exiftool's |
99
|
|
|
# -json output format. |
100
|
|
|
# https://github.com/jmathai/elodie/issues/171 |
101
|
|
|
# http://u88.n24.queensu.ca/exiftool/forum/index.php/topic,7952.0.html # noqa |
102
|
|
|
this_coordinate = float(exif[key]) |
103
|
|
|
|
104
|
|
|
# TODO: verify that we need to check ref key |
105
|
|
|
# when self.set_gps_ref != True |
106
|
|
|
if type == 'latitude' and key in self.latitude_keys: |
107
|
|
|
if self.latitude_ref_key in exif and \ |
108
|
|
|
exif[self.latitude_ref_key] == 'S': |
109
|
|
|
direction_multiplier = -1.0 |
110
|
|
|
return this_coordinate * direction_multiplier |
111
|
|
|
elif type == 'longitude' and key in self.longitude_keys: |
112
|
|
|
if self.longitude_ref_key in exif and \ |
113
|
|
|
exif[self.longitude_ref_key] == 'W': |
114
|
|
|
direction_multiplier = -1.0 |
115
|
|
|
return this_coordinate * direction_multiplier |
116
|
|
|
|
117
|
|
|
return None |
118
|
|
|
|
119
|
|
|
def get_exiftool_attributes(self): |
120
|
|
|
"""Get attributes for the media object from exiftool. |
121
|
|
|
|
122
|
|
|
:returns: dict, or False if exiftool was not available. |
123
|
|
|
""" |
124
|
|
|
source = self.source |
125
|
|
|
|
126
|
|
|
#Cache exif metadata results and use if already exists for media |
127
|
|
|
if(self.exif_metadata is None): |
128
|
|
|
self.exif_metadata = ExifTool().get_metadata(source) |
129
|
|
|
|
130
|
|
|
if not self.exif_metadata: |
131
|
|
|
return False |
132
|
|
|
|
133
|
|
|
return self.exif_metadata |
134
|
|
|
|
135
|
|
|
def get_camera_make(self): |
136
|
|
|
"""Get the camera make stored in EXIF. |
137
|
|
|
|
138
|
|
|
:returns: str |
139
|
|
|
""" |
140
|
|
|
if(not self.is_valid()): |
141
|
|
|
return None |
142
|
|
|
|
143
|
|
|
exiftool_attributes = self.get_exiftool_attributes() |
144
|
|
|
|
145
|
|
|
if exiftool_attributes is None: |
146
|
|
|
return None |
147
|
|
|
|
148
|
|
|
for camera_make_key in self.camera_make_keys: |
149
|
|
|
if camera_make_key in exiftool_attributes: |
150
|
|
|
return exiftool_attributes[camera_make_key] |
151
|
|
|
|
152
|
|
|
return None |
153
|
|
|
|
154
|
|
|
def get_camera_model(self): |
155
|
|
|
"""Get the camera make stored in EXIF. |
156
|
|
|
|
157
|
|
|
:returns: str |
158
|
|
|
""" |
159
|
|
|
if(not self.is_valid()): |
160
|
|
|
return None |
161
|
|
|
|
162
|
|
|
exiftool_attributes = self.get_exiftool_attributes() |
163
|
|
|
|
164
|
|
|
if exiftool_attributes is None: |
165
|
|
|
return None |
166
|
|
|
|
167
|
|
|
for camera_model_key in self.camera_model_keys: |
168
|
|
|
if camera_model_key in exiftool_attributes: |
169
|
|
|
return exiftool_attributes[camera_model_key] |
170
|
|
|
|
171
|
|
|
return None |
172
|
|
|
|
173
|
|
|
def get_original_name(self): |
174
|
|
|
"""Get the original name stored in EXIF. |
175
|
|
|
|
176
|
|
|
:returns: str |
177
|
|
|
""" |
178
|
|
|
if(not self.is_valid()): |
179
|
|
|
return None |
180
|
|
|
|
181
|
|
|
exiftool_attributes = self.get_exiftool_attributes() |
182
|
|
|
|
183
|
|
|
if exiftool_attributes is None: |
184
|
|
|
return None |
185
|
|
|
|
186
|
|
|
if(self.original_name_key not in exiftool_attributes): |
187
|
|
|
return None |
188
|
|
|
|
189
|
|
|
return exiftool_attributes[self.original_name_key] |
190
|
|
|
|
191
|
|
|
def get_title(self): |
192
|
|
|
"""Get the title for a photo of video |
193
|
|
|
|
194
|
|
|
:returns: str or None if no title is set or not a valid media type |
195
|
|
|
""" |
196
|
|
|
if(not self.is_valid()): |
197
|
|
|
return None |
198
|
|
|
|
199
|
|
|
exiftool_attributes = self.get_exiftool_attributes() |
200
|
|
|
|
201
|
|
|
if exiftool_attributes is None: |
202
|
|
|
return None |
203
|
|
|
|
204
|
|
|
if(self.title_key not in exiftool_attributes): |
205
|
|
|
return None |
206
|
|
|
|
207
|
|
|
return exiftool_attributes[self.title_key] |
208
|
|
|
|
209
|
|
|
def reset_cache(self): |
210
|
|
|
"""Resets any internal cache |
211
|
|
|
""" |
212
|
|
|
self.exiftool_attributes = None |
213
|
|
|
self.exif_metadata = None |
214
|
|
|
super(Media, self).reset_cache() |
215
|
|
|
|
216
|
|
|
def set_album(self, album): |
217
|
|
|
"""Set album for a photo |
218
|
|
|
|
219
|
|
|
:param str name: Name of album |
220
|
|
|
:returns: bool |
221
|
|
|
""" |
222
|
|
|
if(not self.is_valid()): |
223
|
|
|
return None |
224
|
|
|
|
225
|
|
|
tags = {self.album_keys[0]: album} |
226
|
|
|
status = self.__set_tags(tags) |
227
|
|
|
self.reset_cache() |
228
|
|
|
|
229
|
|
|
return status |
230
|
|
|
|
231
|
|
|
def set_date_taken(self, time): |
232
|
|
|
"""Set the date/time a photo was taken. |
233
|
|
|
|
234
|
|
|
:param datetime time: datetime object of when the photo was taken |
235
|
|
|
:returns: bool |
236
|
|
|
""" |
237
|
|
|
if(time is None): |
238
|
|
|
return False |
239
|
|
|
|
240
|
|
|
tags = {} |
241
|
|
|
formatted_time = time.strftime('%Y:%m:%d %H:%M:%S') |
242
|
|
|
for key in self.exif_map['date_taken']: |
243
|
|
|
tags[key] = formatted_time |
244
|
|
|
|
245
|
|
|
status = self.__set_tags(tags) |
246
|
|
|
self.reset_cache() |
247
|
|
|
return status |
248
|
|
|
|
249
|
|
|
def set_location(self, latitude, longitude): |
250
|
|
|
if(not self.is_valid()): |
251
|
|
|
return None |
252
|
|
|
|
253
|
|
|
# The lat/lon _keys array has an order of precedence. |
254
|
|
|
# The first key is writable and we will give the writable |
255
|
|
|
# key precence when reading. |
256
|
|
|
tags = { |
257
|
|
|
self.latitude_keys[0]: latitude, |
258
|
|
|
self.longitude_keys[0]: longitude, |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
# If self.set_gps_ref == True then it means we are writing an EXIF |
262
|
|
|
# GPS tag which requires us to set the reference key. |
263
|
|
|
# That's because the lat/lon are absolute values. |
264
|
|
|
if self.set_gps_ref: |
265
|
|
|
if latitude < 0: |
266
|
|
|
tags[self.latitude_ref_key] = 'S' |
267
|
|
|
|
268
|
|
|
if longitude < 0: |
269
|
|
|
tags[self.longitude_ref_key] = 'W' |
270
|
|
|
|
271
|
|
|
status = self.__set_tags(tags) |
272
|
|
|
self.reset_cache() |
273
|
|
|
|
274
|
|
|
return status |
275
|
|
|
|
276
|
|
|
def set_original_name(self, name=None): |
277
|
|
|
"""Sets the original name EXIF tag if not already set. |
278
|
|
|
|
279
|
|
|
:returns: True, False, None |
280
|
|
|
""" |
281
|
|
|
if(not self.is_valid()): |
282
|
|
|
return None |
283
|
|
|
|
284
|
|
|
# If EXIF original name tag is set then we return. |
285
|
|
|
if self.get_original_name() is not None: |
286
|
|
|
return None |
287
|
|
|
|
288
|
|
|
source = self.source |
289
|
|
|
|
290
|
|
|
if not name: |
291
|
|
|
name = os.path.basename(source) |
292
|
|
|
|
293
|
|
|
tags = {self.original_name_key: name} |
294
|
|
|
status = self.__set_tags(tags) |
295
|
|
|
self.reset_cache() |
296
|
|
|
return status |
297
|
|
|
|
298
|
|
|
def set_title(self, title): |
299
|
|
|
"""Set title for a photo. |
300
|
|
|
|
301
|
|
|
:param str title: Title of the photo. |
302
|
|
|
:returns: bool |
303
|
|
|
""" |
304
|
|
|
if(not self.is_valid()): |
305
|
|
|
return None |
306
|
|
|
|
307
|
|
|
if(title is None): |
308
|
|
|
return None |
309
|
|
|
|
310
|
|
|
tags = {self.title_key: title} |
311
|
|
|
status = self.__set_tags(tags) |
312
|
|
|
self.reset_cache() |
313
|
|
|
|
314
|
|
|
return status |
315
|
|
|
|
316
|
|
|
def __set_tags(self, tags): |
317
|
|
|
if(not self.is_valid()): |
318
|
|
|
return None |
319
|
|
|
|
320
|
|
|
source = self.source |
321
|
|
|
|
322
|
|
|
status = '' |
323
|
|
|
status = ExifTool().set_tags(tags,source) |
324
|
|
|
|
325
|
|
|
return status != '' |
326
|
|
|
|