File:  [Local Repository] / tn / tn
Revision 1.11: download - view: text, annotated - select for diffs
Mon Nov 4 18:11:19 2013 UTC (10 years, 6 months ago) by nick
Branches: MAIN
CVS tags: HEAD
Added ability to display file sizes --showsize

#!/bin/bash
################################################################################
##
## Generate Thumbnails and an index.html in a gallery-esque fashion
## Nicholas DeClario <nick@demandred.dyndns.org>
## February 2011
## $Id: tn,v 1.11 2013/11/04 18:11:19 nick Exp $
##
##   Please use the '--man' option for details on how this works.  Running
##   with no options will fall back to defaults and should work fine for
##   most cases.  The defaults are listed below; feel free to change them.
##
################################################################################

##
## These are default options, feel free to manipulate these.
##
declare FORCE=0
HTML="index.html"
TN_SIZE=64
MAX_COL=6
FNAMES=0
DIRECTORIES=0
MISSING=0
NOVIDEO=0
FSIZE=0
TN_DIR=thumbs
TITLE="`hostname`:`pwd`"
ALL=$*
FILE_TYPES='jpg png jpeg gif mp4 mpg m4v avi mov wmv'
VERSION="$Id: tn,v 1.11 2013/11/04 18:11:19 nick Exp $"


###############################################################################
##
## help( )
## 
##    Display the basic help text
##
###############################################################################
help( ) {
        cat <<EOF
Usage:
    $0 [options]

    Options:
	   --columns	Number of columns for images (default 6)
	   --dir	Enable links for sub directories (default off)
	   --filenames	Display filename of image below thumbnail (default off)
	   --force	Force overwriting the specified html file
           --help,?     Display the basic help menu
	   --htmlfile	HTML filename to write to (default index.html)
           --man,m      Display the detailed man page
	   --missing	Only generate thumbs for missing thumbs
	   --novideo	Skip generating video thumnails
	   --showsize	Display the size of the file beneath the thumbnail
	   --size	Set the thumbnail size (default 64x64)
	   --sort	Use 'sort' to order the images within the HTML
	   --title	Title of HTML page (default 'hostname - current dir') 
	   --tndir	Thumbnail directory (default thumbs)
           --version    Display the version

EOF

        exit 1
}


###############################################################################
##
## man( )
##
##      Display the detailed man page
##
###############################################################################
man( ) {
        cat <<EOF | nroff -man
.TH template.sh 1 "`date`" 
.SH NAME
tn \- Thumbnail and HTML "gallery-esque" generator
.SH SYNOPSIS
.B tn
[ options ]
.SH DESCRIPTION
.I tn
is a script that will generate a gallery-esque HTML page.  It will create thumbnails of all your images and sort them in to a web page.  There are many options and features.
.SH OPTIONS
.IP "--columns <number of columns>"
When generating the HTML output the script will split the thumbnails up in to rows and columns.  The number of rows varies as necessary but the number of columns or items per row can be specified with this option.  The default is 6 items per row or 6 columns.
.IP "--dir"
Enabling this will allow the script to list the subdirectories that were found.  It will place this list, with proper links in the HTML file.  This is extremely handy when running this script recursivly.  The default is off for this option. See below for running recursivly.
.IP "--filenames"
This will display the filename of the image below it's thumbnail in the HTML page.  This is off by default.
.IP "--help,?"
Display the basic help menu
.IP "--htmlfile filename.html"
The output is all dumped to HTML and saved to a file, by default index.html.  The default can be changed by using this option.
.IP "--man,m"
Display the detailed man page
.IP "--size <size XxY>"
This sets the default thumbnail size to pass to Imagemagick.  The default is 64x64.
.IP "--missing"
Using this setting will trigger the script to only generate thumbnails for images that do not currently have any.
.IP "--title <title>"
The page has a default title and header.  This will statically set that header.
.IP "--tndir <directory name>"
The thumbnails can be saved in a seperate directory.  This directory name may not exist in some areas making this a very handy option.
 "--version"
Display the version
.SH "SEE ALSO"
man(1)	convert(1) sort(1)
.SH "BUGS"
.I Template.sh
is a work in progress.  Please report bugs.
.SH "COPYRIGHT NOTICE"
Copyright (c) 2006 Free Software Foundation, Inc.
.br
Copyright (c) 2011 Nicholas DeClario
.PP
Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.
.ig
Permission is granted to process this file through troff and print the
results, provided the printed document carries copying permission
notice identical to this one except for the removal of this paragraph
(this paragraph not being relevant to the printed manual).
..
.PP
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the entire
resulting derived work is distributed under the terms of a permission
notice identical to this one.
.PP
Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be stated in a translation approved
by the Foundation.
EOF
        exit 1;
}

###############################################################################
## End of shell function definitions
###############################################################################

##
##  We are going to read in our basic set of options and parse everything 
##  out.  Setting NOARGS to 1 at the top of this script will tell getOpts 
##  to call 'help' if no arguments are supplied on the command line.
##

