#!/bin/bash
# Version : 0.1
#
# Licence :  GPL
#
# 2004/01/30   Alex Ferrer <alex@ftconsult.com>
#
#Script : Script to create an image page of thumbnails from a bunch of movie files
# Requires 
#                  mplayer
#                  imagemagick montage
#
#
#
# input  : a file pattern 
# output : a .jpg file with thumbnails for such videos. 
#
# take file pattern, extract filename
# create png's from mplayer
# rename 0000001 to filename
# when done use ImageMagik to generate a page
#
# Check that Mplayer & montage are installed and on the path.
for exe in mplayer montage ; do
	if [ -z "`which $exe`" ]; then
		echo "ERROR: $exe must be in your path $PATH!"
		exit 1
	fi
done
################################################################################
#
# Config section, set according to your pereferences 
TMPDIR="."                                 # path to directory for creating temporary files, recommended 1 meg space
THUMBW=144                            # width of the raw thumbnail
THUMBH=96                              # heigth of th eraw thumbnail
COLS=7                                     # Number of columns per page
ROWS=13                                  # Number of rows per page
SOURCE="./"                               #  path to directory with movie files
FTYPE="MOV"                           #  video file format 
INAME="moviethumbs" # default name for output image
###########################################
usage() {
	echo
	echo "------------------------------------------------------------------------------------------------------"
	echo "mov2thumb <source path>  <file ext mov|avi|mpeg|other> <imagename>"
	echo "   default source = ."
	echo "   default file ext = mov"
	echo "   default iamge name = moviethumbs.jpg"
	echo 
	echo "  Usage:   move2thumb /your/path/ mov mymovie"
	echo "    Generates a thumbnail image of the 1st frame of every *.mov file on the directory /your/path/ and merges them" 
	echo "       into a single .jpg image of COLS X ROWS thumbs per page."
	echo "  mov2thumb by Aferrer alex@ftconsult.com"
	echo "-------------------------------------------------------------------------------------------------------"
}
# test to make sure parameters where entered
SOURCE=$1
FTYPE=$2
echo "Runing: mov2thumb $SOURCE $FTYPE $INAME"
if [ $# -le 1 ]; then
   echo "mov2thumb error 1: Not enough parameters!"
   usage 
   exit 1
fi
#Test for iname parameter , or leave as default
if [ $# -ge 3 ]; then
    INAME=$3
fi 

# create a brand new temporary image directory.
rmdir  $TMPDIR/tempimages
mkdir $TMPDIR/tempimages

#traverse thru the filetype on the directory creating thumbnails using mplayer

for X in $SOURCE/*$FTYPE 
do  
  new=${X%.$FTYPE}                                                                    # remove the file extencion
  new=${new/$SOURCE/}                                                           # remove the file path
# use mplayer to get 1 png frame from the given file
  mplayer -frames 1 -nosound -vo png outdir=./:quality=75  $X                  
  mv  $TMPDIR/00000001.png  $TMPDIR/tempimages/$new.png                 # rename the file
  rm -f $TMPDIR/00000002.png                                                                     # mplayer -nosound creates a second png, so get rid of it too
 done

# geometry is the width and height of the image
# tile is how many per row and column
# any excess goes to next file
echo "Building image, please wait ...."
DLINE1=$TMPDIR/tempimages/'*'.png 
montage  -geometry '144x96+2+2>' -label '%f' -tile ${COLS}x${ROWS}  $DLINE1  $INAME.jpg

# remove the temp png's 
rm -f $TMPDIR/tempimages/*.png
rmdir  $TMPDIR/tempimages
echo "Thumbnail Image $INAME  Done!"
# done 


