#!/bin/sh # screens.sh: v46 2007/09/30 KELEMEN Peter # Generate screencaps for a given videostream in the background. # Copyright 2007 KELEMEN Peter # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. debug () { if [ -n "$DEBUG" ]; then echo "$@" fi } set -a SELF=${0##*/} VERSION=$( sed -nr '2{s/^[^:]+: ([^ ]+) .*$/\1/p;q}' $0 ) debug $VERSION W=1280 # destination image width H=1024 # destination image height SW=200 # scaled width (one tile in screens) SKIP=17 # skip how many seconds at beginning of video START=1 # border size (around tiles) in pixels DELTA=1 # gap size (between tiles) in pixels SCREENSDIR=Screens if [ -z "$1" ]; then echo "Usage: $SELF [-slow] " exit 127 fi MPLAYEROPTS="-benchmark" LOOP=1 while [ $LOOP != 0 ]; do case $1 in -slow) debug "option -slow seen" shift; MPLAYEROPTS="" ;; --) debug "option -- seen" shift; LOOP=0; ;; *) LOOP=0; ;; esac done if [ ! -r "$1" ]; then echo "E: $1 is not readable." exit 126 fi FULLPATH=$( readlink -f $1 ) BASENAME=${FULLPATH##*/} DIRNAME=${FULLPATH%/*} if [ -z "$DIRNAME" ]; then DIRNAME=. fi FILENAME=${BASENAME%.*} FILEEXT=${BASENAME##*.} TEMPLATE="${SELF}.XXXXXXXXX" TEMPFILE=$( mktemp -p "$DIRNAME" $TEMPLATE ) ERROR=$? if [ $ERROR != 0 ]; then echo "E: Error creating temporary file $TEMPFILE ($ERROR), aborting." exit $ERROR fi WORKDIR=${TEMPFILE}.$$ mkdir $WORKDIR ERROR=$? if [ $ERROR != 0 ]; then echo "E: Error creating working directory $WORKDIR ($ERROR), aborting." echo $ERROR fi debug "WORKDIR=$WORKDIR" cd $WORKDIR # attempt to identify video stream mplayer -frames 1 -ao null -vo null -identify "$FULLPATH" 2>/dev/null | sed -nr 's/^(ID_[^=]+)=(.*)$/\1="\2"/p' > $TEMPFILE ERROR=$? . $TEMPFILE debug "TEMPFILE=$TEMPFILE" if [ -z "$ID_LENGTH" ]; then echo "Ooops, couldn't -identify $FULLPATH ($ERROR)" exit 125 fi # scaled height (one tile in screens) SH=$(($SW*$ID_VIDEO_HEIGHT/$ID_VIDEO_WIDTH)) debug "original: ${ID_VIDEO_WIDTH}x${ID_VIDEO_HEIGHT}" debug " scaled: ${SW}x${SH}" debug " WxH: ${W}x${H}" XTILES=$(( $W/$SW )) # how many screens per column YTILES=$(( $H/$SH )) # how many screens per row # if tiles+gaps+borders are larger than target width, # take one column away if [ $(( $SW * $XTILES + ($XTILES-1)*$DELTA + 2*$START)) -gt $W ]; then XTILES=$(( $XTILES-1 )) debug "too wide, columns set to $XTILES" fi # if tiles+gaps+borders are higher than target height, # take one row away if [ $(( $SH * $YTILES + ($YTILES-1)*$DELTA + 2*$START)) -gt $H ]; then YTILES=$(( $YTILES-1 )) debug "too high, rows set to $YTILES" fi NUMTILES=$(( $XTILES * $YTILES )) debug " tiles: ${XTILES}x${YTILES} (=${NUMTILES})" FPS=${ID_VIDEO_FPS%.*} LENGTH=${ID_LENGTH%.*} FRAMES=$(( $FPS * ($LENGTH-$SKIP) )) FRAMESTEP=$(( $FRAMES / $NUMTILES )) if [ $FRAMESTEP -lt $FPS ]; then echo "ABORT: FRAMESTEP < FPS! ($FRAMESTEP < $FPS)" exit 100 fi if [ $XTILES -lt 2 -o $YTILES -lt 2 ]; then echo "ABORT: too big tiles or too small target" exit 100 fi MPLAYEROPTS="$MPLAYEROPTS -quiet -nosound -vo jpeg -ss $SKIP -vf framestep=${FRAMESTEP},scale=${SW}:-3,tile=${XTILES}:${YTILES}:-1:${START}:${DELTA}" CMD="nice -n 19 mplayer $MPLAYEROPTS" debug $CMD if [ -z "$DRYRUN" ]; then if [ -z "$DEBUG" ]; then $CMD "$FULLPATH" >/dev/null 2>&1 else $CMD "$FULLPATH" fi mkdir -p "${DIRNAME}/${SCREENSDIR}" mv 0*1.jpg "$DIRNAME/$SCREENSDIR/$FILENAME.jpg" rm -f $TEMPFILE rm -rf $WORKDIR fi cd $OLDPWD # vim: set tw=0 nowrap number: # End of file.