Many changes including:

# 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 - reported by tom
#       Changed debug to be > 0 rather than =0.- reported by tom
#       Added program name to all messages - reported by tom
#!/bin/bash
# Written by: Roger Heflin ([EMAIL PROTECTED])
#
# 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.
#       
#
# 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

# -vf sacle=720:480 -vf expand=576:432
if [ -f "$filename" ] ; then
  export TMPFILE=`/bin/mktemp`
  if [ $DEBUG -gt 0 ] ; then
    echo "${0}: Temp file is $TMPFILE"
  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
#

  mencoder -of mpeg -frames 1200 -vf cropdetect -oac mp3lame -ovc raw -o 
/dev/null "$filename" 2>&1 > ${TMPFILE}

else
  echo "Usage: ${0} <videofiletoconvert>"
  echo " "
  exit -1
fi

export oldfile="$filename"
export newfile="${filename}.tmp"
if [ $DEBUG -gt 0 ] ; then
  echo "${0}: Oldfile= $oldfile   newfile= $newfile"
fi

# 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<NF;i++) 
printf("%s",parts[i]) }'`
echo "Base = ${obase}"

#
# 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<NF;i++) if ($i ~ 
"kbps" ) printf("%6.0f", $(i-1))}'`
export size=`cat ${TMPFILE} | awk '$4 ~ "size:" { print $4}'`
export hsize=`echo $size | awk -F: '{split($2,parts,"x");print parts[1]}'`
export vsize=`echo $size | awk -F: '{split($2,parts,"x");print parts[2]}'`
export atype=`cat ${TMPFILE} | awk '$1 ~ "Selected" && $2 ~ "audio" { print $6 
}'`
export abitrate=`cat ${TMPFILE} | awk '$1 ~ "AUDIO:" { printf("%d",$7) }'`
export srate=`cat ${TMPFILE} | awk '$1 ~ "AUDIO:" { print $2 }'`
export fps=`cat ${TMPFILE} | awk '$1 ~ "VIDEO:" && $6 ~ "fps" { print $5 } $1 ~ 
"VIDEO:" && $7 ~ "fps" { print $6} '`

export SWS="-sws 0"
if [ ${hsize} -eq 1920 ] && [ ${vsize} -eq 1080 ] ; then
  echo "${0}: Interlaced video - using default sws of 2"
fi

#if [ "$vtype" == "[XVID]" ] || [ "$vtype" == "[DX50]" ]  ; then 
#
# in both cases there is no separate "audio" to get the bit rate of
#
#  export atype=$vtype
#  export abitrate=320
# line looks like this for [DX50]
# audiocodec: framecopy (format=55 chans=2 rate=48000 bits=0 B/s=16000 sample-1)
#  export srate=`cat ${TMPFILE} | awk '$1 ~ "audiocodec:" { 
split($5,parts,"=");print parts[2] }'`
#  echo "SRATE=${srate}"
#fi
echo " "
echo "${0}: ABITRATE = ${abitrate} TMPFILE is ${TMPFILE}"
echo " "
#cat ${TMPFILE}

if [ ${srate} -le 22050 ] ;  then
# certain audio rates seem to fail.
# ie 12000 fails either with VLC or transcoded, so set
# a lower limit.   Lower limits may work, and/or only certain
# ones may not work - not enough testing has been done to determine
# which work and which fail.
  export srate=22050
  export abitrate=128
fi

if [ ${abitrate} -gt 320 ] ; then
#
# lame limits to 320
#
  echo "${0}: abitrate is ${abitrate} limited to 320kbit/second"
  export abitrate=320
elif [ ${abitrate} -lt 64 ] ; then
  echo "${0}: abitrate is ${abitrate} limited to min of 64kbit/second"
  export abitrate=64
fi

if [ -z "$vsize" ] || [ -z "$hsize" ] || [ -z "$atype" ] || [ -z "$vbps" ] ||
  [ -z "$atype" ] || [ -z "$vtype" ] ; then
  cat ${TMPFILE}
  echo "${0}: ERROR: file $filename had a empty size or video or audio decoder 
- or "
  echo "${0}:       the information was parsed incorrectly"
  echo "${0}: $size is $hsize x $vsize with vtype = $vtype at $vbps bps and 
atype $atype with $crop"
  exit -1
