Try to infer dates from the filename if there is no "date" metadata

pull/64/merge
Romain 8 years ago
parent 8b66a16134
commit 3531f1e533

@ -3,6 +3,13 @@ const moment = require('moment')
const path = require('path')
const EXIF_DATE_FORMAT = 'YYYY:MM:DD HH:mm:ssZ'
// infer dates from files with a date-looking filename
const FILENAME_DATE_REGEX = /\d{4}[_\-.\s]?(\d{2}[_\-.\s]?){5}\..{3,4}/
// moment ignores non-numeric characters when parsing
const FILENAME_DATE_FORMAT = 'YYYYMMDD HHmmss'
var index = 0
/*
@ -13,7 +20,7 @@ function Media (file) {
this.file = file
this.filename = path.basename(file.path)
this.urls = _.mapValues(file.output, o => o.path)
this.date = exifDate(file)
this.date = getDate(file)
this.caption = caption(file)
this.isVideo = (file.type === 'video')
this.isAnimated = animated(file)
@ -26,13 +33,17 @@ function Media (file) {
// ]
}
function exifDate (file) {
function getDate (file) {
const date = tagValue(file, 'EXIF', 'DateTimeOriginal') ||
tagValue(file, 'H264', 'DateTimeOriginal') ||
tagValue(file, 'QuickTime', 'CreationDate')
if (date) {
return moment(date, EXIF_DATE_FORMAT).valueOf()
} else {
if (FILENAME_DATE_REGEX.test(file.path)) {
const namedate = moment(file.path, FILENAME_DATE_FORMAT)
if (namedate.isValid()) return namedate.valueOf()
}
return file.date
}
}

@ -27,7 +27,35 @@ describe('Media', function () {
should(media.date).eql(fixtures.date('2016-10-28 17:34:58').getTime())
})
it('defaults to the file date if there is no date in the metadata', function () {
it('infers the date from the filename (Android format)', function () {
const file = fixtures.file({path: 'folder/VID_20170220_114006.mp4'})
const media = new Media(file)
should(media.date).eql(fixtures.date('2017-02-20 11:40:06').getTime())
})
it('infers the date from the filename (Dropbox format)', function () {
const file = fixtures.file({path: 'folder/2017-03-24 19.42.30.jpg'})
const media = new Media(file)
should(media.date).eql(fixtures.date('2017-03-24 19:42:30').getTime())
})
it('only infers dates from valid formats', function () {
const file = fixtures.file({
path: 'folder/IMG_1234.jpg',
date: '2016-10-28 17:34:58'
})
const media = new Media(file)
should(media.date).eql(fixtures.date('2016-10-28 17:34:58').getTime())
})
it('does not look at the file name if it already has EXIF data', function () {
const file = fixtures.file({path: '2017-03-24 19.42.30.jpg'})
file.meta.EXIF.DateTimeOriginal = '2016:10:28 17:34:58'
const media = new Media(file)
should(media.date).eql(fixtures.date('2016-10-28 17:34:58').getTime())
})
it('defaults to the file date if there is no other date', function () {
const file = fixtures.file({date: '2016-10-28 17:34:58'})
const media = new Media(file)
should(media.date).eql(fixtures.date('2016-10-28 17:34:58').getTime())

Loading…
Cancel
Save