From MythTV Official Wiki
Revision as of 14:57, 14 May 2010 by GBee (Talk | contribs)

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

Here is a script that allows lossless cutting of HD-PVR content. Set this script as executable, and as a userjob. It works on the cutlist, so get that set before running. You could technically swap --getcutlist for --getskiplist and this would probably work as an auto-commercial-removal, but that is an insanely bad idea.

PS, Discussion pages are for suggesting changes, don't just edit someone's user page.  :)

#!/bin/sh -e
# Written by Robert McNamara
# (robert DOT mcnamara ATSIGN gmail DOT com)
# Requires:
#    AVIDemux 2.4 SVN revision 4445 or later (current 10/7/08)
#    Working MythTV setup (To steal the cutlist from)
#    FFMpeg (to fix indexing and drop into an MKV)
# This script *might* work on 1080i material now.  Who knows?
# This script works at the keyframe level only.  I strongly advise
# you to set cutpoints approximately one second into actual program 
# material to avoid accidentally inserting commercials.
# The output MKV file will be in your recordings directory.
# Basic user job setup is:
#  h264cut %DIR% %FILE% 

test -f /etc/mythtv/mysql.txt && . /etc/mythtv/mysql.txt
test -f ~/.mythtv/mysql.txt && . ~/.mythtv/mysql.txt

mysqlconnect="mysql -N -h$DBHostName -u$DBUserName -p$DBPassword $DBName"
export mysqlconnect


chanid=`echo "select chanid from recorded where basename=\"$BASENAME\";" |

starttime=`echo "select starttime from recorded where basename=\"$BASENAME\";" |

if test "$chanid" = "" || test "$starttime" = "" ; then
	echo "Recording not found in MythTV database, aborted."
	exit 1

cat > $RECDIR/$BASENAME.cut << EOF
//AD  <- Needed to identify// 

var app = new Avidemux();

//** Video **
// 01 videos source 

CUTLIST=`mythcommflag --getcutlist -f /$RECDIR/$BASENAME |grep Cutlist| sed 's/Cutlist//g' | sed 's/-/ /g' |sed 's/,/-/g' | sed 's/: 0 //'| sed 's/: /-/'`-
FIRSTFRAME=`echo "select MIN(mark) from recordedseek where chanid=$chanid and starttime='$starttime';" | $mysqlconnect`
LASTFRAME=`echo "select MAX(mark) from recordedseek where chanid=$chanid and starttime='$starttime';" | $mysqlconnect` 

for i in ${CUTLIST} 

# Get exact frame cut number from Myth
STARTKEYFRAME=`echo $i | cut -d '-' -f 1`   
ENDKEYFRAME=`echo $i | cut -d '-' -f 2`

# Turn those framenumbers into nearest keyframe prior to cutpoint.

START=`echo "select mark from recordedseek where mark < $STARTKEYFRAME and chanid=$chanid and starttime='$starttime' order by mark DESC LIMIT 1;" | $mysqlconnect || continue`
END=`echo "select mark from recordedseek where mark < $ENDKEYFRAME and chanid=$chanid and starttime='$starttime' order by mark DESC LIMIT 1;" | $mysqlconnect || continue`

if [ .${START}. = .. ] 
   then START=0 
    if [ ${START} -ge $LASTFRAME ] 

if [ .${END}. = .. ] 
    then END=$LASTFRAME 

LENGTH=$((${END} - ${START} )) 

if [ ${LENGTH} -le 0 ] 
      echo LENG $LENGTH

cat >> $RECDIR/$BASENAME.cut << EOF 
count=$((${count} + 1)) 

cat >> $RECDIR/$BASENAME.cut << EOF

//** Postproc **,3,0);;

//** Filters **

//** Video Codec conf **"Copy","CQ=4","0 ");

//** Audio **;"copy",128,0,"");;;;"NONE");

//End of script

CORENAME=`basename $BASENAME .mpg`

# Run through AVIDemux.  Don't know how the audio sync will be.

nice -n 9 avidemux2_cli --force-alt-h264 --autoindex --rebuild-index --nogui --force-smart --load "$RECDIR/$BASENAME" --run $RECDIR/$BASENAME.cut --save "$RECDIR/$CORENAME.avi"  --quit 2> /dev/null

# Move the AVI file into a Matroska.  
# Failure to do this will result in broken seeking.

ffmpeg -i $RECDIR/$CORENAME.avi -acodec copy -vcodec copy $RECDIR/$CORENAME.mkv

# Do a little cleanup.


This is a script that will correct broken MPEG-2 TSs from firewire or DVB. The script replaces the original recording with a file that will work with mythtranscode rather than crashing it.


# Written in 2008 by Robert McNamara.  Please redistribute freely and without
# restriction. (robert DOT mcnamara ATSIGN g m a i l DOTCOM)
# Script to clean up Firewire and DVB recordings that kill mythtranscode.
# It appears the bogus PTS information makes mythtrascode lossless transcoding
# fail with error 232 (Unknown).  It complains about a deadlock.
# This solution is not the most elegant in the world, but it *does* work and
# ultimately permits Myth to successfully lossless-transcode the files.
# There are two prerequisites for this script.  The first is the xport
# transport stream demuxer.  It is only distributed for Windows but it compiles
# fine on linux.  Get the following file:
# And unzip it.  Compile it with "cc -o xport xport.c" and copy the resulting
# file to somewhere in your path (in my case, /usr/bin).
# You will also need a copy of ffmpeg with AC3 and MPEG-2 support.
# In essence, this script demuxes the file recorded from DVB/Firewire, and
# remuxes the streams with ffmpeg into a "clean" MPEG container.  It copies the
# existing recording file to the same location with extension .tmp and inserts
# the new file in its place.  Commercial flags and cutlists are cleared, and
# commflagging is run on the new file.  In my limited testing, Myth can now
# successfully transcode the resulting file losslessly.
# Here's how it should be set up as a userjob:
# "%DIR%" "%FILE%" 
# I set the script to work within /tmp.  If you don't want it to do that,
# change the first variable below.
# Even on a powerful processor (I run this on a Q6600) you can expect this to cost 
# you 10 or so minutes of processing time for each hourlong show.  It's the price
# you pay to preserve quality and end up with lossless-transcode-able files.



# Make the directory to work in:

# First step is dumping the PIDs to files
xport "$DIR/$FILE" 1 1 1
# Once we're done with that, let's mux everything into a new file.
ffmpeg -i bits0001.mpv -i bits0001.mpa -vcodec copy -acodec copy "$WORKINGDIR/$FILE/$FILE"

# Let's move the old file out of the way, and the new file into its place.
mv "$DIR/$FILE" "$DIR/$FILE.old"

# Now we need to clear the cutlist on the file, rebuild its seektable, and rerun commflagging.

mythcommflag --clearcutlist -f "$DIR/$FILE"
if [ $ERROR -ne 0 ]; then
        echo "Clearing cutlist failed for $FILE with error $ERROR"
        exit $ERROR

# *Don't* rebuild the seektable traditionally or you will end up with screwed up times and it will be uneditable.
# Use mythtranscode on lossless/buildindex mode instead.

mythtranscode --mpeg2 --infile "$DIR/$FILE" --buildindex --showprogress
if [ $ERROR -ne 0 ]; then
        echo "Rebuilding seektable failed for $FILE with error $ERROR"
        exit $ERROR

# Commercial Flagging.  This mostly sucks on my broken recordings, you can comment it out if 
# you wish.  I find that I basically have to go in and manually set cutpoints anyway.

mythcommflag -f "$DIR/$FILE"
if [ $ERROR -ne 0 ]; then
        echo "Commercial flagging failed for ${FILENAME} with error $ERROR"
        exit $ERROR

# Remove the contents of the working directory
# If you want to remove the old recording, uncomment the last line:
# rm "$DIR/$FILE.old"