Last active
April 13, 2024 18:30
-
-
Save tenbits/5458252 to your computer and use it in GitHub Desktop.
nodejs script to set a file modification date from EXIF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Change file modification time to the date from EXIF | |
* | |
* System Requirements: | |
* | |
* > npm install -g includejs | |
* > npm install -g exif | |
* | |
* Usage: | |
* | |
* Drop this script to any folder with images, and run in that directory | |
* | |
* > ijs custom exif | |
*/ | |
var ExifImage = require('exif').ExifImage, | |
fs = require('fs'); | |
include.exports = { | |
process: function() { | |
ruqq.arr.each(new io.Directory().readFiles('**.jpg').files, function(file) { | |
new ExifImage({ | |
image: file.uri.toLocalFile() | |
}, function(error, exif) { | |
if (error) { | |
console.error(error); | |
return; | |
} | |
processFile(file, exif); | |
}) | |
}); | |
} | |
} | |
function processFile(file, exif) { | |
var date = resolveDate(exif), | |
timestamp; | |
if (date == null) { | |
console.error('Date not resolved', file.uri.toString()); | |
return; | |
} | |
timestamp = +date / 1000 | 0; | |
try { | |
fs.utimesSync(file.uri.toLocalFile(), timestamp, timestamp); | |
} catch (error) { | |
console.error(error.toString(), 'timestamp', timestamp); | |
return; | |
} | |
console.log(color('green{Changed - }'), file.uri.file, '-', date.toString()); | |
} | |
function resolveDate(exif) { | |
var dateMeta = ruqq.arr.first(exif.image, 'tagName', '==', 'DateTime'), | |
string = dateMeta.value, | |
date = new Date(string); | |
if (!isNaN(date)) { | |
return date; | |
} | |
// (CANON) - 2012:12:16 23:03:01 | |
string = string.replace(/([\d]{4})[^\s]([\d]+)[^\s]([\d]+)/, function(full, num1, num2, num3) { | |
return Array.prototype.slice.call(arguments, 1, 4).join('-'); | |
}); | |
date = new Date(string); | |
if (!isNaN(date)) { | |
return date; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!
I had a lot of images with broken creation date, and used your script as a base for solution