#!/bin/bash # Written by: Roger Heflin (rahmrh@wildblue.net) # # script to convert a video file into a form that is playable on a # mvpmc box, used inside of MYTHTV to convert HD signals and other # files into forms that will work on the MVPMC box without using VLC # # Version 0.5 8/10/2007 # # Version 0.52 11/05/2007 # Changes: # Added code to use audio bitrate from original # Added code to use audio srate from original # Added code to remove audio preload - this seems to cause mvpmc to be # out of sync on *all* bitrates, not having the preload appears to # make the audio sync be wrong with mplayer - but we aren't using # mplayer for this are we. # Removed extra crap from mencoder sampling code # mplayer -identify only works with mplayer - not with mencoder - not sure # whoever added this option thought it out, and mplayer does not # appear to have a way to redirect to nowhere. # Added -lameopts and removed some lavcopts for audio that were basically # ignored # Changed scale to be in the y rather than the x (640) and use 400 rather # than 480, I found mencoder to sometimes decide to expand to 484 # with 480 as the setting and 484 won't play # on mvpmc # Set a lower audio srate and audio bps as some lower audio rates fail - # ie a srate of 12000 fails to produce any sound. # # Version 0.53 11/18/2007 # Changes: # Added code to rename the files, when the extension is .mpg # Used variables to reduce duplication of options in different locations # Change the default sws option from default to 0. (def=31.2fps/0=37.8fps) # on the conversion of video. # Added code to print out cpu time and cpu percent used and to produce # a speed summary line. # Added more complicated code for scaling things to get to the biggest # size, 400 fails with some videos resulting in too large for # the mvpmc to play, and using 640 results in the y being too # big sometimes. # # Version 0.54 12/06/2007 # Changes: # On 1920x1080 video use -sws 2 (this is interlaced I believe) # otherwise you get some annoying artifacts, this may # need to be used on more than just that. Not sure that # I understand this, only some programming on one of my # channels have these artifacts, it does not appear to be # releated to bit rate, and I cannot identify the difference # between the two programs. # # Version 0.55 1/9/2008 # Changes: # Corrected reporting of 0 size on missing filename # Changed debug to be > 0 rather than =0. # # Version 0.56 1/11/2008 # Changes: # Added code to detect 528x480 mpeg file with ac3 (from qam cable # I believe, this also claims to have a bps of 15000 but # actually appears to really have 3000 bps that will actually # play on a mvpmc - reported by Mark Paulus. # Added code to catch any > 10000 bps and lower the bit rate. # Corrected a later if statement that did not have both possible mp3lib # strings that could be returned. # # Version 0.57 1/18/2008 # Changes: # Added nice to mencoder to attempt to prevent slowness with # backend operations # Increased crop frames to get past small regions with different # crop - we may eventually have to do the entire show or # somehow sample the show and figure out from a number # of samples what the crop should be. # Version 0.58 1/30/2008 # Changes: # Added code to deal with clips with no audio in them. # # # How to add as a mythtv user job: # # mythtv-setup -> general -> screen with user jobs # Add to a user job this: # "/usr/local/bin/makempeg2 mythjob %DIR% %FILE% >> /tmp/makempeg2_out.log" # # You will need to enable user job and set it up to actually run on # new recordings. # To test: # Watch Recordings -> Right Arrow -> Job Options -> select run user job # The job should start in a minute or two. # echo $* >> /tmp/makempeg2.log export DEBUG=1 export srate=48000 export abitrate=320 if [ "${1}" == "mythjob" ] ; then export filename="${2}/${3}" cd ${2} export mythjob=1 else export mythjob=0 export filename=${1} fi # if [ ! -s "$filename" ] ; then echo "${0}: $filename has 0 size" exit -1 fi if [ -L "$filename" ] ; then echo "${0}: $filename is a link - this program will not copy and replace properly with a link" exit -1 fi # # sample in several places # -ovc copy fails to check for audio with DX50 file ... but appears to be fine with # everything else # # -vf scale=720:480 -vf expand=576:432 if [ -f "$filename" ] ; then # this really should work in bash - it is documented but does not actually appear to work ext=`echo "${filename}" | awk '{ NF=split($0,parts,".") ;print parts[NF] }'` echo "Ext = ${ext}" obase=`echo "${filename}" | awk '{ NF=split($0,parts,".") ;for (i=1;i&1 > ${TMPFILE} else echo "Usage: ${0} " echo " " exit -1 fi export oldfile="$filename" export newfile="${filename}.tmp" if [ $DEBUG -gt 0 ] ; then echo "${0}: Oldfile= $oldfile newfile= $newfile" fi # # success: format: 0 data: 0x0 - 0x5ae43e0c #TS file format detected. #DEMUX OPEN, AUDIO_ID: -1, VIDEO_ID: -1, SUBTITLE_ID: -1, #PROBING UP TO 2000000, PROG: 0 #VIDEO MPEG2(pid=49)AUDIO A52(pid=52) NO SUBS (yet)! PROGRAM N. 1 #Opened TS demuxer, audio: 2000(pid 52), video: 10000002(pid 49)...POS=564 #VIDEO: MPEG2 1920x1080 (aspect 3) 29.970 fps 65000.0 kbps (8125.0 kbyte/s) #[V] filefmt:29 fourcc:0x10000002 size:1920x1080 fps:29.97 ftime:=0.0334 #Opening audio decoder: [liba52] AC3 decoding with liba52 #AC3: 2.0 (stereo) 48000 Hz 384.0 kbit/s #AUDIO: 48000 Hz, 2 ch, s16le, 384.0 kbit/25.00% (ratio: 48000->192000) #Selected audio codec: [a52] afm:liba52 (AC3-liba52) export vtype=`cat ${TMPFILE} | awk '$1 ~ "VIDEO:" { print $2}'` export vbps=`cat ${TMPFILE} | awk '$1 ~ "VIDEO:" { for (i=1;i cropmaxx ) cropmaxx=crop[2] ; if ( crop[3] > cropmaxy ) cropmaxy=crop[3] ; if (crop[4] < cropminx ) cropminx=crop[4] ; if (crop[5] < cropminy ) cropminy=crop[5]}}} END { if ((cropmaxx != 0) && ( cropmaxy != 0) ) printf("crop=%d:%d:%d:%d,",cropmaxx,cropmaxy,cropminx,cropminy)}'` if [ "${crop}" == "" ] ; then echo "No crop found - using full size" export crop="crop=${hsize}:${vsize}:0:0," fi if [ $? != 0 ] ; then exit fi if [ $DEBUG -gt 0 ] ; then #echo "START >>>>>>" #cat ${TMPFILE}* #echo "<<<<<< tvsize ){ factor=tvsize/vsize ; nhsize=factor*hsize ; nvsize=tvsize } ; printf("scale=%d:%d",nhsize,nvsize)}'` if [ $? != 0 ] ; then exit fi if [ ${DEBUG} -gt 0 ] ; then echo "${0}: scale = ${SCALE}" fi export EXPAND="expand=640:480" if [ $hsize == 1920 ] && [ $vsize == 1080 ] ; then export CONVERSION="1920x1080 convertion selected" # this is interlaced video - sws 0 produces annoying artificats export SWS="-sws 2" export VIDOPTS="-ovc lavc -vf ${crop}${SCALE},${EXPAND},harddup -lavcopts vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}" elif [ $hsize == 1280 ] && [ $vsize == 720 ] ; then export CONVERSION="1280x720 convertion selected" export VIDOPTS="-ovc lavc -vf ${crop}${SCALE},${EXPAND},harddup -lavcopts vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}" elif ( ( [ $hsize == 704 ] && [ $vsize == 480 ] ) || ( [ $hsize == 720 ] && [ $vsize == 480 ] ) || ( [ $hsize == 528 ] && [ $vsize == 480 ] ) || ( [ $hsize == 640 ] && [ $vsize == 480 ] ) ) && ( [ "$vtype" == "MPEG1" ] || [ "$vtype" == "MPEG2" ] ) ; then if [ "${atype}" != "(mp3lib" ] && [ "${atype}" != "mp3lib" ] ; then if [ $vbps -gt 10000 ] ; then echo "${0}: Warning this files variable bps is listed as ${vbps} and my be too high" echo "${0}: to play on the mvpmc - the actual rate may not be this rate" fi echo "${0}: Audio is $atype and will not work - just transcoding the audio" export CONVERSION="Audio is ${atype} and will not work - just trancoding the audio" export VIDOPTS="-ovc copy" else echo "${0}: This movie should play on the MVPMC - no transcoding needed" echo " " exit 0 fi elif [ $hsize == 320 ] && [ $vsize == 240 ] && [ "$vtype" != "MPEG2" ] ; then # plays as postage stamp size # rescale to something that will play abps=`expr ${abps} / 4` mbps=`expr ${mbps} / 4` export CONVERSION="320x240 non-mpeg2 to mpeg2 conversion selected" export VIDOPTS="-ovc lavc -vf ${crop}scale=320:240,harddup -lavcopts vcodec=mpeg2video:acodec=mp3:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}" elif [ $vbps -gt 10000 ] ; then echo "${0}: This file has a bit rate of $vbps and is to high to play on MVPMC - transcoding video to lower rate" export CONVERSION="bps to high convertion selected" export VIDOPTS="-ovc lavc -vf ${crop}${SCALE},${EXPAND},harddup -lavcopts vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}" elif [ "$vtype" != "MPEG1" ] && [ "$vtype" != "MPEG2" ] ; then echo "${0}: This file is not MPEG1 or MPEG2 - transcoding" export CONVERSION="non-mpeg2/mpeg1 to mpeg2 conversion selected" abps=`expr ${abps} / 2` mbps=`expr ${mbps} / 2` if [ $hsize -eq 480 ] && [ $vsize -eq 480 ] ; then export mbps=`expr ${vbps} \* 2` export VIDOPTS="-ovc lavc -vf ${crop}scale=640:480,harddup -lavcopts vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${vbps}" else export VIDOPTS="-ovc lavc -vf ${crop}${SCALE},${EXPAND},harddup -lavcopts vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}" fi elif [ "$atype" != "(mp3lib" ] && [ "$atype" != "mp3lib" ] ; then # # there is probably an audio bps limit - I have not yet seen a failure around it so won't # worry about the audio bps at this time # echo "${0}: This file does not use mp3lib audio and will not play correctly on audio on MVPMC (ac3 will play but be out of sync)" exit 0 elif ( [ $hsize == 352 ] && [ $vsize == 240 ] ) || ( [ $hsize == 480 ] && [ $vsize == 480 ] ) || ( [ $hsize == 528 ] && [ $vsize == 480 ] ) || ( [ $hsize == 704 ] && [ $vsize == 480 ] ) || ( [ $hsize == 720 ] && [ $vsize == 480 ] ) ; then echo "${0}: This video file is believed to work on MVPMC correctly - no changes needed" exit 0 else echo "${0}: Unknown MPEG1/MPEG2 with mp3 audio $hsize x $vsize it may or may not play correctly" exit 0 fi echo ${0}: $CONVERSION echo "${0}: Command line: " echo "${0}: ${MENCODEROPTS} ${VIDOPTS} ${AUDIOOPTS} ${AUDIOEXTRA} ${filename} -o ${filename}.mpg" /usr/bin/time --verbose -o ${TMPFILE}.time ${MENCODEROPTS} ${SWS} ${VIDOPTS} ${AUDIOOPTS} ${AUDIOEXTRA} "${filename}" -o "${filename}.mpg" 2>&1 | tee ${TMPFILE}.transcode if [ $? == 0 ] && [ -s "${filename}.mpg" ] ; then # only rename to original if original extension is .mpg or .mpeg if [ "${ext}" = "mpg" ] || [ "${ext}" = "mpeg" ] || [ "${ext}" = "MPG" ] || [ "${ext}" = "MPEG" ] ; then echo "${0}: Extension is a mpeg type extension - renaming " if [ -f "${filename}.orig" ] ; then echo "${0}: File ${filename}.orig already exists renaming original ${filename}.orig to .2" if [ -f "${filename}.orig.2" ] ; then mv "${filename}.orig.2" "${filename}.orig.3" fi mv "${filename}.orig" "${filename}.orig.2" fi mv "${filename}" "${filename}.orig" mv "${filename}.mpg" "${filename}" if [ ${mythjob} -eq 1 ] ; then echo "${0}: Running mythcommflag" nice -n 10 mythcommflag -f "${filename}" fi else echo "${0}: Original extension was not a mpeg extension" if [ -f "${obase}.mpg" ] ; then echo "${0}: File ${obase}.mpg already exists - not using" else echo "${0}: Renaming ${filename}.mpg to ${obase}.mpg" mv "${filename}.mpg" "${obase}.mpg" fi fi fi ls -l ${TMPFILE}.time ${TMPFILE}.transcode "${obase}"* CPUpercent=`grep "Percent of CPU" ${TMPFILE}.time | awk '{print $7}'` ElapsedTime=`grep "Elapsed " ${TMPFILE}.time | awk '{print $8}'` Vbps=`grep "Video stream" ${TMPFILE}.transcode | awk '{print $3}'` Abps=`grep "Audio stream" ${TMPFILE}.transcode | awk '{print $3}'` Vsize=`grep "Video stream" ${TMPFILE}.transcode | awk '{print $8}'` Vtime=`grep "Video stream" ${TMPFILE}.transcode | awk '{print $10}'` logger -p info -t transcode "Transcoded ${filename} (${hsize}x${vsize}-${Vtime} sec) in ${ElapsedTime} with ${CPUpercent} CPU to mvpmc with vbps=${Vbps}Kbps and abps=${Abps}Kbps" echo "${0}: Transcoded ${filename} (${hsize}x${vsize}-${Vtime} sec) in ${ElapsedTime} with ${CPUpercent} CPU to mvpmc with vbps=${Vbps}Kbps and abps=${Abps}Kbps" date >> /tmp/makempeg2.log echo "${0}: Transcoded ${filename} (${hsize}x${vsize}-${Vtime} sec) in ${ElapsedTime} with ${CPUpercent} CPU to mvpmc with vbps=${Vbps}Kbps and abps=${Abps}Kbps" >> /tmp/makempeg2.log