Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more examples #9

Open
drewish opened this issue Apr 27, 2012 · 0 comments
Open

Add more examples #9

drewish opened this issue Apr 27, 2012 · 0 comments

Comments

@drewish
Copy link
Collaborator

drewish commented Apr 27, 2012

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant