1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
#!/usr/bin/python
# -*- coding: latin1 -*-
### Copyright (C) 2011-2012 Damon Lynch <damonlynch@gmail.com>
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
### USA
import subprocess
import json
import datetime, time
import string
import multiprocessing
import logging
logger = multiprocessing.get_logger()
def version_info():
"""returns the version of exiftool being used"""
try:
# unfortunately subprocess.check_output does not exist on python 2.6
proc = subprocess.Popen(['exiftool', '-ver'], stdout=subprocess.PIPE)
v = proc.communicate()[0].strip()
except OSError:
v = None
return v
EXIFTOOL_VERSION = version_info()
class ExifToolMetaData:
"""
Class to use when a python based metadata parser fails to correctly load
necessary metadata. Calls exiftool as a subprocess. It is therefore slow,
but in contrast to exiv2 or kaa metadata, exiftool somtimes gives better
output.
"""
def __init__(self, filename):
self.filename = filename
self.metadata = None
self.metadata_string_format = None
self.exiftool_error = "Error encountered using exiftool with file %s"
self.exiftool_output = "Unexpected output from exiftool with file %s"
def _get(self, key, missing):
if key == "VideoStreamType" or "FileNumber":
# special case: want exiftool's string formatting
if self.metadata_string_format is None:
try:
proc = subprocess.Popen(['exiftool', '-j', self.filename], stdout=subprocess.PIPE)
s = proc.communicate()[0]
except:
logger.error(self.exiftool_error, self.filename)
return missing
try:
self.metadata_string_format = json.loads(s)
except:
logger.error(self.exiftool_output, self.filename)
return missing
try:
v = self.metadata_string_format[0][key]
except:
return missing
return v
elif self.metadata is None:
# note: exiftool's string formatting is OFF (-n switch)
try:
proc = subprocess.Popen(['exiftool', '-j', '-n', self.filename], stdout=subprocess.PIPE)
s = proc.communicate()[0]
except:
logger.error(self.exiftool_error, self.filename)
return missing
try:
self.metadata = json.loads(s)
except:
logger.error(self.exiftool_output, self.filename)
return missing
try:
v = self.metadata[0][key]
except:
return missing
return v
def date_time(self, missing=''):
"""
Returns in python datetime format the date and time the image was
recorded.
Trys to get value from key "DateTimeOriginal"
If that fails, tries "CreateDate"
Returns missing either metadata value is not present.
"""
d = self._get('DateTimeOriginal', None)
if d is None:
d = self._get('CreateDate', None)
if d is None:
d = self._get('FileModifyDate', None)
if d is not None:
try:
# returned value may or may not have a time offset
# strip it if need be
dt = d[:19]
dt = datetime.datetime.strptime(dt, "%Y:%m:%d %H:%M:%S")
except:
logger.error("Error reading date metadata with file %s", self.filename)
return missing
return dt
else:
return missing
def time_stamp(self, missing=''):
"""
Returns a float value representing the time stamp, if it exists
"""
dt = self.date_time(missing=None)
if dt:
# convert it to a time stamp (not optimal, but better than nothing!)
v = time.mktime(dt.timetuple())
else:
v = missing
return v
def file_number(self, missing=''):
v = self._get("FileNumber", None)
if v is not None:
return str(v)
else:
return missing
def width(self, missing=''):
v = self._get('ImageWidth', None)
if v is not None:
return str(v)
else:
return missing
def height(self, missing=''):
v = self._get('ImageHeight', None)
if v is not None:
return str(v)
else:
return missing
def length(self, missing=''):
"""
return the duration (length) of the video, rounded to the nearest second, in string format
"""
v = self._get("Duration", None)
if v is not None:
try:
v = float(v)
v = "%0.f" % v
except:
return missing
return v
else:
return missing
def frames_per_second(self, stream=0, missing=''):
"""
value stream is ignored (kept for compatibilty with code calling kaa)
"""
v = self._get("FrameRate", None)
if v is None:
v = self._get("VideoFrameRate", None)
if v is None:
return missing
try:
v = '%.0f' % v
except:
return missing
return v
def codec(self, stream=0, missing=''):
"""
value stream is ignored (kept for compatibilty with code calling kaa)
"""
v = self._get("VideoStreamType", None)
if v is None:
v = self._get("VideoCodec", None)
if v is not None:
return v
else:
return missing
def fourcc(self, stream=0, missing=''):
"""
value stream is ignored (kept for compatibilty with code calling kaa)
"""
return self._get("CompressorID", missing)
if __name__ == '__main__':
import sys
if (len(sys.argv) != 2):
print 'Usage: ' + sys.argv[0] + ' path/to/video/containing/metadata'
sys.exit(0)
else:
m = ExifToolMetaData(sys.argv[1])
dt = m.date_time()
print dt
print "%sx%s" % (m.width(), m.height())
print m.length()
print m.frames_per_second()
print m.codec()
|