Skip to content

Instantly share code, notes, and snippets.

@mandric
Last active July 1, 2016 12:39
Show Gist options
  • Save mandric/5c6d5c62ba21ea1913df to your computer and use it in GitHub Desktop.
Save mandric/5c6d5c62ba21ea1913df to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
EXTS_PICS_RE='(\.png$|\.jpg$|\.jpeg$)'
EXTS_VIDS_RE='(\.mp4$|\.mov$|\.mpg$|\.m4v$)'
EXTS_AUDIO_RE='(\.mp3$|\.opus$|\.m4a$)'
ALBUM_SUBTITLE="${ALBUM_SUBTITLE-`date '+%B %Y'`}"
ALBUM_SORT="${ALBUM_SORT-sort}"
ALBUM_POSTER_WIDTH=${ALBUM_POSTER_WIDTH-640}
ALBUM_TITLE="${ALBUM_TITLE-`basename "$PWD"`}"
if [ -n "$DEBUG" ]; then
set -x
fi
_usage () {
echo 'Create `originals` directory and link or copy files there, then run `sh make-album.sh`.'
exit 1
}
# echo msg to stdout and exit with zero value
_exit_clean () {
local msg="$1"
echo "Done. $msg"
exit 0
}
_exit_fail () {
local msg="$1"
local code="$2"
echo "Failed. $msg" 1>&2
[ -n "$code" ] && exit "$code"
exit 1
}
_is_darwin () {
return uname -v | grep -i darwin > /dev/null
}
_is_linux () {
return uname -v | grep -i linux > /dev/null
}
_require_util () {
type "$1" > /dev/null 2>&1 || which "$1" > /dev/null 2>&1 ||
_exit_fail "you do not have '$1' installed, which I need to $2"
}
_video_length () {
path="$1"
return ffmpeg -i "$path" 2>&1 | grep Duration | awk '{print $2}' | tr -d ,
}
_process_audio () {
local path="$1"
local filename=`basename "$1"`
cat <<EOT >> index.html || _exit_fail
<div class="box">
<div class="audio">
<p>$filename</p>
<audio controls preload="none" src="$path">
<p>Your browser does not support the audio element.</p>
</audio>
<div class="desc">
<p><a href="$path">Download</a></p>
</div>
</div>
<div class="desc"></div>
</div>
EOT
}
_process_image () {
local orig="$1"
local thumb="`echo \"$orig\" | sed s/originals/processed/`"
thumb="`echo \"$thumb\" | sed 's/\.\(.*\)/_thumb.\1/g'`"
if [ ! -f "$thumb" ]; then
_update_progress
# native osx util
#sips -Z 640 "$orig" --out "$thumb" > /dev/null || _exit_fail
convert -define jpeg:size=200x200 "$orig" \
-thumbnail 200x200^ \
-gravity center \
-extent 200x200 \
"$thumb" || _exit_fail
fi
cat <<EOT >> index.html || _exit_fail
<div class="box">
<div class="image">
<a href="$orig"><img src="$thumb"></a>
</div>
<div class="desc">
<!--p><a href="$orig">$orig</a></p//-->
</div>
</div>
EOT
}
_process_video () {
local orig="$1"
local poster="`echo \"$orig\" | sed s/originals/processed/`"
poster="`echo \"$poster\" | sed 's/\.\(.*\)/_thumb.jpg/g'`"
if [ ! -f "$poster" ]; then
_update_progress
ffmpeg -ss 00:00:05 \
-i "$orig" \
-frames:v 1 \
-f image2 \
"$poster.tmp" > /dev/null 2>&1 && \
# native osx util
#sips -Z 640 "$poster.tmp" --out "$poster" > /dev/null && \
convert -define jpeg:size=440x200 "$poster.tmp" \
-thumbnail 440x200^ \
-gravity center \
-extent 440x200 \
"$poster" && \
rm "$poster.tmp" || _exit_fail
fi
cat <<EOT >> index.html || _exit_fail
<div class="box video">
<video controls width=440 height=200 poster="$poster">
<source src="$orig" />
I'm sorry; your browser doesn't support HTML5 video.
</video>
<!--a href="$orig">Download Original</a//-->
<div class="desc">
<!--p><a href="$orig">$orig</a></p-->
</div>
</div>
EOT
}
_update_progress () {
echo -n '.'
}
_init () {
if [ ! -d originals ]; then
mkdir originals || _exit_fail
fi
if [ ! -d processed ]; then
mkdir processed || _exit_fail
fi
[ -d static ] || _exit_fail "static directory missing"
# reproduce directory structure of originals processing later.
for i in `find -L originals -type d`; do
mkdir -p "`echo \"$i\" | sed s/originals/processed/`"
done;
cat <<EOT > index.html || _exit_fail
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>$ALBUM_TITLE</title>
<link href="static/style.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>$ALBUM_TITLE</h1>
<h2>$ALBUM_SUBTITLE</h2>
EOT
}
_main () {
_init
# find files and follow symlinks
for i in `find -L originals -type f`; do
if (echo "$i" | grep -iE "$EXTS_PICS_RE" > /dev/null); then
_update_progress
_process_image "$i"
fi
if (echo "$i" | grep -iE "$EXTS_VIDS_RE" > /dev/null); then
_update_progress
_process_video "$i"
fi
if (echo "$i" | grep -iE "$EXTS_AUDIO_RE" > /dev/null); then
_update_progress
_process_audio "$i"
fi
done;
echo '<div class="footer"></div></body></html>' >> index.html
}
echo -n "Create site from files in $PWD? (y/n) "
read check
if [ $check == 'y' ]; then
_main
else
_exit_clean
fi
echo ' Done. Open index.html to view album.'
echo "Total size of this directory is `du -Lhs . | cut -f1`."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment