[mythtv-users] Updated screenshooter.sh file

Sarah Katherine Hayes aleph.wintermute at ntlworld.com
Wed Aug 15 15:12:50 UTC 2007


Greetings,
Fixed it.
Well, here it is for giggles.  I'm sure I've got something the wrong way 
round, but it appears to work on my video collection.
You'll need the mplayer.exp file from the URL mentioned in the script 
itself. 

Basically it'll now work across multiple frontends, talk to the backend, 
only pull up 1 location for the screenshots as i assumed (logically) 
that if you have a huge backend it'll be storing the screenshots/IMDB 
posters and the like. 
Next stage is to also have it pull the video store out of the DB and 
just trundle through itself without needing the find command.
Comments, critiques, flames...


Sarah


#!/bin/bash

# Written by Bob Igo from the MythTV Store at http://MythiC.TV
# with contributions from TJC
# Email: bob at stormlogic.com
#
# If you run into problems with this script, please send me email

# This is alpha code to auto-generate thumbnails for previews in MythVideo.
# It won't currently work on some filenames that have spaces in them.
# It's surely just a matter of escaping or quoting, but I have yet to find
# the right incantation.

# example usage:
# find -L /myth/video -wholename '*.covers' -prune -o -name '*.resume' 
-o -type f -exec screenshooter.sh -v {} \;

# limitations:
# --
# In an MBE/SBE/FE setup this might get the settings for the wrong 
machine...
# The script has no AI to know if a grabbed frame is useful to identify 
the video, only that it was able to grab it.
# Doesn't clean up after itself if videos are deleted, though MythTV may 
do this on its own.
# Surely more limitations I can't think of because it's my baby :)


# Overhauled by Sarah Hayes, 2007.
# Added:
# Backend DB hostnames
# Only return 1 screen shot directory, assumes multiple FE's go here.
# Check the file is a known extension.
# Check the file is in the Database.
# Optional: Import the file with a sensible set of defaults if it isn't.
# Optional: verbose mode.

while getopts "v:s:ch" FLAG ; do
    case "$FLAG" in
    h) echo "USAGE:"
    echo `basename $0` " -v PATHNAME [-s SECONDS] [-c]"
    echo "-v: pathname to video"
    echo "-s: number of seconds to skip before capturing (defaults to 270)"
    echo "-c: clobber any previous screenshot found for this video"
        echo "-h: hostname or IP address of the backend"
        echo "-f: hostname of IP adress of a frontend to get the 
settings for"
        echo "-u: MySQL user to access the database"
        echo "-p: MySQL user's password"
    echo
    echo "EXAMPLE: $0 -v /myth/video/HDTV/shuttle.mpg -c -s 30"
    exit 3
    ;;
    v) VIDEO_PATHNAME="$OPTARG";;
    s) SKIPAHEAD="$OPTARG";;
    c) CLOBBER="$OPTARG";;
        h) BACKEND_HOST="$OPTARG";;
        u) DBUSERNAME="$OPTARG";;
        p) DBPASSWORD="$OPTARG";;
        d) VERBOSE="$OPTARG";;
        
    esac
done

# Declaring Variables here and assigning sensible defaults.
# SKIPAHEAD is the number of seconds to skip ahead before starting the 
frame capture.
# Set it to an arbitrary value if none is specified.
SKIPAHEAD=${SKIPAHEAD:-"270"}
BACKEND_HOSTNAME=${BACKEND_HOSTNAME:-"madcat"}
DBUSERNAME=${DBUSERNAME:-"mythtv"}
DBPASSWORD=${DBPASSWORD:-"mythtv"}
VERBOSE=${VERBOSE:-"0"}  
# Defaults to quiet.
# Unless otherwise told, do not clobber existing cover files.
CLOBBER=${CLOBBER:-0}

