Skip to content

Instantly share code, notes, and snippets.

@alexfouche
Last active January 4, 2020 17:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexfouche/1923696 to your computer and use it in GitHub Desktop.
Save alexfouche/1923696 to your computer and use it in GitHub Desktop.
Search all subfolders for photos/videos to rename from their Exif data. Also compress videos to 720height 3000kbps x264 mp4
#!/usr/bin/env bash
#set -x
# Search all subfolders for photos/videos to rename from their Exif data.
# Also compress videos to 720height 3000kbps x264 mp4 (which is roughly divide size by ten from my camera .mov files)
# https://gist.github.com/1923696
where=$1
test -z "$where" && where='.'
jhead_cmd='jhead -autorot -exonly -ft -nf%Y_%m_%d-%H_%M_%S -norot {}'
find "$where" -type f -name "PICT*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "pict*.jp*" -exec $jhead_cmd \;
find "$where" -type f -name "DSC*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "dsc*.jp*" -exec $jhead_cmd \;
find "$where" -type f -name "SDC*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "sdc*.jp*" -exec $jhead_cmd \;
find "$where" -type f -name "IMAG*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "IMAG*.jp*" -exec $jhead_cmd \;
find "$where" -type f -name "imag*.jp*" -exec $jhead_cmd \;
find "$where" -type f -name "100*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "100*.jp*" -exec $jhead_cmd \;
find "$where" -type f -name "IMG_*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "IMG_*.jp*" -exec $jhead_cmd \;
find "$where" -type f -name "P*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "ST*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "photo*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "photo*.jp*" -exec $jhead_cmd \;
find "$where" -type f -name "0*.JP*" -exec $jhead_cmd \;
find "$where" -type f -name "PANO_*.jp*" -exec $jhead_cmd \;
# find "$where" -type f -name "*.jp*" -exec jhead -autorot -exonly -ft -nf%Y_%m_%d-%H_%M_%S -norot {} \;
# find "$where" -type f -name "*.JP*" -exec jhead -autorot -exonly -ft -nf%Y_%m_%d-%H_%M_%S -norot {} \;
# find "$where" -type f -name "*.jp*" -exec jhead -da2007:12:23-2008:01:02 -autorot -exonly -ft -nf%Y_%m_%d-%H_%M_%S -norot {} \;
# find "$where" -type f -name "*.jp*" -exec jhead -ta-1:20 -autorot -exonly -ft -nf%Y_%m_%d-%H_%M_%S -norot {} \;
# jhead -da2007:12:26-2005:01:02 -autorot -exonly -ft -nf%Y_%m_%d-%H_%M_%S -norot *
# jhead -ta0:01:01 -autorot -exonly -ft -nf%Y_%m_%d-%H_%M_%S -norot *
# jhead -te orig pano
# Now do png:
for png_name in `find "$where" -type f -a \( -name "IMG_*.PNG" -o -name "IMG_*.png" \) `; do
new_png_name=$( perl -e " (\$sec,\$min,\$hour,\$mday,\$mon,\$year) = localtime((stat '$png_name')[9]); printf( '%04d_%02d_%02d-%02d_%02d_%02d.png', \$year+1900,\$mon+1,\$mday, \$hour,\$min,\$sec); " )
d=`dirname $png_name`
mv -v "$png_name" "$d/$new_png_name"
done
# # Now do simple renames for Android phone photos
# for f in *; do
# mv -v "$f" `echo "$f" |ruby -ne "puts \\$_.sub(/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2})\.([0-9]{2})\.([0-9]{2})/, '\1_\2_\3-\4_\5_\6')"`
# done
# Now do videos:
for videoname in `find "$where" -type f -a \( -name "MVI_*.MOV" -o -name "MDG_*.MOV" -o -name "IMG_*.MOV" -o -name "img_*.mov" -o -name "100_*.MOV" -o -name "100_*.mov" -o -name "VID_*.mp4" \) `; do
filename=$(basename "$videoname")
extension="${filename##*.}"
filename="${filename%.*}"
extension=`echo $extension |tr '[:upper:]' '[:lower:]'`
new_videoname=$( perl -e " (\$sec,\$min,\$hour,\$mday,\$mon,\$year) = localtime((stat '$videoname')[9]); printf( '%04d_%02d_%02d-%02d_%02d_%02d.$extension', \$year+1900,\$mon+1,\$mday, \$hour,\$min,\$sec); " )
d=`dirname $videoname`
mv -v "$videoname" "$d/$new_videoname"
done
# Now do voice
for videoname in `find "$where" -type f -a \( -name "VOX*.WAV" -o -name "vox*.wav" \) `; do
new_videoname=$( perl -e " (\$sec,\$min,\$hour,\$mday,\$mon,\$year) = localtime((stat '$videoname')[9]); printf( '%04d_%02d_%02d-%02d_%02d_%02d', \$year+1900,\$mon+1,\$mday, \$hour,\$min,\$sec); " )
d=`dirname $videoname`
f=`basename $videoname`
mv -v "$videoname" "$d/$new_videoname-$f"
done
# Compress videos to 720height 3000kbps x264 mp4
for videoname in `find "$where" -type f -name "*.mov"`; do
test ! -e "${videoname}.mp4" && nice -n 20 /Applications/HandBrakeCLI -i "$videoname" -o "${videoname}.mp4" -e x264 -b 3000 -2 -T -E ca_aac -B 160 -4 -Y 720 --loose-anamorphic -m
touch -m -r "${videoname}" "${videoname}.mp4"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment