Updated time parsing to work with different formats

This commit is contained in:
Jaisen Mathai 2015-10-14 21:27:42 -07:00
parent 83fea003f4
commit 82ff1da944
3 changed files with 11 additions and 37 deletions

View File

@ -5,6 +5,7 @@ Media package that handles all video operations
# load modules
from sys import argv
from datetime import datetime
from fractions import Fraction
import LatLon
import mimetypes
@ -117,9 +118,11 @@ class Media(object):
for key in self.exif_map['date_taken']:
try:
if(key in exif):
seconds_since_epoch = time.mktime(datetime.strptime(str(exif[key].value), '%Y:%m:%d %H:%M:%S').timetuple())
break;
except:
if(re.match('\d{4}(-|:)\d{2}(-|:)\d{2}', str(exif[key].value)) is not None):
seconds_since_epoch = time.mktime(exif[key].value.timetuple())
break;
except BaseException as e:
print e
pass
if(seconds_since_epoch == 0):

View File

@ -30,37 +30,6 @@ class Photo(Media):
# We only want to parse EXIF once so we store it here
self.exif = None
"""
Get the date which the photo was taken.
The date value returned is defined by the min() of mtime and ctime.
@returns, time object or None for non-photo files or 0 timestamp
"""
def get_date_taken(self):
if(not self.is_valid()):
return None
source = self.source
seconds_since_epoch = min(os.path.getmtime(source), os.path.getctime(source))
# We need to parse a string from EXIF into a timestamp.
# EXIF DateTimeOriginal and EXIF DateTime are both stored in %Y:%m:%d %H:%M:%S format
# we use date.strptime -> .timetuple -> time.mktime to do the conversion in the local timezone
# EXIF DateTime is already stored as a timestamp
# Sourced from https://github.com/photo/frontend/blob/master/src/libraries/models/Photo.php#L500
exif = self.get_exif()
for key in self.exif_map['date_taken']:
try:
if(key in exif):
seconds_since_epoch = time.mktime(datetime.strptime(str(exif[key]), '%Y:%m:%d %H:%M:%S').timetuple())
break;
except:
pass
if(seconds_since_epoch == 0):
return None
return time.gmtime(seconds_since_epoch)
"""
Get the duration of a photo in seconds.
Uses ffmpeg/ffprobe

View File

@ -15,14 +15,16 @@ def main(argv):
print 'No file specified'
sys.exit(1)
if('type' in args and args['type'] == 'photo'):
media_type = Photo
else:
if('type' in args and args['type'] == 'video'):
media_type = Video
else:
media_type = Photo
media = media_type(args['file'])
metadata = media.get_metadata()
print '%r' % metadata
output = {'date_taken': metadata['date_taken']}
print '%r' % output