Here is an update transcode script that changes the following, this script attempts to setup a proper transcode command line to produce
a .mpg file that will play properly on mvpmc.

Version 0.51  11/09/2007
Changes:
Added code to use audio bitrate from original when possible
Added code to use audio srate from original when possible
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 lines
Renames .mpg audio only transcode files to the original filename and
     renames the original to $1.orig.
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, so cannot be
     easily used.
Added -lameopts and removed some lavcopts for audio that were basically
     being 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
     when using x (640) and autoaspect as the setting and 484 won't play
     on mvpmc, this was on some different sized videos.
Set a lower audio srate and audio bps as some lower audio rates fail -
     ie a srate of 12000 fails to produce any sound, other rates might
     work but only basic testing was done to conform that 22050 worked.

#!/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.51  11/09/2007 
# Changes:
#     Added code to use audio bitrate from original when possible
#     Added code to use audio srate from original when possible
#     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 lines
#     Renames .mpg audio only transcode files to the original filename and
#          renames the original to $1.orig.
#     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, so cannot be
#          easily used.
#     Added -lameopts and removed some lavcopts for audio that were basically
#          being 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 
#          when using x (640) and autoaspect as the setting and 484 won't play
#          on mvpmc, this was on some different sized videos.
#     Set a lower audio srate and audio bps as some lower audio rates fail -
#          ie a srate of 12000 fails to produce any sound, other rates might
#          work but only basic testing was done to conform that 22050 worked.
#
# Version 0.5   8/10/2007
#     Original script.
#
export DEBUG=1
export srate=48000
export abitrate=320

# -vf sacle=720:480 -vf expand=576:432
if [ ! -s "$1" ] ; then
  echo "$1 has 0 size"
  exit 0
fi
if [ -L "$1" ] ; then
  echo "$1 is a link - this program will not copy and replace properly with a 
link"
  exit 0
fi
if [ -f "$1" ] ; then
  export TMPFILE=`/bin/mktemp`
  if [ $DEBUG == 1 ] ; then
    echo "Temp file is $TMPFILE"
  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 5 -vf cropdetect -oac mp3lame -ovc raw -o /dev/null 
$1 2>&1 > ${TMPFILE}
#  mencoder -ss 5 -of mpeg -frames 5 -vf cropdetect -oac copy -ovc raw -o 
/dev/null $1 2>&1 > ${TMPFILE}.1
#  mencoder -ss 10 -of mpeg -frames 5 -vf cropdetect -oac copy -ovc raw -o 
/dev/null $1 2>&1 > ${TMPFILE}.2
#  mencoder -ss 15 -of mpeg -frames 5 -vf cropdetect -oac copy -ovc raw -o 
/dev/null $1 2>&1 > ${TMPFILE}.3
else
  echo "Usage: ${0} <videofiletoconvert>"
  echo " "
  exit -1
fi

export oldfile=$1
export newfile=${1}.tmp
if [ $DEBUG == 1 ] ; then
  echo "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<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 }'`

#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 "ABITRATE = ${abitrate} TMPFILE is ${TMPFILE}"
echo " "
#cat ${TMPFILE}

if [ ${srate} -lt 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 "abitrate is ${abitrate} limited to 320kbit/second"
  export abitrate=320
elif [ ${abitrate} -lt 64 ] ; then
  echo "abitrate is ${abitrate} limited to min of 224kbit/second"
  export abitrate=64
fi

if [ -z "$vsize" ] || [ -z "$hsize" ] || [ -z "$atype" ] || [ -z "$vbps" ] ||
  [ -z "$atype" ] || [ -z "$vtype" ] ; then
  cat ${TMPFILE}
  echo "ERROR: file $1 had a empty size or video or audio decoder - or "
  echo "       the information was parsed incorrectly"
  echo "$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) ) then 
printf("crop=%d:%d:%d:%d,",cropmaxx,cropmaxy,cropminx,cropminy)}'`

if [ $DEBUG == 1 ] ; then
  #echo "START >>>>>>"
  #cat ${TMPFILE}*
  #echo "<<<<<<<END"
  #grep -i CROP ${TMPFILE}*
  echo "$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
export abps=`expr ${vbps} \* 640 / ${hsize} \* 480 / ${vsize}`
if [ ${abps} -gt 5500 ] ; then 
  export abps=5500
fi
export mbps=`expr ${abps} \* 2`

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

if [ $hsize == 1920 ] && [ $vsize == 1080 ] ; then

  echo "1920x1080 convertion selected"
  mencoder -of mpeg -mpegopts format=dvd -srate ${srate} -ofps 30000:1001 -oac 
mp3lame -ovc lavc -vf ${crop}scale=-3:400,expand=640:480,harddup -lavcopts 
vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}
  -lameopts cbr:br=${abitrate}:preset=${abitrate} -audio-preload 0.0 ${1} -o 
${1}.mpg

elif [ $hsize == 1280 ] && [ $vsize == 720 ] ; then

  echo "1280x720 conversion selected"
  mencoder -of mpeg -mpegopts format=dvd -srate ${srate} -ofps 30000:1001 -oac 
mp3lame -ovc lavc -vf ${crop}scale=-3:400,expand=640:480,harddup -lavcopts 
vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}
  -lameopts cbr:br=${abitrate}:preset=${abitrate} -audio-preload 0.0  ${1} -o 
${1}.mpg


elif ( ( [ $hsize == 704 ] && [ $vsize == 480 ] ) ||
       ( [ $hsize == 640 ] && [ $vsize == 480 ] ) || 
       ( [ $hsize == 720 ] && [ $vsize == 480 ] ) ) &&
       ( [  "$vtype" == "MPEG1" ] || [ "$vtype" == "MPEG2" ] ) ; then

    if [ "$atype" != "(mp3lib" ] ; then
      echo "Audio is $atype and will not work - just transcoding the audio"
      mencoder -of mpeg -mpegopts format=dvd -srate ${srate} -ofps 30000:1001 
-oac mp3lame  -lameopts cbr:br=${abitrate}:preset=${abitrate} -ovc copy  
-audio-preload 0.0 ${1} -o ${1}.mpg && mv -i ${1} ${1}.orig && mv ${1}.mpg ${1}
    else
      echo "This movie should play on the MVPMC"
    fi
elif [ $hsize == 320 ] && [ $vsize == 240 ] && [ "$vtype" == "MPEG1" ] ; then 
    # plays as postage stamp size
    # rescale to something that will play
    echo "320x240 conversion selected" 
    abps=`expr ${abps} / 2`
    mbps=`expr ${mbps} / 2`
    mencoder -of mpeg -mpegopts format=dvd -srate ${srate} -ofps 30000:1001 
-oac mp3lame -ovc lavc -vf ${crop}scale=320:240,harddup -lavcopts 
vcodec=mpeg2video:acodec=mp3:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}
  -lameopts cbr:br=${abitrate}:preset=${abitrate} -audio-preload 0.0 ${1} -o 
${1}.mpg

elif [ $vbps -gt 10000 ] ; then
  echo "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 "This file is not MPEG1 or MPEG2 - transcoding"
echo "  mencoder -of mpeg -mpegopts format=dvd -srate ${srate} -ofps 30000:1001 
-oac mp3lame -ovc lavc -vf ${crop}scale=-3:400,expand=640:476,harddup -lavcopts 
vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}
  -lameopts cbr:br=${abitrate}:preset=${abitrate} -audio-preload 0.0 ${1} -o 
${1}.mpg "
  mencoder -of mpeg -mpegopts format=dvd -srate ${srate} -ofps 30000:1001 -oac 
mp3lame -ovc lavc -vf ${crop}scale=-3:400,expand=640:476,harddup -lavcopts 
vcodec=mpeg2video:keyint=18:vrc_buf_size=1835:vrc_maxrate=${mbps}:vbitrate=${abps}
  -lameopts cbr:br=${abitrate}:preset=${abitrate} -audio-preload 0.0 ${1} -o 
${1}.mpg
  exit 0
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 "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 "This video file is believed to work on MVPMC correctly - no changes 
needed"
  exit 0
else

  echo "Unknown MPEG1/MPEG2 with mp3 audio $hsize x $vsize it may or may not 
play correctly"
  exit 0
fi

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Mvpmc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mvpmc-users
mvpmc wiki: http://mvpmc.wikispaces.com/

Reply via email to