Ticket #8347: mythcutprojectx

File mythcutprojectx, 5.9 KB (added by J.Pilk@…, 10 years ago)
Line 
1
2#!/bin/sh -e
3
4# Copyright (C) 2010 John Pilkington
5# Largely based on scripts posted by Tino Keitel and Kees Cook in the Mythtv lists.
6
7# Usage: ./mythcutprojectx <recording>
8# <recording> is an mpeg2 file recorded by MythTV with a valid DB entry.
9
10# This script is essentially a replacement for the 'lossless' mpeg2 mythtranscode.
11# It will pass the recording and the MythTV cutlist to ProjectX.
12# If the cutlist is empty the entire recording will be processed.
13# It uses ffmpeg to report what streams are present and
14# asks the user to input the PIDs of the video and one audio stream.
15# It uses ProjectX to demux and mplex (from mjpegtools) to remux.
16# Output format is DVD compliant without nav packets.
17# It then clears the cutlist, updates the filesize in the database and rebuilds the seek table.
18# The result is apparently acceptable as a recording within MythTV and as input to MythArchive.
19# The ProjectX log file is kept. Other tempfiles are deleted.
20
21# The script needs to be edited to define some local variables.
22
23####################
24
25# Variables RECDIR1, RECDIR2, TEMPDIR1, TEMPDIR2, PROJECTX, PASSWD need to be customised.
26# RECDIR1 and TEMPDIR1 should if possible be on different drive spindles.  Likewise RECDIR2 and TEMPDIR2.
27
28RECDIR1=/mnt/f10store/myth/reca
29TEMPDIR1=/mnt/sam1/tempb
30
31RECDIR2=/mnt/sam1/recb
32TEMPDIR2=/mnt/f10store/myth/tempa
33
34#PROJECTX=/path/to/ProjectX.jar (or to a link to it)
35PROJECTX=~/projectx
36
37#PASSWD=`grep "^DBPassword" ~/.mythtv/mysql.txt | cut -d '=' -f 2-`
38PASSWD=mythtv
39
40#################
41
42if test "$1" = "-h" || test "$1" = "--help" ; then
43echo "Usage: ./mythcutprojectx <recording>"
44echo "<recording> is an mpeg2 file recorded by MythTV with a valid DB entry."
45echo "e.g. 1234_20100405123400.mpg in one of the defined RECDIRs"
46echo "The output file replaces the input file which is renamed to <recording>.old"
47exit 0
48fi
49
50# Customize with paths to alternative recording and temp folders
51
52cd $RECDIR1
53TEMP=$TEMPDIR1
54if  [ ! -f "$1" ] ; then
55   cd $RECDIR2
56   TEMP=$TEMPDIR2
57     if  [ ! -f "$1" ] ; then
58       echo " "$1" not found.  Giving up"
59       cd
60       exit 1
61     fi
62fi
63
64if [ $# -lt 3 ]
65then
66   echo "Error: needs three arguments. Running  ffmpeg -i "$1" 2>&1 | grep -C 4 Video "
67   echo 
68   ffmpeg -i "$1" 2>&1 | grep -C 4 Video
69   echo
70   echo "Expected Command Line is of the form   ./mythcutprojectx 1234_20070927190000.mpg  0xvvv 0xaaa "
71   echo "                                                         filename_in_DB           vPID  aPID  "
72   cd ~
73   exit 1
74fi
75
76# chanid and starttime identify the recording in the DB
77chanid=`echo "select chanid from recorded where basename=\"$1\";" |
78mysql -N -u mythtv  -p$PASSWD mythconverg `
79
80starttime=`echo "select starttime from recorded where basename=\"$1\";" |
81mysql -N -u mythtv  -p$PASSWD mythconverg `
82
83# list0 gives start (cut-in) points but omits initial zero if start of recording is wanted; includes eof.
84list0=`echo "select mark from recordedmarkup
85where chanid=$chanid and starttime='$starttime' and type=0 order by mark;" |
86mysql -N -u mythtv  -p$PASSWD mythconverg `
87 
88# list1 gives start of cut (cut-out points). Has initial zero if start of recording is to be cut
89list1=`echo "select mark from recordedmarkup
90where chanid=$chanid and starttime='$starttime' and type=1 order by mark;" |
91mysql -N -u mythtv  -p$PASSWD mythconverg ` 
92
93echo "CollectionPanel.CutMode=0" > cutlist$$ ;
94for i in $list1 ;
95do
96  if [ $i = "0" ]
97    then 
98    list=`echo "select mark from recordedmarkup
99    where chanid=$chanid and starttime='$starttime' and type in (0,1) order by mark;" |
100    mysql -N -u mythtv  -p$PASSWD mythconverg | tail -n +2 `
101# tail -n +2 drops the initial zero.
102  else
103    echo "0" >> cutlist$$
104    list=`echo "select mark from recordedmarkup
105    where chanid=$chanid and starttime='$starttime' and type in (0,1) order by mark;" |
106    mysql -N -u mythtv  -p$PASSWD mythconverg ` 
107  fi
108#  use only the first element of list1, as a switch.
109  break
110done
111 
112# find the key frame (mark type 9) right before each cut mark,
113# extract the byte offset, write it into the ProjectX cutlist
114for i in $list ;
115do echo "select offset from recordedseek
116  where chanid=$chanid and starttime='$starttime' and type=9 and mark >= $i and mark < ($i + 100)
117  order by offset;" |
118  mysql -N -u mythtv  -p$PASSWD mythconverg | head -n 1
119# for each cycle, head -n 1 yields the first line only.
120done >> cutlist$$
121
122echo "list0"
123echo $list0
124echo
125echo "list1"
126echo $list1
127echo
128echo "list"
129echo $list
130echo
131cat cutlist$$
132echo
133
134#exit
135
136if  [ -f "$1".old ] ; then
137  echo " "$1".old exists.  Giving up"
138  exit 1
139else
140  mv  "$1" "$1".old
141# use ProjectX to de-multiplex selected streams with the created cutlist
142  ionice -c3 java -jar "$PROJECTX" -name tempcut$$ -id $2,$3 -out $TEMP -cut cutlist$$ "$1".old || :
143# and pipe for re-multiplexing to mplex. -f 9 is dvd format without navpacks
144  ionice -c3  mplex -o "$1" -V -f 9 $TEMP/tempcut${$}.m2v $TEMP/tempcut${$}.mp2
145
146# tell mythDB about new filesize and clear myth cutlist
147  FILESIZE=`du -b "$1" | cut -f 1`
148  if [ "${FILESIZE}" -gt 1000000 ]; then
149      echo "running: update recorded set filesize=${FILESIZE} where basename=\"$1\";"
150      echo "update recorded set filesize=${FILESIZE} where basename=\"$1\";" | mysql -u mythtv -p$PASSWD mythconverg
151      echo "filesize has been reset"
152      echo "running: ionice -c3 mythcommflag -f "$1" --clearcutlist"
153      ionice -c3 mythcommflag -f "$1" --clearcutlist
154      echo "cutlist has been cleared"
155  fi
156
157#rebuild seek table
158  echo "running: ionice -c3 mythtranscode --mpeg2 --buildindex --showprogress --chanid "$chanid" --starttime "$starttime""
159  ionice -c3 mythtranscode --mpeg2 --buildindex --showprogress --chanid "$chanid" --starttime "$starttime"
160  echo "seek table has been rebuilt"
161
162  echo "Output file is $1  -  PID streams $2 and $3 copied"
163  echo
164  rm -f "$1".png
165#rm -f $TEMP/tempcut${$}*
166  mv $TEMP/tempcut${$}_log.txt $TEMP/$1_pxlog.txt
167  rm -f $TEMP/tempcut${$}.m2v
168  rm -f $TEMP/tempcut${$}.mp2
169  rm -f cutlist$$
170fi
171
172cd
173exit 0
174