fi
export crop=`grep  "Crop area" ${TMPFILE}*  | awk -v hsize=${hsize} -v 
vsize=${vsize} 'BEGIN { cropminx=hsize;cropminy=vsize;cropmaxx=0;cropmaxy=0} 
{for (i=1;i<NF;i++) { if ($i ~ "-vf") split($(i+1),crop,"[=:)]") ; if ( crop[1] 
== "crop" ) { if ( crop[2] > 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 [ $? != 0 ] ; then
  exit
fi
if [ $DEBUG -gt 0 ] ; then
  #echo "START >>>>>>"
  #cat ${TMPFILE}*
  #echo "<<<<<<<END"
  #grep -i CROP ${TMPFILE}*
  echo "${0}: $size is $hsize x $vsize with vtype = $vtype at $vbps bps and 
atype $atype with $crop"
fi

if [ ${vbps} -gt 20000 ] ; then 
  # ATSC is limited to about 20Mbps
  export vbps=20000
fi
# Cut the bps down by replacing 480 with 360 to reduece by 75%, 
# a factor of 2 causes some annoying artifacts.
export abps=`expr ${vbps} \* 640 / ${hsize} \* 480 / ${vsize} / 2`
if [ "${vtype}" != "MPEG1" ] && [ "${vtype}" != "MPEG2" ] ; then
  export abps=`expr ${abps} \* 6`
fi
if [ ${abps} -gt 5500 ] ; then 
  export abps=5500
fi

if [ ${abps} -eq 0 ] ; then 
  # probably not needed unless doing the / 2 to reduce size
  # this reduces to just above 1GB/hour.
  if [ ${hsize} -eq 848 ] && [ ${vsize} -eq 480 ] ; then
    # video from digital camera
    export abps=3000
  else
    export abps=1500
  fi
fi
export mbps=`expr ${abps} \* 2`

echo "${0}: New Average bps = ${abps} Max bps = ${mbps}"
echo "${0}: Crop is: ${crop}"
echo "${0}: srate = ${srate} abitrate=${abitrate}"

#rm ${TMPFILE}*

export AUDIOOPTS="-srate ${srate}  -oac mp3lame -lameopts 
cbr:br=${abitrate}:preset=${abitrate}"
export AUDIOPRELOAD="-audio-preload 0.0"
#
# Note that the -ofps is only needed if the original file is at a different 
fps, this may
# need to be adjusted for PAL usage.
#
# 1000.000 seems to come out of wmv files
# 29.970 comes from avi and is not supported for mpeg
#if [ "${fps}" == "29.970" ] || [ "${fps}" == "1000.000" ] || [ "${fps}" == 
"59.940" ] ; then
  export fps="30000:1001"
#fi
echo "${0}: FPS = ${fps}"
export MENCODEROPTS="mencoder -of mpeg -mpegopts format=dvd -ofps ${fps}"


export SCALE=`echo "${crop} 640 480" | awk ' { thsize=$2; tvsize=$3 ; 
split($1,cropd,"=") ; split(cropd[2],croplim,":") ; hsize=croplim[1]-cromlim[3] 
; vsize=croplim[2]-croplim[4]; factor=thsize/hsize ; nhsize=thsize ; 
nvsize=factor*vsize ; if ( nvsize > 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 == 640 ] && [ $vsize == 480 ] ) || 
       ( [ $hsize == 720 ] && [ $vsize == 480 ] ) ) &&
       ( [  "$vtype" == "MPEG1" ] || [ "$vtype" == "MPEG2" ] ) ; then

    if [ "${atype}" != "(mp3lib" ]  && [ "${atype}" != "mp3lib" ] ; then
      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"
  exit 0
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`
  export VIDOPTS="-ovc lavc -vf ${crop}${SCALE},${EXPAND},harddup -lavcopts 
vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}"
 
elif [ "$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 == 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} ${AUDIOPRELOAD} ${filename} 
-o ${filename}.mpg"
/usr/bin/time --verbose -o ${TMPFILE}.time ${MENCODEROPTS} ${SWS} ${VIDOPTS} 
${AUDIOOPTS} ${AUDIOPRELOAD} "${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
      mythcommflag -f "${filename}"
    fi
  else
    if [ -f "${obase}.mpg" ] ; then
      echo "${0}: File ${obase}.mpg already exists - not using"
    else
      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
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Mvpmc-users mailing list
Mvpmc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mvpmc-users
mvpmc wiki: http://mvpmc.wikispaces.com/

Reply via email to