#! /bin/bashmain() { # Find length ID_LENGTH=$($MPLAYER -identify -frames 0 -vo null -ao null \ "$MOVIEFILE" | grep '^ID_LENGTH' | sed -e 's:.*=\([0-9]*\).*:\1:') # Calculate interval let "INTERVAL=$ID_LENGTH / $NUMSHOTS" # Setup tmp-dir local tmpdir_existed if [ -d "$TMPDIR" ]; then tmpdir_existed=1 else tmpdir_existed=0 mkdir -p "$TMPDIR" fi POS=$INTERVAL NUM=1 local filelist="" while [ $POS -lt $ID_LENGTH ]; do echo "Grabbing screenshot #${NUM}" # Sometimes the first frame is grabbed before the seek # operation finishes, so grab 2 frames and use the second one $MPLAYER -ao null -ss $POS -frames 2 -vo jpeg "$MOVIEFILE" \ &>/dev/null mv 00000002.jpg "$TMPDIR"/$NUM.jpg filelist="$filelist ${TMPDIR}/${NUM}.jpg" let "POS += $INTERVAL" let "NUM += 1" done rm -f 00000001.jpg echo "Creating montage.." let "rows = $NUMSHOTS / 2" $MONTAGE -geometry 200x -tile 2x$rows $filelist "${OUTFILE}" echo "Wrote ${OUTFILE}" [ $tmpdir_existed -eq 0 ] && rm -fr "$TMPDIR"}sanity_checks() { if [ ! -x "$MPLAYER" ]; then echo "Required program 'mplayer' not found." exit 1 fi if [ ! -x "$MONTAGE" ]; then echo "Required program 'montage' not found. Make sure you have" echo "imagemagick installed in your system." exit 1 fi if [ ! -f "$MOVIEFILE" ]; then echo "Movie file '${MOVIEFILE}' not found." exit 1 fi}usage() { echo echo "Usage: $(basename $0) [OPTIONS] MOVIEFILE" echo "Creates a group of screenshots from a movie file using mplayer" echo "and imagemagick." echo echo " -d DIR Use DIR as a temporary directory (defaults to" echo " /tmp/screenshots)" echo " -n NUM Number of images to include in the final montage" echo " -o FILE Output file" exit 1}while getopts "d:n:o:" option; do case $option in d ) TMPDIR="$OPTARG" ;; n ) NUMSHOTS="$OPTARG" ;; o ) OUTFILE="$OPTARG" ;; * ) usage ;; esacdoneshift $(($OPTIND - 1))[ $# -lt 1 ] && usageMONTAGE=$(which montage)MOVIEFILE="$1"MPLAYER=$(which mplayer)NUMSHOTS=${NUMSHOTS:-20}OUTFILE=${OUTFILE:-$(basename ${MOVIEFILE%.*}_screens.jpg)}TMPDIR=${TMPDIR:-/tmp/screenshots}sanity_checksmain