From dd0d1371a1ef3b0a5a71b32721b9cef2277d7caa Mon Sep 17 00:00:00 2001
From: Britney Fransen <brfransen@gmail.com>
Date: Tue, 11 Feb 2014 21:31:51 +0000
Subject: LinHES-system: myth2mkv: use jobqueue_helper.py for myth db access.
 refs #958

jobqueue_helper.py: initial inclusion.
---
 abs/core/LinHES-system/PKGBUILD           |   7 +-
 abs/core/LinHES-system/jobqueue_helper.py |  63 ++++++++++++++++++
 abs/core/LinHES-system/myth2mkv           | 105 ++++++++++++++++++++----------
 3 files changed, 136 insertions(+), 39 deletions(-)
 create mode 100755 abs/core/LinHES-system/jobqueue_helper.py

diff --git a/abs/core/LinHES-system/PKGBUILD b/abs/core/LinHES-system/PKGBUILD
index 8bb0e76..97d7fa0 100755
--- a/abs/core/LinHES-system/PKGBUILD
+++ b/abs/core/LinHES-system/PKGBUILD
@@ -1,6 +1,6 @@
 pkgname=LinHES-system
 pkgver=8.1
-pkgrel=14
+pkgrel=15
 arch=('i686' 'x86_64')
 install=system.install
 pkgdesc="Everything that makes LinHES an automated system"
@@ -21,7 +21,7 @@ binfiles="LinHES-start optimize_mythdb.py myth_mtc.py
  create_media_dirs.sh msg_client.py msg_daemon.py
  gen_is_xml.py gen_lib_xml.py gen_light_include.py gen_game_xml.py
  misc_recent_recordings.pl misc_status_config.py misc_status_info.sh
- misc_upcoming_recordings.pl misc_which_recorder.pl
+ misc_upcoming_recordings.pl misc_which_recorder.pl jobqueue_helper.py
  change_channel.sh change_channel_wrapper.sh stop_xss.sh
  be_check.py checkXFSfrag.sh find_orphans.py idle.py xwin_find.sh
  linhes_update.sh linhes_update2.sh myth2mkv myth2mp3 ripD_eject.sh
@@ -111,6 +111,7 @@ md5sums=('7ab2a2c643d2b286811d8303d08982ad'
          '3300ea8b02e4fb8bd3409df348de6e16'
          '145b1da6ce501b3ce38ea415a576bf2d'
          'b51c93ad9f3717a616d92899f6bfde76'
+         'd8f30983055dcfe2f53197d0f9a62158'
          '17f678d37187be0f12d67f64e40429c6'
          'bacc813b48bafcc6fe906e5969930501'
          '8e02efe1ad0df9a179075147eebb05b9'
@@ -121,7 +122,7 @@ md5sums=('7ab2a2c643d2b286811d8303d08982ad'
          'a94fe6d980f4b810f2e2ae5352084b39'
          '2c56266a79d058bf01f0de19c2cd042a'
          'c27d3fdf59b211f9d3cd76a81f6257dc'
-         '503df99218373dfc75e7e7f5e449a44e'
+         '28f29578e5b3ba84fdf2aa57cf475bcf'
          '4a1fda884dcd7d65fb2690fbdbd92a83'
          '2b7fe3b57592823a4c7e3ec132dcb7f4'
          '20dd97b614cab2454794416a3601c497'
diff --git a/abs/core/LinHES-system/jobqueue_helper.py b/abs/core/LinHES-system/jobqueue_helper.py
new file mode 100755
index 0000000..d27e693
--- /dev/null
+++ b/abs/core/LinHES-system/jobqueue_helper.py
@@ -0,0 +1,63 @@
+#!/usr/bin/python2
+
+import argparse, os, re, subprocess, sys, time
+from MythTV import MythDB, Job
+
+mythDB = MythDB()
+cursor = mythDB.cursor()
+
+def set_cmds(cmdargs,job):
+    #print "Setting cmds on job %s to %s" %(cmdargs.jobid,cmdargs.cmd)
+    cursor.execute("update jobqueue set cmds = '%s' where id = '%s'" %(cmdargs.cmd,cmdargs.jobid))
+
+def set_comment(cmdargs,job):
+    #print "Setting comment on job %s to %s" %(cmdargs.jobid,cmdargs.comment)
+    job.setComment("%s" %cmdargs.comment)
+
+def set_status(cmdargs,job):
+    #print "Setting status on job %s to %s" %(cmdargs.jobid,cmdargs.status)
+    job.setStatus("%s" %cmdargs.status)
+
+def run_cursor(cmdargs):
+    cursor.execute("%s" %cmdargs.man_cursor)
+    results=cursor.fetchone()
+    print results[0]
+
+def usage():
+    line = '''
+    jobqueue_helper.py provides MythTV job queue functions
+    using python bindings for bash scripts.
+    Use jobqueue_helper.py -h to see options.
+    '''
+    print line
+    sys.exit(0)
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-cs', '--comment_set', action='store', dest='comment', help='Set the comment of the jobid')
+    parser.add_argument('-cmds', '--cmd_set', type=int, default=77777, action='store', dest='cmd', help='Set the cmd of the jobid')
+    parser.add_argument('-ss', '--status_set', type=int, action='store', dest='status', help='Set the status of the jobid')
+    action = parser.add_mutually_exclusive_group(required=True)
+    action.add_argument('-j', '--jobid', type=int, help='jobid of the job to control')
+    action.add_argument('-m', '--man_cursor', action='store', dest='man_cursor', help='Manual mysql cursor command')
+    action.add_argument('-u', '--usage', action='store_true', help='Print usage instructions.')
+
+    cmdargs = parser.parse_args()
+
+    if cmdargs.usage:
+        usage()
+
+    if cmdargs.jobid:
+        job = Job(cmdargs.jobid)
+
+    if cmdargs.comment:
+        set_comment(cmdargs, job)
+
+    if cmdargs.status:
+        set_status(cmdargs, job)
+
+    if cmdargs.cmd != 77777:
+        set_cmds(cmdargs, job)
+
+    if cmdargs.man_cursor:
+        run_cursor(cmdargs)
diff --git a/abs/core/LinHES-system/myth2mkv b/abs/core/LinHES-system/myth2mkv
index aedccff..1352476 100644
--- a/abs/core/LinHES-system/myth2mkv
+++ b/abs/core/LinHES-system/myth2mkv
@@ -2,13 +2,14 @@
 #
 # Convert video to AVC-1 / h264
 #
-# version 0.25-004
+# version 0.27-001
 #
 # Prerequisites:
-#   - mythtv >= 0.25
+#   - mythtv >= 0.27
 #   - handbrake-cli
 #   - mplayer
 #   - mkvtoolnix
+#   - jobqueue_helper.py
 #
 # Arguments
 # $1 must be the directory/file of the recording
@@ -20,7 +21,9 @@
 # $7 must be quality of encode
 #
 # As a MythTV user job:
-# myth2mkv "%DIR%/%FILE%" "%CHANID%" "%STARTTIME%" "%TITLE%" "%SUBTITLE%" "%JOBID%" HQ|MQ|LQ
+# myth2mkv "%DIR%/%FILE%" "%CHANID%" "%STARTTIME%" "%TITLE%" "%SUBTITLE%" "%JOBID%" "HP|HQ|MQ|LQ"
+# Select only 1 quality setting
+# HP is similar to the HandBrake built-in preset High Profile
 
 ########################
 #                      #
@@ -47,12 +50,13 @@ TUNING=""
 # Crop 160 pixels off the left and right for 4:3 image in 1280x720 frame
 # <T:B:L:R>
 # i.e. 0:0:240:240
-# Default: In HQ: CROP="0:0:0:0" (no cropping).
+# Default: In HP and HQ: CROP="0:0:0:0" (no cropping).
 #          IN MQ and LQ: autocrop.
 CROP=""
 
 # Force custom output resolution.
 # Default: Keep same resolution as input file (less any cropping).
+# The HP quality setting always keeps the same resolution as the input file.
 WIDTH=""
 HEIGHT=""
 
@@ -69,15 +73,6 @@ DEINT="G"
 #                          #
 ############################
 
-if [[ -e $HOME/.mythtv/mysql.txt ]] ; then
-  . $HOME/.mythtv/mysql.txt
-else
-  DBHostName=${DBHostName:-"localhost"}
-  DBUserName=${DBUserName:-"mythtv"}
-  DBPassword=${DBPassword:-"mythtv"}
-  DBName=${DBName:-"mythconverg"}
-fi
-
 if [[ ! -d ${TMPDIR} ]] ; then
   mkdir -p ${TMPDIR}
 fi
@@ -91,8 +86,7 @@ update_comment()
 # Arg_1 = COMMENT
 {
 if [ ${NO_JOBID} -eq 0 ]; then
-   SQL_CMD="update jobqueue set comment=\"${1}\" where id=\"${JOBID}\";"
-   `${MYSQLCMD} "${SQL_CMD}"`
+    `jobqueue_helper.py -j ${JOBID} -cs "${1}"`
 fi
 }
 
@@ -101,17 +95,16 @@ check_background_progress()
 {
 while [ `tail -2 ${STATUSFILE} | grep -c "^HandBrake has exited"` = 0 ]
 do
-    sleep 10
+    sleep 15
     check_myth_jobcmds
-    pass=`tail -1 ${STATUSFILE} | egrep -o -e 'task [0-9]' | tail -1 | sed 's/task\ //g'`
+    pass=`tail -1 ${STATUSFILE} | egrep -o -e 'task [0-9] of [0-9], ' | tail -1 | sed 's/task\ /Pass\ /g'`
     prog_percent=`tail -1 ${STATUSFILE} | egrep -o -e '[0-9]*\.[0-9]. %' | tail -1 | sed 's/\ %//g'`
     current_FPS=`tail -1 ${STATUSFILE} | egrep -o -e 'avg [0-9.]*\.[0-9]* fps' | tail -1 | sed -e 's/avg\ //g' -e 's/\ fps//g'`
     current_ETA=`tail -1 ${STATUSFILE} | egrep -o -e 'ETA [0-9.][0-9.]h[0-9.][0-9.]m[0-9.][0-9.]s' | tail -1`
     if [ -n "$prog_percent" ]; then
-        echo "Pass ${pass}, ${prog_percent}% @ ${current_FPS} fps. ${current_ETA}"
-        update_comment "Pass ${pass} of 2, ${prog_percent}% @ ${current_FPS} fps. ${current_ETA}"
+        echo "${pass}${prog_percent}% @ ${current_FPS} fps. ${current_ETA}"
+        update_comment "${pass}${prog_percent}% @ ${current_FPS} fps. ${current_ETA}"
     fi
-    sleep 10
 done
 }
 
@@ -119,24 +112,24 @@ check_myth_jobcmds()
 # check the myth database for stop pause or resume commands
 {
 if [[ ${NO_JOBID} -eq 0 ]] ; then
-    CURRENT_CMD=`${MYSQLCMD} "select cmds from jobqueue where id = \"${JOBID}\";"`
+    CURRENT_CMD=`jobqueue_helper.py -m "select cmds from jobqueue where id = ${JOBID}"`
     case "${CURRENT_CMD}" in
         # JOB_RUN
         0) ;;
         # JOB_PAUSE
-        1) `${MYSQLCMD} "update jobqueue set status=\"6\" where id=\"${JOBID}\";"`
+        1) `jobqueue_helper.py -j ${JOBID} -ss 6`
            kill -s STOP ${handbrake_pid} ;;
         # JOB_RESUME
-        2) `${MYSQLCMD} "update jobqueue set status=\"4\" where id=\"${JOBID}\";"`
-           `${MYSQLCMD} "update jobqueue set cmds=\"0\" where id=\"${JOBID}\";"`
+        2) `jobqueue_helper.py -j ${JOBID} -ss 4`
+           `jobqueue_helper.py -j ${JOBID} -cmds 0`
            kill -s CONT ${handbrake_pid} ;;
         # JOB_STOP
-        4) `${MYSQLCMD} "update jobqueue set status=\"5\" where id=\"${JOBID}\";"`
-           `${MYSQLCMD} "update jobqueue set cmds=\"0\" where id=\"${JOBID}\";"`
+        4) `jobqueue_helper.py -j ${JOBID} -ss 5`
+           `jobqueue_helper.py -j ${JOBID} -cmds 0`
            kill -9 ${handbrake_pid} ${command_pid}
            clean_up_files
            echo "Encode Cancelled" >> ${LOGFILE}
-            `${MYSQLCMD} "update jobqueue set status=\"320\" where id=\"${JOBID}\";"`
+           `jobqueue_helper.py -j ${JOBID} -ss 320`
            exit ;;
     esac
 fi
@@ -200,7 +193,14 @@ if [[ -n ${HEIGHT} ]] ; then
   HEIGHT="-l ${HEIGHT} -Y ${HEIGHT}"
 fi
 
-if [[ ${QUALITY} = HQ ]] ; then
+if [[ ${QUALITY} = HP ]] ; then
+  if [[ -n ${CROP} ]] ; then
+    CROP="--crop ${CROP}"
+  else
+    CROP="--crop 0:0:0:0 --strict-anamorphic"
+  fi
+  HB_OPTS="-o ${TMPFILE} -e x264 ${TUNING} -q 20.0 -a 1,1 -E copy:ac3,faac -B 160,160 -6 dpl2,auto -R Auto,Auto -D 0.0,0.0 --audio-copy-mask aac,ac3,dtshd,dts,mp3 --audio-fallback ffac3 -f mkv --decomb --loose-anamorphic --modulus 2 -m --x264-preset medium --h264-profile high --h264-level 4.1 ${CROP} -s 1"
+elif [[ ${QUALITY} = HQ ]] ; then
   if [[ -n ${CROP} ]] ; then
     CROP="--crop ${CROP}"
   else
@@ -260,10 +260,18 @@ echo "       <Simple>" >> "${TAG_FILE}"
 echo "         <Name>DATE_RELEASED</Name>" >> "${TAG_FILE}"
 echo "         <String>${OAD}</String>" >> "${TAG_FILE}"
 echo "       </Simple>" >> "${TAG_FILE}"
+echo "       <Simple>" >> "${TAG_FILE}"
+echo "         <Name>SEASON</Name>" >> "${TAG_FILE}"
+echo "         <String>${SEASON}</String>" >> "${TAG_FILE}"
+echo "       </Simple>" >> "${TAG_FILE}"
+echo "       <Simple>" >> "${TAG_FILE}"
+echo "         <Name>EPISODE</Name>" >> "${TAG_FILE}"
+echo "         <String>${EPISODE}</String>" >> "${TAG_FILE}"
+echo "       </Simple>" >> "${TAG_FILE}"
 echo "     </Simple>" >> "${TAG_FILE}"
 echo "   </Simple>" >> "${TAG_FILE}"
 echo "   <Simple>" >> "${TAG_FILE}"
-echo "       <Name>ENCODER</Name>" >> "${TAG_FILE}"
+echo "       <Name>ENCODED_BY</Name>" >> "${TAG_FILE}"
 echo "       <String>HandBrakeCLI ${HBCLIVER}</String>" >> "${TAG_FILE}"
 echo "   </Simple>" >> "${TAG_FILE}"
 echo "   <Simple>" >> "${TAG_FILE}"
@@ -308,9 +316,12 @@ STATUSFILE=/tmp/${TMPNAME}-status.log
 HB_RETURN_CODE=/tmp/${TMPNAME}-hb_return_code
 IDFILE=/tmp/${TMPNAME}-id.txt
 TAG_FILE=/tmp/${TMPNAME}.xml
-MYSQLCMD="mysql -B --skip-column-names -u ${DBUserName} -p${DBPassword} -h ${DBHostName} -D ${DBName} -e"
-OAD=`${MYSQLCMD} "select originalairdate from recorded where basename LIKE '${BASE}';"`
-DESCR=`${MYSQLCMD} "select description from recorded where basename LIKE '${BASE}';" | sed 's/\&/and/g'`
+SEASON=`jobqueue_helper.py -m "select season from recorded where basename LIKE '${BASE}'"`
+SEASON=`printf "%02d" $SEASON`
+EPISODE=`jobqueue_helper.py -m "select episode from recorded where basename LIKE '${BASE}'"`
+EPISODE=`printf "%02d" $EPISODE`
+OAD=`jobqueue_helper.py -m "select originalairdate from recorded where basename LIKE '${BASE}'"`
+DESCR=`jobqueue_helper.py -m "select description from recorded where basename LIKE '${BASE}'" | sed 's/\&/and/g'`
 USER=`whoami`
 
 # check if %JOBID% is passed from command line
@@ -392,7 +403,11 @@ fi
 
 # make output filename unique and do not clobber an existing file
 # Build a final file name
-FILE=$( echo "${TITLE,,} ${OAD} ${SUBTITLE,,}" | tr -d [:punct:] | tr [:blank:] "_" | tr -s "_" )
+if [[ $SEASON != "00" && $EPISODE != "00" ]]; then
+    FILE=$( echo "${TITLE,,} s${SEASON}e${EPISODE} ${SUBTITLE,,}" | tr -d [:punct:] | tr [:blank:] "_" | tr -s "_" )
+else
+    FILE=$( echo "${TITLE,,} ${OAD} ${SUBTITLE,,}" | tr -d [:punct:] | tr [:blank:] "_" | tr -s "_" )
+fi
 OUTPUTFILE="${OUTDIR}/${FILE}.mkv"
 i=1
 while [ -e "${OUTPUTFILE}" ]
@@ -422,9 +437,27 @@ if [ ${ERROR} -eq 0 ]; then
     seconds=$((seconds % 3600))
     minutes=$((seconds / 60))
     seconds=$((seconds % 60))
-    echo "Encoding took ${hours} hour\(s\) ${minutes} minute\(s\) ${seconds} second\(s\) @ ${current_FPS} fps." >> ${LOGFILE}
-    `${MYSQLCMD} "update jobqueue set status = \"272\" where id = \"${JOBID}\";"`
-    update_comment "Encode Successful. Encoding Time: ${hours} hour\(s\) ${minutes} minute\(s\) ${seconds} second\(s\)"
+    if [ $hours -eq 0 ]; then
+        hours=""
+    elif [ $hours -eq 1 ]; then
+        hours=" $hours hour"
+    else
+        hours=" $hours hours"
+    fi
+    if [ $minutes -eq 1 ]; then
+        minutes="$minutes minute"
+    else
+        minutes="$minutes minutes"
+    fi
+    if [ $seconds -eq 1 ]; then
+        seconds="$seconds second"
+    else
+        seconds="$seconds seconds"
+    fi
+
+    echo "Encoding took${hours} ${minutes} ${seconds} @ ${current_FPS} fps." >> ${LOGFILE}
+    `jobqueue_helper.py -j ${JOBID} -ss 272`
+    update_comment "Encode Successful. Encoding Time:${hours} ${minutes} ${seconds}"
 else
     echo "ERROR: ${ERROR}" >> ${LOGFILE}
 fi
-- 
cgit v0.12

