Mythcutprojectx

From MythTV Official Wiki
Revision as of 11:25, 8 March 2013 by Johnp (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Important.png Note: The correct title of this article is mythcutprojectx. It appears incorrectly here due to technical restrictions.


Author John Pilkington
Description Script to preprocess MPEG-2 recordings from DVB-T before running MythArchive
Supports


Warning.png Warning: An updated script based on this one is now at http://www.mythtv.org/wiki/MythDVBcut

It is not limited to mpeg2video recordings and uses MythTV tools from 0.25+

The link between the editor-based cutlist and the cutpoints actually used has also been changed slightly.--Johnp 11:25, 8 March 2013 (UTC)

I have modified this script and believe that it will now work under both MythTV 0.24 and 0.23 with appropriate setting of the variable INVERT. #9389 refers - Johnp 23:02, 20 December 2010 (UTC).

Added call to mythpreviewgen immediately before exit. Johnp 23:49, 5 January 2012 (UTC)

Updated link in text to latest version of Project-X Johnp 11:08, 7 January 2012 (UTC)

As stated in the preamble, this script is essentially a replacement for the 'lossless' mythtranscode with cutlist for mpeg-2 recordings known to the mythtv database. Mythtranscode works well with many recordings, but it has problems with others, maybe because individual data streams start or stop. One typical failure mode reports that 'one buffer is full and the other is empty', and aborts, but other defects in the input may not be detected until they cause a failure in a later process such as DVD creation or a defect in the DVD that has been created. It seems that Project-X is more tolerant of defects in its input, and recordings preprocessed by this script usually yield good DVDs.

I have used mythcutprojectx in MythTV 0.23-fixes, many hundreds of times. It initially assumed that the audio format was mp2. I see no reason for leaving this version here and I am removing it (3 Nov 2010).

I had reports that using it in trunk (July 2010) gave an inverse cut, and I posted a version called trunkcutprojectx, which I had not tested myself, that was intended to cut in the right sense in trunk and to recognize both mp2 and ac3 audio.

In July I posted a new version, found below, in which the sense of cut is controlled by editing the variable INVERT. It is still intended to be run from a terminal but now after listing the video and audio streams found by "ffmpeg -i" it will automatically select the first listed and work with them unless the user intervenes within TIMEOUT seconds. If the choice is to quit, the two wanted PIDs can be given in the command line. If the wanted PIDs are known in advance no interaction is needed. In the UK the two main PIDs are usually determined by the channel number.

MythArchive does itself offer the ability to use Project-X to apply the MythTV cutlist. I have preferred to use this script. Applying the cutlist via Project-X within MythArchive sometimes gave me the wrong sound channel, and it means that the size of the final file is not known when the recordings are selected. I also believe that the conversion and application of the cutlist here is more robust than in mythburn.py. It uses byte counts, not frame counts. See below for additional comments.


The cutlist is not applied with single-frame accuracy but I have found it robust and 'good enough'. I have had very few failures when the script has been used on the original recording; it seemed less reliable when, in its early days, I used it on recordings that had been preprocessed using mencoder.

The script assumes that the recording will be in one of two folders that must be defined by editing, and uses two other folders for temp storage. The intention is to read from one disc and write to another, but this isn't essential. I hope that modifications for a different number of discs will be obvious.

Commands that cause intensive disc activity are preceded in the script by 'ionice -c3'; this works as intended on my Fedora_12 box, but not under CentOS_5. Just use the command without the 'ionice -c3' preface.

Project-X uses a configuration file, default name X.ini, which it may create or modify e.g. when used under the GUI. This script, and MythArchive, work as intended if no X.ini is found. Under this script its expected location is in the relevant RECDIR. When Project-X is used within MythArchive the expected location is ~/X.ini.

At the original time of writing 'commflag --clearcutlist' produced a stream of false error messages after completion. This has now (July 2010) been cured.

The script (still called mythcutprojectx) uses ideas from scripts found in the mythtv lists: mythcutprojectx and pxcut.sh

This script was initially posted in trac. The trac page has links explaining the motivation for writing this script and its relationship to MythArchive.

#8347#comment:5

I first used the script with the 2006 'release' version 0.90.4 of Project-X. A newer version is available but seems quite well hidden; I had seen a reference to it in the myth-user list but only located it as a tarball around Sept 2010. It's here: http://project-x.cvs.sourceforge.net/viewvc/project-x/Project-X/?view=tar

I have had no problems with it. In the script logs it identifies itself as ProjectX 0.90.4.00.b32/30.12.2009 TEST PROJECT ONLY.

Update: the current latest tarball is now available from the foot of this page:

http://project-x.cvs.sourceforge.net/project-x/Project-X/ Johnp 11:08, 7 January 2012 (UTC)

Comment added 29 Dec 2010: I have been looking into the way the MythTV cutlist is translated for use by Project-X. It is clear that some comments built into this script are not up-to-date and it may be that changes in the method used would sometimes be useful. That would depend on what sort of errors might be present in the data. Project-X seems to be able to use a CutMode in the range 0 to 4, defined in Gop.java as CUTMODE_BYTE, CUTMODE_GOP, CUTMODE_FRAME, CUTMODE_PTS and CUTMODE_TIME. The recordedseek and recordedmarkup tables in the MythTV database have markers with a type listed here http://www.mythtv.org/wiki/Recordedseek_table that may be related to a particular Project-X CutMode. In the script here and now the CUTMODE_BYTE and mark type 9 are linked; in Mythburn.py the link is between CUTMODE_FRAME and marks type 0 and 1. These, and other possibilities, may offer different combinations of cut-accuracy and robustness.


Comments from boehmi:

/bin/sh doesn't understand the -t option in read, at least not in Ubuntu 10.04 LTS, so I use /bin/bash instead.

The remark about the INVERT variable isn't true anymore. In 0.24+fixes INVERT needs to be "false"


Script.png mythcutprojectx_20120105.sh


#!/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 terminal-based 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, chooses the first video and audio streams listed, 
# and gives the user TIMEOUT seconds to accept that choice or quit and make another.
# 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 and ffmpeg stream analysis are kept. Other tempfiles are deleted.
# The variable INVERT controls the sense in which the cutlist is applied.

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

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

# Variables RECDIR1, RECDIR2, TEMPDIR1, TEMPDIR2, PROJECTX, PASSWD, INVERT need to be customised.
# At present (July 2010) MythTV trunk and fixes apparently apply the cutlist in opposite senses.
# TESTRUN is initially set to true so that the polarity of the cutlist that will be passed to Project-X can be checked
# 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

INVERT=true  # old MythTV setting, used in "fixes"
# INVERT=false   # setting for use in trunk

TIMEOUT=20   # Longest 'thinking time' in seconds allowed before adopting the automatically selected audio stream.

TESTRUN=true    # cutlists will be shown but the recording will be unchanged  
# TESTRUN=false  # the recording will be processed
#################

if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
echo "Usage: "$0" <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

# exit if .old file exists

if  [ -f ${RECDIR1}/"$1".old ] ; then 
    echo " ${RECDIR1}/"$1".old exists: giving up." ; exit 1
fi

if  [ -f ${RECDIR2}/"$1".old ] ; then 
    echo " ${RECDIR2}/"$1".old exists: giving up." ; exit 1
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 | tee temp$$.txt
   echo

   # Thanks to Christopher Meredith for the basic parsing magic here. 
   VPID=`grep Video  temp$$.txt | head -n1 | cut -f 1,1 -d']' | sed 's+.*\[++g'`
   # It has to be tweaked for multiple audio streams.  This (with head -n1 ) selects the first listed by ffmpeg.
   # You may alternatively wish to select for language, format, etc.   May be channel, programme, user dependent.
   APID=`grep Audio  temp$$.txt | head -n1 | cut -f 1,1 -d']' | sed 's+.*\[++g'`

   echo -e "Choosing the first audio track listed by \" ffmpeg -i \".  It may not be the one you want."
   echo -e "\nThe selected values would be "$VPID" and "$APID".  The track info for these is \n"

   grep "$VPID" temp$$.txt
   grep "$APID" temp$$.txt

   echo -e "\nTo accept these values press \"a\", or wait....\n"  
   echo  "If you want to select other values, or quit to think about it, press another key within $TIMEOUT seconds."

   read -t $TIMEOUT -n 1 RESP
   if  [ $? -gt 128 ] ; then    
       RESP="a"
   fi

   if [ "$RESP" != "a" ] ; then
       echo -e "Quitting: if you want to select the PIDs from the command line its expected form is   \n"
       echo " "$0" 1234_20070927190000.mpg  0xvvv 0xaaa " 
       echo -e "                    filename_in_DB           vPID  aPID \n" 
       cd ~
       exit 1
   fi

   echo -e "Going on: processing with suggested values $VPID  $APID \n"
   grep "$VPID" temp$$.txt
   grep "$APID" temp$$.txt
   echo
else
   VPID="$2"
   APID="$3"
fi

#Now do the actual processing
# 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 `

# In 0.24 an initial zero is apparently treated as a normal cut-in point.
# list0 shows cut-in points and eof, but in 0.23 never includes zero
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 shows cut-out points.  In 0.23 an initial 0 here is a cut-in. 
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$$ ;
if  ! $INVERT ; then 

    FIRSTCUT=`echo "select mark from recordedmarkup
    where chanid=$chanid and starttime='$starttime' and type=1 order by mark limit 1;" |
    mysql -N -u mythtv  -p$PASSWD mythconverg `  

    FIRSTEDIT=`echo "select mark from recordedmarkup
    where chanid=$chanid and starttime='$starttime' and type in (0,1) order by mark limit 1;" |
    mysql -N -u mythtv  -p$PASSWD mythconverg `  

    if [ ${FIRSTCUT} = ${FIRSTEDIT} ] ; then
        # echo "that was a cut-out point and we need to insert an earlier cut-in point"
        echo "0" >> cutlist$$
    fi

    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 ` 
 
else 
    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$$
           # That isn't quite the same as inserting a leading zero in list.  Does it matter?
           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
fi 

# 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

echo -e "\"list\" is MythTV's frame-count cutlist that is used to create the byte-count cutlist used here by Project-X."
echo "At the time of writing (July 2010) the internal cutlists used by fixes and trunk appear to work in opposite senses."
echo "The cut can be inverted by adding or removing a leading zero. "
echo -e "That is what the INVERT variable does.  For fixes it should be set to \"true\" \n"

echo "This is the byte-count cutlist for Project-X. The first value is a cut-in point."
echo -e "Cut-out and cut-in then follow in sequence to EOF. \n"
cat cutlist$$
echo -e "\nThis is a test exit point for you to check that INVERT is correctly set."
echo -e "Its value now is  \"${INVERT}\".  When it's OK, edit the script to set TESTRUN=false.\n"

if $TESTRUN ; then
   rm -f cutlist$$
   rm -f temp$$.txt   
   cd ~
   exit 0
fi

mv  "$1" "$1".old

# use ProjectX to de-multiplex selected streams with the created cutlist
ionice -c3 java -jar "$PROJECTX" -name tempcut$$ -id ${VPID},${APID} -out $TEMP -cut cutlist$$ "$1".old || :

# and pipe for re-multiplexing to mplex. -f 9 is dvd format without navpacks
DEMUXPREF=$TEMP/tempcut${$}
if [ -f $DEMUXPREF.mp2 ] ; then
    DEMUXAUDIO=$DEMUXPREF.mp2
else
    DEMUXAUDIO=$DEMUXPREF.ac3
fi
ionice -c3  mplex -o "$1" -V -f 9 $DEMUXPREF.m2v $DEMUXAUDIO
  
# 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 -e "Seek table has been rebuilt.\n"

echo -e "Output file is $1.  INVERT is set to \"${INVERT}\".  PID streams $VPID and $APID were copied.\n"
if [ -f temp$$.txt ] ; then
     echo -e "Their original parameters were \n"
     grep "$VPID" temp$$.txt
     grep "$APID" temp$$.txt
     cat temp$$.txt >> "$DEMUXPREF"_log.txt
     echo
fi

rm -f "$1".png
#rm -f $TEMP/tempcut${$}*
mv  ${DEMUXPREF}_log.txt ${TEMP}/"$1"_pxlog.txt 
rm -f $DEMUXPREF.m2v
rm -f $DEMUXAUDIO
rm -f cutlist$$
rm -f temp$$.txt
cd ~

# mythpreviewgen isn't essential here so put it where failure won't cause other problems.

echo "Running: ionice -c3 mythpreviewgen --chanid "$chanid" --starttime "$starttime""
ionice -c3 mythpreviewgen --chanid "$chanid" --starttime "$starttime"
echo -e "Preview created.\n"

exit 0