##
## Parse through our command line arguments here
##
while [ $# -gt 0 ]
do
        arg=$1
        shift

        case $arg in
		--columns)
			MAX_COL=$1
			shift
			;;
		--dir)
			DIRECTORIES=1
			;;
		--filenames)
			FNAMES=1
			;;
		--force)
			FORCE=1
			;;
		--htmlfile)
			HTML=$1
			shift
			;;
                --man)
                        man
                        break
                        ;;
		--missing)
			MISSING=1
			;;
		--novideo)
			NOVIDEO=1
			;;
		--showsize)
			FSIZE=1
			;;
		--size)
			TN_SIZE=$1
			shift
			;;
		--sort)
			SORT=1
			;;
		--title)
			TITLE=$1
			shift
			;;
		--tndir)
			TN_DIR=$1
			shift
			;;
                --version)
                        echo "Version: $VERSION"
                        break
                        ;;
                *)
                        help
                        break
                        ;;
        esac
done

##
## Make sure imagemagick is installed
##
CONVERT=/usr/bin/convert
[ -x $CONVERT ] || \
{
	echo "ERROR: Imagemagick not found"
	exit 1
}

##
## Make sure ffmpegthumbnailer is installed
##
FFMPEGTHUMBNAILER=/usr/bin/ffmpegthumbnailer
[ -x $FFMPEGTHUMBNAILER ] || [ $NOVIDEO -eq 1 ] || \
{
	echo "ERROR: ffmpegthumbnailer required for video thumbnails"
	exit 1
}

[ -d $TN_DIR ] || mkdir $TN_DIR

[ -f $HTML ] && [ $FORCE -eq 0 ] && \
{
	echo "ERROR: $HTML exists, use --force to overwrite"
	exit 1
}

cat <<EOF>$HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>$TITLE</title>
  </head>
  <body bgcolor="#F2F2F2">
<h1>$TITLE</h1>
<table align="center" border="0" cellpadding="2">
<tr>
EOF

TIFS=$IFS
CNT=0
for i in $FILE_TYPES; do
	IFS=$(echo -en "\n\b")
	[ $SORT ] && \
		FILES=`/usr/bin/find -maxdepth 1 -type f -iname \*.$i | sort` || \
		FILES=`/usr/bin/find -maxdepth 1 -type f -iname \*.$i`
	for n in $FILES; do 
		file $n | grep -q -ie video -e media -e sequence -e movie && \
		{
			TN=`basename $n .$i`-tn.gif
			[ $MISSING -eq 1 ] && [ -f $TN_DIR/$TN ] || \
			{
				tmpfile=$(mktemp -d)
				for cap in $(seq -w 1 5 99); do 
					$FFMPEGTHUMBNAILER -s $TN_SIZE -i $n -o $tmpfile/thumb-${cap}.jpg -t ${cap}%
				done
				$CONVERT -delay 30 -loop 0 $tmpfile/thumb*.jpg $TN_DIR/$TN
				rm -rf $tmpfile
			}
		} || {
			TN=`basename $n .$i`-tn.$i;
			[ $MISSING -eq 1 ] && [ -f $TN_DIR/$TN ] || \
				$CONVERT -resize $TN_SIZE $n $TN_DIR/$TN
		}
		[ $CNT -ge $MAX_COL ] && \
		{
			echo -e "</tr>\n<tr>" >>$HTML
			CNT=0
		}
	
		[ $FNAMES -eq 0 ] || \
			IMAGE_NAME="<br/><font size=\"0\">`basename $n`</font>"
		[ $FSIZE -eq 1 ] && \
			FILE_SIZE="<br /><font size=\"0\">$( ls -sh $n | awk '{print $1}')</font>"
	
		cat <<EOF>>$HTML
<td align="center" valign="middle">
<a href="$n"><img src="$TN_DIR/$TN" alt="$n" border="0" /></a>&nbsp;
$IMAGE_NAME
$FILE_SIZE
</td>
EOF
		CNT=$(( CNT + 1 ))
	done
done

##
## Link directories if enabled
##
IFS=$(echo -en "\n\b")
[ $DIRECTORIES -eq 0 ] || \
{
	echo -e "</tr>\n<tr>\n<td colspan=\"$MAX_COL\">\n\t<ul>"
	for dir in `find . -maxdepth 1 -type d`; do
		dir=`basename $dir`
		[ "$dir" != "$TN_DIR" ] && [ "$dir" != "." ] && \
			echo -e "\t\t<li><a href=\"$dir\">$dir</a></li>"
	done
	echo -e "\t</ul>\n</td>"
} >> $HTML
IFS=$TIFS

cat <<EOF>>$HTML
</tr>
<tr>
<td colspan="$MAX_COL">
<hr/>
<center>
	<font size="0">
		Generated on <b>`date`</b>.<br/>
		Generated with command <b>'$0 $ALL'</b> by <b>`whoami`</b>.
	</font>
  <p>
    <a href="http://validator.w3.org/check?uri=referer"><img
        src="http://www.w3.org/Icons/valid-xhtml10" border="0"
        alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a>
  </p>
</center>
</td>
</tr>
</table>
</body>
</html>
EOF

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>