VIDEO_CAPTURE_HOME=$(mysql -u $DBUSERNAME --password=$DBPASSWORD -h 
$BACKEND_HOSTNAME mythconverg -sNBe "select data from settings where 
value='VideoArtworkDir' limit 1")
if [ ! -d "$VIDEO_CAPTURE_HOME" ] ; then
    echo "Directory $VIDEO_CAPTURE_HOME does not exist, nowhere to put 
the screen shot!"
    exit 1
fi

VIDEO_FILENAME=$(basename "$VIDEO_PATHNAME")
VIDEO_EXTENSION=${VIDEO_FILENAME##*.}
# Since we cron'd lets first make sure the validity of the file  
EXCHECK=$(mysql -u $DBUSERNAME --password=$DBPASSWORD -h 
$BACKEND_HOSTNAME mythconverg -sNBe "select f_ignore from videotypes 
where extension=\"$VIDEO_EXTENSION\";")

#excheck returns blank, it found nothing.  
if [ "$EXCHECK" == "" ]; then
  if [ "$VERBOSE" == "1" ]; then
    echo "$VIDEO_EXTENSION does not appear to be a valid media file, 
skipping."
  fi
  exit 1
else
# It is valid, but should we ignore it.  If so then excheck will equal 1.
  if [ "EXCHECK" == "1" ]; then
    if [ "$VERBOSE" == "1" ]; then
      echo "$VIDEO_EXTENSION is set to ignore."
    fi
   exit 1
  fi
# It is valid, it's not set to ignore.  
   if [ "$VERBOSE" == "1" ]; then
     echo "$VIDEO_EXTENSION appears in the Database, checking further."
   fi
  EXCHECK=$(mysql -u $DBUSERNAME --password=$DBPASSWORD -h 
$BACKEND_HOSTNAME mythconverg -sNBe "select title from videometadata 
where filename=\"$VIDEO_PATHNAME\";")
#Right, the file is supposed to be playable.  Has it been imported to 
the Db yet?
  if [ "$EXCHECK" == "" ] ; then
    if [ "$VERBOSE" == "1" ]; then
     echo "$VIDEO_FILENAME does not exist in the database."
    fi  
# If you decide you want the system to 'auto import' the video then 
comment out
# the exit line and uncomment the rest of it.  Bewarned, this is sucky 
SQL at
# the best but will give sensible defaults.
#
   exit 1
#    if [ "$VERBOSE" == "1" ]; then
#     echo "Importing $VIDEO_FILENAME in to database."
#    fi
#  mysql -u $DBUSERNAME --password=$DBPASSWORD -h $BACKEND_HOSTNAME 
mythconverg -sNBe "insert into videometadata (intid, title, director, 
plot, rating, inetref, year, userrating, length, showlevel, filename, 
coverfile, childid, browse, playcommand, category) values (' ', 
'$VIDEO_FILENAME', 'Unknown', 'Unknown', 'NR', '00000000', 1895, 0.0, 0, 
1, '$VIDEO_PATHNAME', 'No Cover', -1, 1, ' ', 0);"
 
  fi
fi

  if [ "$CLOBBER" -eq 0 ]; then
      # Since we're not clobbering, first check to see if this video 
already has a coverfile entry in MySQL:
      SQL_CMD="select coverfile from videometadata where 
filename=\"$VIDEO_PATHNAME\";"
      CURRENT_COVERFILE=`mysql -u $DBUSERNAME --password=$DBPASSWORD -h 
$BACKEND_HOSTNAME mythconverg -B -e "$SQL_CMD" | tail -1`

      if [[ "$CURRENT_COVERFILE" != "" ]] && [[ "$CURRENT_COVERFILE" != 
"No Cover" ]]; then
        # there's already a cover file for this video
        if [ "$VERBOSE" == "1" ]; then
         echo "$VIDEO_FILENAME has cover file, skipping."
      fi
    exit 2
      fi
  fi
#Having blah.avi.png just looks scrappy.  Swap the extension for png, 
should work assuming the extension only appears ONCE! You've been warned.
VIDEO_CAPTURE_PATHNAME="$VIDEO_CAPTURE_HOME/${VIDEO_FILENAME/$VIDEO_EXTENSION/png}"

# The video we're processing may be shorter than SKIPAHEAD seconds.
# Keep trying to capture until we find a SKIPAHEAD value within the 
length of the video.
# Give up if we reach 0 seconds.
SHOTFILE=""

while [ -z "$SHOTFILE" ]; do
    SHOTFILE=`/usr/local/bin/mplayer-screenshot.exp "$VIDEO_PATHNAME" 
$SKIPAHEAD | grep "screenshot '" | awk -F"'" '{print $2}'`
    SKIPAHEAD=$(expr $SKIPAHEAD / 2)
    if [ "$SKIPAHEAD" -eq 0 ]; then
    break;
    fi
done

if [ -n "$SHOTFILE" ]; then
    # Now, the video_capture is taken, and the name of the shot is in 
$SHOTFILE
    # Rename it and move it to the place where video_captures live.
    /bin/mv -f "$SHOTFILE" "$VIDEO_CAPTURE_PATHNAME"
    chown mythtv: "$VIDEO_CAPTURE_PATHNAME"
    # put this into videometadatatable

    # Pre-escape any single or double quotes for the SQL command.

    VIDEO_CAPTURE_PATHNAME=`echo $VIDEO_CAPTURE_PATHNAME | sed -e 
"s/'/\\\'/g" -e 's/"/\\\"/g' `
    VIDEO_PATHNAME=`echo $VIDEO_PATHNAME | sed -e "s/'/\\\'/g" -e 
's/"/\\\"/g' `
    SQL_CMD="update videometadata set 
coverfile=\"$VIDEO_CAPTURE_PATHNAME\" where filename=\"$VIDEO_PATHNAME\";"

    mysql -u $DBUSERNAME --password=$DBPASSWORD -h $BACKEND_HOSTNAME 
mythconverg -e "$SQL_CMD"
else
    echo "No image could be captured from $VIDEO_PATHNAME"
    exit 1
fi


More information about the mythtv-users mailing list