Using exiftool to put date, rating, and title in photo filenames

Now that we have a Synology backup server, I want to get better at keeping and organizing photos. I’ve got lots of pictures and videos of A- in Google Photos, but I don’t want to rely only on that. When Google eventually decommissions the service (no signs of this now, but you can never tell), I’d like to already have copies of my favourite photos and videos all prioritized and backed up instead of spending days wading through accumulated cruft.

I had previously added significant moments to a “Weekly highlights” album in the Google Photos app. I had also tried to select a number of those for “Level 2 highlights” and “Level 3 highlights” roughly approximating monthly and yearly filters. This was awkward, though, since Google Photos didn’t let me see which photos were already in an album or which timespans weren’t well represented. I wanted a five-star rating system so that I could gradually winnow images I liked, and I wanted tags for more flexibility.

The F-Stop Gallery app on Android seemed to be a quick way to sort through images, rate them, and tag them. I liked how it stored the metadata in the file instead of in a separate database. The bulk management tools were decent, although of course it would be even better to do things with fewer taps.

I downloaded ZIPs of my highlight albums and extracted them to a Samba share so that I could access them from my phone. I rated the level 3 highlights as 5 stars, level 2 as 3 stars, and weekly highlights as 2 stars, leaving the 4-star level for finer distinctions if I need to fiddle with things. Then I used cp -n (no clobber; don’t overwrite existing files) to copy the 5-star photos, then the 3-star photos, and lastly the 2-star photos. That way, even if a photo was in multiple albums, the file would have just the highest rating I assigned.

I can’t always rely on apps to index and search by the metadata in a file, so I like putting that information in the filename. A good filename might look like this:

2016-08-01-20-14-44 ### Relaxing on the deck #family.jpg

It starts with a date and time, since timelines make sense. I add one to the number of stars and convert that to # so that I can easily search for, say, ### to show me all entries that have at least two stars. This also creates a neat ASCII bar chart effect when looking at lists of filenames. I want to include the title if available, and any tags for easy searching as well.

Here’s the Bash script that takes an image file and renames it accordingly:

exiftool -m \
  -"Filename<\${DateTimeOriginal;s/[ :]/-/g} \${Rating;s/([1-5])/'#' x (\$1 + 1)/e} \${Title} \${Subject;s/^/#/;s/, / #/g}.%e" \
  "[email protected]"

This probably has bugs, but it seems to be a decent start.

It was great to find out that I can pass Perl expressions to exiftool to modify the field value. I figured out that I could combine that with the /e flag for executable replacements so that I could generate a string of N+1 characters. While tinkering with the code, I accidentally used \1 instead of \$1 in my Org Mode block. That made a number such as 5 turn up as a scalar with a six-digit value, so I ended up with a really long line of #####... in my Org file. Emacs was definitely not happy. I ended up opening my Org file in vi so that I could delete the offending line. Anyway, I managed to recover from that and figure out what I needed to put in. Yay!

It would be pretty neat to have some kind of inotify thing watching my NAS inbox and processing files accordingly. In the meantime, I don’t mind running another script. We’ll see how this goes!