#!/bin/sh
# this script will loop through all videos in videometadata,
# capture a screenshot for each, move each screenshot to the
# COVERPATH dir, and update each row in videometadata with
# the appropriate information. Only rows with coverfile=
# 'No Cover' are processed. A percentage of the length
# of each video is used for the capture position to account
# for clips of widely varying lengths.
# only run as mythtv so writing to /myth is allowed
if [ $(whoami) != 'root' ]; then
echo "This script must run under user mythtv."
exit 1
fi
# intialize variables
DB='mythconverg'
USER='root'
OUTPATH='/home/james/media/.mythtv/MythVideo/tmp'
COVERPATH='/myth/video/.covers'
FRAME='00000002.jpg'
SKIPOFFSET='35'
# query videos with no cover image
VIDEOS="mysql $DB -u $USER \
-e \"SELECT intid,filename FROM videometadata \
WHERE coverfile='No Cover';\" -BN"
sh -c "$VIDEOS"|while read line
#echo "$FILE"|while read line
do
ID=`echo "$line" |cut -f 1`
FILE=`echo "$line" |cut -f 2`
echo "processing $FILE with primary key id $ID"
# if video is mpeg, use ffmpeg12 codec to avoid pixelation
if echo "$FILE" | egrep ".*\.mp[e]*g" ; then
VC='-vc ffmpeg12'
else
VC=''
#fi
# use mplayer to get length of video in seconds
LENGTH=`mplayer -vo null -nosound -identify \
-vc dummy "$FILE" | \
egrep "^(ID_LENGTH=)" | cut -d '=' -f 2`
if [ -z $length ]; then
echo "mplayer was unable to get the $FILE length."
continue
fi
SKIP=`echo $LENGTH*$SKIPOFFSET/100|bc -l`
echo "calcuated skip seconds: $SKIP"
# command to get jpeg from video
mplayer -noframedrop -ss $SKIP -vo jpeg:quality=50:outdir=$OUTPATH -frames 2 $VC -nosound "$FILE" &
wait
echo mv $OUTPATH/$FRAME $COVERPATH/intid.$ID.jpg
# move jpeg from outpath to coverpath
if ! mv -f "$OUTPATH/$FRAME" "$COVERPATH/intid.$ID.jpg" ; then
echo "Move failed. Will not update videometadata table..."
rm -f "$OUTPATH/$FRAME"
else
# SQL to update table with cover image path
UPDATE=`mysql $DB -u $USER
-e "UPDATE videometadata SET
coverfile='$COVERPATH/intid.$ID.jpg'
WHERE intid=$ID LIMIT 1 ;" -BN`
fi
done
exit 0