Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add more examples #9

Open
drewish opened this Issue · 0 comments

1 participant

@drewish
Collaborator

I think it'd be good to provide some more examples. One I'd thought about was dealing with dates, something like:

    var fs = require('fs')
      , path = require('path')
      , exiv = require('exiv2')
      , async = require('async');

    /**
     * Convert exiv2's date time strings into Date instances.
     *
     * The first parameter is the date string from exiv2, e.g. '2012:04:14 17:45:52'
     * a tag like 'Exif.Photo.DateTimeOriginal'.
     *
     * The second parameter may be hundredths of a second (0-99) to add to the date,
     * e.g. the Exif.Photo.SubSecTimeOriginal tag. This can be useful when sorting
     * photos by time and photos may have may been taken in a single second.
     */
    function makeDate(exivDate, subSec) {
      // Date.parse() doesn't recognize the custom format so the easiest thing to
      // do is convert it to ISO 8601 (e.g. "2012-04-14T17:45:52") which it
      // recognizes. The colons in the date need to become hyphens and the space
      // needs to become a 'T'.

      // So split it into two...
      var datetime = exivDate.split(' ');
      // ...then change the colons in date into hyphens...
      datetime[0] = datetime[0].replace(/:/g, '-');
      // ...then join it back together with a 'T' and parse it.
      var d = new Date(Date.parse(datetime.join('T')));

      // If they provided sub-second data, scale it from hundredths of a second to
      // thousands and apply it to the date.
      if (subSec) d.setMilliseconds(subSec * 10)

      return d;
    }

    var dirpath = '100EOS5D';
    path.exists(dirpath, function (exists) {
      if (exists) {
        if (fs.statSync(dirpath).isDirectory()) {
          var files = fs.readdirSync(dirpath);
          // Use async to handle a all the delayed callbacks.
          async.sortBy(files,
            function iterate(filename, done) {
              var filepath = path.join(dirpath, filename),
                  sequence = /^IMG_(\d{4})\.CR2$/i.exec(filename);
              // Make sure it's one of my photos.
              if (sequence) {
                exiv.getImageTags(filepath, function(err, tags) {
                  var date = makeDate(tags['Exif.Photo.DateTimeOriginal'], tags['Exif.Photo.SubSecTimeOriginal']);
                  // console.log("%s: %s", filename, date);
                  done(err, date);
                });
              }
            },
            function complete(err, sorted) {
              console.log(sorted);
            }
          );
        }
      }
    });

But I need to debug this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.