Length Script

From MythTV Official Wiki
Revision as of 19:24, 2 January 2008 by Iamlindoro (talk | contribs)

Jump to: navigation, search

This script will generate accurate lengths for all movies in your MythVideo directory. Even with IMDB, many movies come back with lengths of zero minutes. Personally, I also have many television episodes archived in MythVideo. In an attempt to make my metadata more accurate, I wrote this script.

Thanks to siXy in #mythtv-users for helping me simplify this script.

#!/bin/bash
#
#  Script to automate setting of video lengths in mythtv.
#
#  Searches for all MythVideo files with length of "0."
#  Generates a list, and pipes each file through an
#  mplayer -identify "filename."  Inserts each length
#  back into the database.  This script needs slight
#  babysitting as, if mplayer cannot play the file
#  properly, it will go into a loop.  In my MythVideo
#  of ~500 files, I had to set lengths four times to
#  get the script to complete.  These files were all
#  .m2ts files from Blu-Ray discs.  If your files are
#  all relatively standard, this should be unnecessary.
#
#  Define the next five items.

# Name of mythtv database.  Default is "mythconverg."
DBNAME=mythconverg

# Hostname or IP Address of MySQL Server.
DBHOST=localhost

# MySQL user name
DBUSER=mythtv

# MySQL Password
DBPASSWORD=mythtv

# Where do your movies live? (Root MythVideo Directory)
MOVIEHOME=/movies


echo "USE $DBNAME; SELECT filename FROM \`videometadata\` WHERE \`length\` = 0;" > file.sql
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < file.sql > results.sql
ed -s results.sql <<< $'1d\nw'

while read FILENAME; do
MOVIE=`cat results.sql`
TOTALSECONDS=`mplayer -vo null -ao null -frames 0 -identify "$MOVIE" 2>/dev/null | grep "ID_LENGTH" | sed -e 's/ID_LENGTH=//' |awk '{print int($1+0.5)}'`
LENGTHCALC=`expr $TOTALSECONDS "/" 60`
LENGTH=`echo $LENGTHCALC | awk '{print int($1+0.5)}'`
echo "USE $DBNAME

SELECT @var_MOVIEID:=\`title\` FROM \`videometadata\` WHERE \`length\` = 0 LIMIT 1;
SELECT @var_MOVIEID;

UPDATE \`videometadata\` SET \`length\` = $LENGTH WHERE \`title\` = @var_MOVIEID LIMIT 1 ;" > lengthupdate.sql
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < lengthupdate.sql
done < results.sql

# Cleanup!
rm results.sql file.sql lengthupdate.sql