
#!/bin/sh -e

# Copyright (C) 2010 John Pilkington 
# Largely based on scripts posted by Tino Keitel and Kees Cook in the Mythtv lists.

# Usage: ./mythcutprojectx <recording>
# <recording> is an mpeg2 file recorded by MythTV with a valid DB entry.

# This script is essentially a replacement for the 'lossless' mpeg2 mythtranscode.
# It will pass the recording and the MythTV cutlist to ProjectX.
# If the cutlist is empty the entire recording will be processed.
# It uses ffmpeg to report what streams are present and
# asks the user to input the PIDs of the video and one audio stream.
# It uses ProjectX to demux and mplex (from mjpegtools) to remux.
# Output format is DVD compliant without nav packets.
# It then clears the cutlist, updates the filesize in the database and rebuilds the seek table.
# The result is apparently acceptable as a recording within MythTV and as input to MythArchive.
# The ProjectX log file is kept. Other tempfiles are deleted.

# The script needs to be edited to define some local variables.

####################

# Variables RECDIR1, RECDIR2, TEMPDIR1, TEMPDIR2, PROJECTX, PASSWD need to be customised.
# RECDIR1 and TEMPDIR1 should if possible be on different drive spindles.  Likewise RECDIR2 and TEMPDIR2.

RECDIR1=/mnt/f10store/myth/reca
TEMPDIR1=/mnt/sam1/tempb

RECDIR2=/mnt/sam1/recb
TEMPDIR2=/mnt/f10store/myth/tempa

#PROJECTX=/path/to/ProjectX.jar (or to a link to it)
PROJECTX=~/projectx

#PASSWD=`grep "^DBPassword" ~/.mythtv/mysql.txt | cut -d '=' -f 2-`
PASSWD=mythtv

#################

if test "$1" = "-h" || test "$1" = "--help" ; then
echo "Usage: ./mythcutprojectx <recording>"
echo "<recording> is an mpeg2 file recorded by MythTV with a valid DB entry."
echo "e.g. 1234_20100405123400.mpg in one of the defined RECDIRs"
echo "The output file replaces the input file which is renamed to <recording>.old"
exit 0
fi

# Customize with paths to alternative recording and temp folders

cd $RECDIR1
TEMP=$TEMPDIR1
if  [ ! -f "$1" ] ; then
   cd $RECDIR2
   TEMP=$TEMPDIR2
     if  [ ! -f "$1" ] ; then 
       echo " "$1" not found.  Giving up"
       cd
       exit 1
     fi
fi 

if [ $# -lt 3 ]
then
   echo "Error: needs three arguments. Running  ffmpeg -i "$1" 2>&1 | grep -C 4 Video " 
   echo  
   ffmpeg -i "$1" 2>&1 | grep -C 4 Video
   echo
   echo "Expected Command Line is of the form   ./mythcutprojectx 1234_20070927190000.mpg  0xvvv 0xaaa " 
   echo "                                                         filename_in_DB           vPID  aPID  " 
   cd ~
   exit 1
fi

# chanid and starttime identify the recording in the DB
chanid=`echo "select chanid from recorded where basename=\"$1\";" |
mysql -N -u mythtv  -p$PASSWD mythconverg `

starttime=`echo "select starttime from recorded where basename=\"$1\";" |
mysql -N -u mythtv  -p$PASSWD mythconverg `

# list0 gives start (cut-in) points but omits initial zero if start of recording is wanted; includes eof.
list0=`echo "select mark from recordedmarkup
where chanid=$chanid and starttime='$starttime' and type=0 order by mark;" |
mysql -N -u mythtv  -p$PASSWD mythconverg `
  
# list1 gives start of cut (cut-out points). Has initial zero if start of recording is to be cut 
list1=`echo "select mark from recordedmarkup
where chanid=$chanid and starttime='$starttime' and type=1 order by mark;" |
mysql -N -u mythtv  -p$PASSWD mythconverg `  

echo "CollectionPanel.CutMode=0" > cutlist$$ ;
for i in $list1 ;
do
  if [ $i = "0" ] 
    then  
    list=`echo "select mark from recordedmarkup
    where chanid=$chanid and starttime='$starttime' and type in (0,1) order by mark;" |
    mysql -N -u mythtv  -p$PASSWD mythconverg | tail -n +2 ` 
# tail -n +2 drops the initial zero.
  else
    echo "0" >> cutlist$$
    list=`echo "select mark from recordedmarkup
    where chanid=$chanid and starttime='$starttime' and type in (0,1) order by mark;" |
    mysql -N -u mythtv  -p$PASSWD mythconverg `  
  fi
#  use only the first element of list1, as a switch.
  break
done
 
# find the key frame (mark type 9) right before each cut mark,
# extract the byte offset, write it into the ProjectX cutlist
for i in $list ;
do echo "select offset from recordedseek
  where chanid=$chanid and starttime='$starttime' and type=9 and mark >= $i and mark < ($i + 100)
  order by offset;" |
  mysql -N -u mythtv  -p$PASSWD mythconverg | head -n 1
# for each cycle, head -n 1 yields the first line only.
done >> cutlist$$

echo "list0"
echo $list0
echo
echo "list1"
echo $list1
echo
echo "list"
echo $list
echo
cat cutlist$$
echo

#exit

if  [ -f "$1".old ] ; then
  echo " "$1".old exists.  Giving up"
  exit 1
else
  mv  "$1" "$1".old
# use ProjectX to de-multiplex selected streams with the created cutlist
  ionice -c3 java -jar "$PROJECTX" -name tempcut$$ -id $2,$3 -out $TEMP -cut cutlist$$ "$1".old || :
# and pipe for re-multiplexing to mplex. -f 9 is dvd format without navpacks
  ionice -c3  mplex -o "$1" -V -f 9 $TEMP/tempcut${$}.m2v $TEMP/tempcut${$}.mp2 

# tell mythDB about new filesize and clear myth cutlist
  FILESIZE=`du -b "$1" | cut -f 1`
  if [ "${FILESIZE}" -gt 1000000 ]; then
      echo "running: update recorded set filesize=${FILESIZE} where basename=\"$1\";"
      echo "update recorded set filesize=${FILESIZE} where basename=\"$1\";" | mysql -u mythtv -p$PASSWD mythconverg
      echo "filesize has been reset"
      echo "running: ionice -c3 mythcommflag -f "$1" --clearcutlist"
      ionice -c3 mythcommflag -f "$1" --clearcutlist
      echo "cutlist has been cleared"
  fi

#rebuild seek table
  echo "running: ionice -c3 mythtranscode --mpeg2 --buildindex --showprogress --chanid "$chanid" --starttime "$starttime""
  ionice -c3 mythtranscode --mpeg2 --buildindex --showprogress --chanid "$chanid" --starttime "$starttime"
  echo "seek table has been rebuilt"

  echo "Output file is $1  -  PID streams $2 and $3 copied"
  echo
  rm -f "$1".png
#rm -f $TEMP/tempcut${$}*
  mv $TEMP/tempcut${$}_log.txt $TEMP/$1_pxlog.txt 
  rm -f $TEMP/tempcut${$}.m2v
  rm -f $TEMP/tempcut${$}.mp2
  rm -f cutlist$$
fi

cd
exit 0

