Difference between revisions of "Length Script"

From MythTV Official Wiki
Jump to: navigation, search
(Script for auto-generating movie lengths in MythVideo)
 
Line 1: Line 1:
 
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.
 
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.''
  
 
<pre>
 
<pre>
Line 17: Line 19:
 
#  all relatively standard, this should be unnecessary.
 
#  all relatively standard, this should be unnecessary.
 
#
 
#
#  Define the next four items.
+
#  Define the next five items.
  
# MythTV MySQL Database Name.  Default is "mythconverg."
+
# Name of mythtv database.  Default is "mythconverg."
 
DBNAME=mythconverg
 
DBNAME=mythconverg
  
Line 35: Line 37:
  
  
##  End of definitions
+
echo "USE $DBNAME; SELECT filename FROM \`videometadata\` WHERE \`length\` = 0;" > file.sql
 
+
mysql -h $DBHOST -u$DBUSER -p$DBPASSWORD < file.sql > results.sql
until [ "$count" = "0" ]
+
ed -s results.sql <<< $'1d\nw'
do
 
 
 
##find out how many movies to process
 
echo "USE mythconverg
 
SELECT filename FROM \`videometadata\` WHERE \`length\` = 0 LIMIT 1;
 
SELECT title FROM \`videometadata\` WHERE \`length\` = 0 LIMIT 1;" > lengthlist.sql
 
 
 
mysql -h $DBHOST -u$DBUSER p$DBPASSWORD < lengthlist.sql > sqllist.out
 
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
+
while read FILENAME; do
MOVIE="`cat sql.out | grep -v title | grep -v filename | grep $MOVIEHOME`"
+
MOVIE=`cat results.sql`
MOVIEID="`cat sql.out | grep -v filename | grep -v title | grep -v $MOVIEHOME`"
+
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)}'`
 
 
##Calculate the length
 
TOTALSECONDS=`mplayer -vo null -ao null -frames 0 -identify "$MOVIE" 2>/dev/null | grep "ID_LENGTH" | sed -e 's/ID_LENGTH=//'`
 
 
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 68: Line 52:
  
 
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
  
# Clean Up!
+
# Cleanup!
 
+
rm results.sql file.sql lengthupdate.sql
rm lengthlist.sql lengthfind.sql lengthupdate.sql sqllist.out sql.out
 
 
 
done
 
 
</pre>
 
</pre>

Revision as of 19:24, 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 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