Length Script
From MythTV Official Wiki
Revision as of 17:19, 2 January 2008 by Iamlindoro (talk | contribs) (Script for auto-generating movie lengths in MythVideo)
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.
#!/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=mythtv # MySQL Password DBPASSWORD=mythtv # 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 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 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=//'` 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 # Clean Up! rm lengthlist.sql lengthfind.sql lengthupdate.sql sqllist.out sql.out done