Difference between revisions of "Length Script"

From MythTV Official Wiki
Jump to: navigation, search
Line 19: Line 19:
 
#  all relatively standard, this should be unnecessary.
 
#  all relatively standard, this should be unnecessary.
 
#
 
#
#  Define the next five items.
+
#  Define the next four items.
  
# Name of mythtv database.  Default is "mythconverg."
+
# MythTV MySQL Database Name.  Default is "mythconverg."
 
DBNAME=mythconverg
 
DBNAME=mythconverg
  
Line 28: Line 28:
  
 
# MySQL user name
 
# MySQL user name
DBUSER=mythtv
+
DBUSER=root
  
 
# MySQL Password
 
# MySQL Password
DBPASSWORD=mythtv
+
DBPASSWORD=soulfire
  
 
# Where do your movies live? (Root MythVideo Directory)
 
# Where do your movies live? (Root MythVideo Directory)
Line 37: Line 37:
  
  
echo "USE $DBNAME; SELECT filename FROM \`videometadata\` WHERE \`length\` = 0;" > file.sql
+
##  End of definitions
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < file.sql > results.sql
 
ed -s results.sql <<< $'1d\nw'
 
  
while read FILENAME; do
+
until [ "$count" = "0" ]
MOVIE=`cat results.sql`
+
do
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)}'`
+
 
 +
##find out how many movies to process
 +
echo "USE $DBNAME; SELECT filename FROM \`videometadata\` WHERE \`length\` = 0;"  > lengthlist.sql
 +
 
 +
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < lengthlist.sql > sqllist.out
 +
ed -s sqllist.out <<< $'1d\nw'
 +
count=`wc -l sqllist.out | awk '{print $1}'`
 +
 
 +
 
 +
##get a  movie to process
 +
echo "USE mythconverg
 +
SELECT filename FROM \`videometadata\` WHERE \`length\` = 0 LIMIT 1;
 +
SELECT title FROM \`videometadata\` WHERE \`length\` = 0 LIMIT 1;" > lengthfind.sql
 +
 
 +
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < lengthfind.sql > sql.out
 +
MOVIE="`cat sql.out | grep -v title | grep -v filename | grep $MOVIEHOME`"
 +
MOVIEID="`cat sql.out | grep -v filename | grep -v title | grep -v $MOVIEHOME`"
 +
 
 +
##Calculate the length
 +
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`
 
LENGTHCALC=`expr $TOTALSECONDS "/" 60`
 
LENGTH=`echo $LENGTHCALC | awk '{print int($1+0.5)}'`
 
LENGTH=`echo $LENGTHCALC | awk '{print int($1+0.5)}'`
Line 52: Line 69:
  
 
UPDATE \`videometadata\` SET \`length\` = $LENGTH WHERE \`title\` = @var_MOVIEID LIMIT 1 ;" > lengthupdate.sql
 
UPDATE \`videometadata\` SET \`length\` = $LENGTH WHERE \`title\` = @var_MOVIEID LIMIT 1 ;" > lengthupdate.sql
 +
 +
  ##update the database
 
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < lengthupdate.sql
 
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < lengthupdate.sql
done < results.sql
 
  
# Cleanup!
+
done
rm results.sql file.sql lengthupdate.sql
 
 
</pre>
 
</pre>

Revision as of 21:22, 2 January 2008

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 four items.

# MythTV MySQL Database Name.  Default is "mythconverg."
DBNAME=mythconverg

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

# MySQL user name
DBUSER=root

# MySQL Password
DBPASSWORD=soulfire

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


##  End of definitions

until [ "$count" = "0" ]
do

##find out how many movies to process
echo "USE $DBNAME; SELECT filename FROM \`videometadata\` WHERE \`length\` = 0;"  > lengthlist.sql

mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < lengthlist.sql > sqllist.out
ed -s sqllist.out <<< $'1d\nw'
count=`wc -l sqllist.out | awk '{print $1}'`


##get a  movie to process
echo "USE mythconverg
SELECT filename FROM \`videometadata\` WHERE \`length\` = 0 LIMIT 1;
SELECT title FROM \`videometadata\` WHERE \`length\` = 0 LIMIT 1;" > lengthfind.sql

mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < lengthfind.sql > sql.out
MOVIE="`cat sql.out | grep -v title | grep -v filename | grep $MOVIEHOME`"
MOVIEID="`cat sql.out | grep -v filename | grep -v title | grep -v $MOVIEHOME`"

##Calculate the length
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

  ##update the database
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < lengthupdate.sql

done