Last active
September 10, 2016 22:01
-
-
Save paazmaya/ad8710b9142bd81de64b to your computer and use it in GitHub Desktop.
Node.js script for renaming burst images from Xperia Z2 camera, as it has not enough options
This file contains hidden or 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
/** | |
* node-xperia-camera-rename.js | |
* Juga Paazmaya <[email protected]> | |
* | |
* Take a folder that originates from Sony Xperia Z2 camera | |
* and rename the burst images, so that they can exist in a | |
* same folder. | |
* | |
* node node-xperia-camera-rename.js . | |
*/ | |
'use strict'; | |
if (process.argv.length !== 3) { | |
console.log('Missing arguments, must only contain directory path.'); | |
process.exit(1); | |
} | |
var fs = require('fs'), | |
path = require('path'); | |
/** | |
* @param {string} name Something like 20140527111208508 | |
* @returns {Date} Date object that has value of something like 2014-05-27T11:12:08.508 | |
*/ | |
var name2date = function name2date(name) { | |
var date = new Date(); | |
date.setFullYear(name.substr(0, 4)); | |
date.setMonth(parseInt(name.substr(4, 2), 10) - 1); | |
date.setDate(name.substr(6, 2)); | |
date.setHours(name.substr(8, 2)); | |
date.setMinutes(name.substr(10, 2)); | |
date.setSeconds(name.substr(12, 2)); | |
date.setMilliseconds(name.substr(14, 3)); | |
return date; | |
}; | |
/** | |
* Pad the given number with leading zero, if less than 10. | |
* @returns {string|number} Number with leading zero added if needed; | |
*/ | |
var pad = function pad(number) { | |
if (number < 10) { | |
return '0' + number; | |
} | |
return number; | |
}; | |
/** | |
* Return partial ISO formatted string of the given date, | |
* but based on the current timezone. | |
* @returns {string} Date string that has value of something like 2014-05-27_11-12-08 | |
*/ | |
var shortIsoDate = function shortIsoDate(date) { | |
return date.getFullYear() + | |
'-' + pad(date.getMonth() + 1) + | |
'-' + pad(date.getDate()) + | |
'_' + pad(date.getHours()) + | |
'-' + pad(date.getUTCMinutes()) + | |
'-' + pad(date.getSeconds()); | |
}; | |
/** | |
* Rename the given source according to the given date and index, | |
* to the destination directory. | |
* @param {Date} date Something like 2014-05-27T11:12:08.508 | |
* @param {string} source Something like 20140527111208508/DSC_00000003.jpg | |
* @param {string} destinationDir here | |
* @param {number} index Integer something from 0 to ... | |
* @returns Whatever fs.existsSync() returns... | |
* @see http://nodejs.org/api/fs.html#fs_fs_renamesync_oldpath_newpath | |
*/ | |
var rename = function rename(date, source, destinationDir, index) { | |
// date = source parent name parsed | |
var suffix = source.split('.').pop().toLowerCase(); | |
var numeric = '' + index; | |
while (numeric.length < 4) { | |
numeric = '0' + numeric; | |
} | |
var filepath = path.join(destinationDir, shortIsoDate(date) + '_' + numeric); | |
var destination = filepath + '.' + suffix; | |
while (fs.existsSync(destination)) { | |
destination = filepath + Math.round(Math.random() * 1000) + '.' + suffix; | |
} | |
console.log('rename() index: ' + index + ', source: ' + source + ', destination: ' + destination); | |
fs.renameSync(source, destination); | |
return fs.existsSync(destination); | |
}; | |
/** | |
* Iterate the given date based named directory under which images should be. | |
* @param {string} sourceDir A directory, whose name is something similar to a date | |
* @param {string} destinationDir A directory where the pictures are to be moved | |
* @returns {boolean|number} False is something went wrong, or the number of renamed files | |
*/ | |
var iterateChildren = function iterateChildren(sourceDir, destinationDir) { | |
if (!fs.existsSync(sourceDir)) { | |
return false; | |
} | |
console.log('iterateChildren. sourceDir: ' + sourceDir + ', destinationDir: ' + destinationDir); | |
var date = name2date(path.basename(sourceDir)); | |
var files = fs.readdirSync(sourceDir); | |
var counter = 0; | |
files.forEach(function (file, index) { | |
var filepath = path.join(sourceDir, file); | |
var stats = fs.statSync(filepath); | |
if (stats.isFile()) { | |
var success = rename(date, filepath, destinationDir, index); | |
if (success) { | |
counter++; | |
} | |
} | |
}); | |
files = fs.readdirSync(sourceDir); | |
if (files.length === 0) { | |
var removal = fs.rmdirSync(sourceDir); | |
console.log('Removed the above mentioned sourceDir (' + sourceDir + ')'); | |
} | |
return counter; | |
}; | |
/** | |
* Iterate the given root directory under which should be those directories | |
* that contain each burst session, named according to the initiation time. | |
* @param {string} rootDir Root directory, most likely called 'burst' | |
* @returns {boolean|number} False is something went wrong, or the number of renamed files | |
*/ | |
var iterateRoot = function iterateRoot(rootDir) { | |
if (!fs.existsSync(rootDir)) { | |
return false; | |
} | |
var files = fs.readdirSync(rootDir); | |
var counter = 0; | |
files.forEach(function (file) { | |
var filepath = path.join(rootDir, file); | |
var stats = fs.statSync(filepath); | |
if (stats.isDirectory()) { | |
console.log('iterateRoot. file: ' + file); | |
var success = iterateChildren(filepath, rootDir); | |
if (success) { | |
counter++; | |
} | |
} | |
}); | |
return counter; | |
}; | |
// arg 2 should be the directory under which are many whose names are dates | |
var total = iterateRoot(process.argv[2]); | |
console.log('Total number of processed folders: ' + total); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use https://github.com/paazmaya/image-flatify instead of this script.