Skip to content

Commit

Permalink
Use exiftool-vendored instead of exif package. It's faster and works …
Browse files Browse the repository at this point in the history
…with mp4 files.
  • Loading branch information
gabooh committed Mar 29, 2019
1 parent 625e2d5 commit f31f350
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 47 deletions.
51 changes: 42 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"dependencies": {
"commander": "^2.19.0",
"execution-time": "^1.3.0",
"exif": "^0.6.0",
"exiftool-vendored": "^8.7.0",
"lodash": "^4.17.11",
"moment": "^2.24.0"
},
"bin": "./photo-organize.js",
"engines": {
"node": "10.5"
"node": "10"
},
"devDependencies": {
"standard": "^12.0.1"
Expand Down
72 changes: 36 additions & 36 deletions photo-organize.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
'use strict'

const program = require('commander')
const path = require('path')
const fs = require('fs')
const _ = require('lodash')
const exif = require('exif').ExifImage
const exiftool = require('exiftool-vendored').exiftool
const moment = require('moment')
const perf = require('execution-time')()
// const q = require('q')

const validExtensions = ['jpg', 'jpeg', 'mp4']
const morningLimit = 5 // 5am;
Expand Down Expand Up @@ -44,9 +42,13 @@ if (!_.endsWith(directoryName, '/')) {
directoryName += '/'
}

processDirectory(directoryName).catch(error => {
console.error(error)
})
processDirectory(directoryName)
.catch(error => {
console.error(error)
}).finally(() => {
return exiftool.end()
}
)

/**
* Process a directory : list files, get timestamp and move them to appropriate subdirectory.
Expand All @@ -71,16 +73,7 @@ async function processDirectory () {
return
}

let date = null
if (path.extname(file) === '.mp4') {
// exif doesn't support mp4 files
date = moment(fs.statSync(directoryName + file).mtime)
} else {
const exifDate = await getDateFromExif(file)
if (exifDate != null && exifDate.isValid()) {
date = exifDate
}
}
let date = await readExifDate(file)

if (date === null) {
date = moment(fs.statSync(directoryName + file).mtime)
Expand All @@ -103,28 +96,35 @@ async function processDirectory () {
}

/**
* Get the date of the picture from the exif data
* @param file the file to read
* @return {Promise}
*
* @param file the media file to read
* @returns {Promise<Date>}
*/
function getDateFromExif (file) {
return new Promise((resolve) => {
new exif({ image: directoryName + file }, (err, exifData) => {
if (err) {
console.error(err)
resolve(null)
return
}

let dateString
if (exifData['exif']['DateTimeOriginal'] !== undefined) {
dateString = exifData['exif']['DateTimeOriginal']
} else if (exifData['exif']['DateTime'] !== undefined) {
dateString = exifData['exif']['DateTime']
function readExifDate (file) {
return new Promise((resolve, reject) => {
exiftool
.read(directoryName + file)
.then((tags) => {
if (tags.errors.length) {
console.error(`error reading ${file} : ${tags.errors}`)
resolve(null)
return
}

if (tags.CreateDate) {
resolve(moment(tags.CreateDate.toDate()))
} else if (tags.ModifyDate) {
resolve(moment(tags.ModifyDate.toDate()))
} else {
console.error(`no date found in exif tags for file ${file}`)
resolve(null)
}
}
resolve(moment(dateString, 'YYYY:MM:DD HH:mm:ss'))
}
)
)
.catch(err => {
console.error(`Error reading tag for ${file} `, err)
reject(err)
})
})
}

Expand Down

0 comments on commit f31f350

Please sign in to comment.