All pastes #802037 Raw Edit

Untitled

public text v1 · immutable
#802037 ·published 2007-11-29 21:39 UTC
rendered paste body
root@mythtv:~# cat /usr/bin/myth720-TV
#!/bin/bash
#
# H.264 encode script for 720p/AC3.  Based on Steve Adeff's excellent
# script from the Myth Wiki.  I used his math but replaced the transcoding
# stuff with ffmpeg to preserve good sync.  I also added a variable
# to define number of processors in order to take advantage of multithreaded
# transcoding.  This speeds my system up fourfold.  I also changed the
# bitrate calculation to make it a bit mroe accurate.
#
## SCRIPT USAGE IN MYTH IS scriptname "%DIR%/%FILE%" "%TITLE%" "%SUBTITLE%"
#
# Define your number of processors/cores here so that I can figure out
# how many threads to encode with.
#
# You need to define all of the below or things may not work right.
#
# Prerequisites for using this script are:
#
# -Working ffmpeg with x.264 support
# -Mencoder/mplayer for timing/framerate/etc
# -A /usr/bin/pc (or somewhere in your path "pc") file.  Just a simple
#  perl calculator.  It's just an executable script that contains:
#
#       #!/usr/bin/perl
#       print eval(join(' ',@ARGV)).qq{
#       };
#

processors=4
resolution=1280x720
mkdir "/home/mythtv/720p/$2"
OUTDIR=/home/mythtv/720p/$2

# Miscellaneous encode options to pass.  Add new options here.
encopts="-coder 1 -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -me_method hex -subq 5 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 1 -i_qfactor 0.71 -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -async 1"


## End of definitions.  Don't change anything after this point unless
## you know what you're doing, or if I screwed something up.

SUBTITLE=$3
overheardbytes=16
server_default=POT
threads=`pc $processors*2-2`

#Functions
#Video Length Calculation
function VideoSizeCalc() {
        audiosize=`pc $audiobitrate/1000*$totalseconds*0.1220703125/1024`
        echo "Audio Size in MB = $audiosize"
        totalseconds=`echo $totalseconds|sed -e 's/\.[0-9]*//'`
        if [ $totalseconds -lt 1800 ]; then
                totalsize=500
        elif [ $totalseconds -lt 3600 ]; then
                totalsize=1050
        elif [ $totalseconds -lt 9000 ]; then   #Here I assume you will want to be more
                totalsize=5000                  #generous if you run it on a movie.
        else
                echo "I don't know how to handle this length!"
                exit 1
        fi
        echo "Total Size = $totalsize"
        videosize=`pc $totalsize-$audiosize`
        videosize=`echo `${videosize:0:3}``
        echo "Videosize = $videosize MB"



}


function FolderNameClean() {
        input=$1
        input=`echo "$input" | sed -e 's/.[Aa][Vv][Ii]//'`
        input=`echo "$input"`
        folder_name="$input"
}

#test for arguements
if [[ -z "@ARGV" ]]; then
        echo "First argument is, in myth syntax, %DIR%/%FILE%"
        echo "Second argument is, in myth syntax, %TITLE%"
        echo "third argument is, in myth syntax, %SUBTITLE%"
        exit
else
        #Reassign variables
        if [[ ! -e $1 ]]; then
                echo "no such input mpg!"
                exit
        else
                original_name=$1
        fi
        folder_name="$2"
        FolderNameClean $2
        file_name=`echo "$folder_name" | tr A-Z a-z`
        # display arguments
        echo "Original mpeg is: $original_name"
        echo "File name will be: $2 - $3.mp4"
fi

#Grab input file information
echo "Determining total file length..."
totalseconds=`mencoder $original_name -ovc copy -nosound -o /dev/null -quiet 2>&1 | awk '/^Video stream:/{print $10+$10/$12}'`
echo "total seconds = $totalseconds"
audiobitrate=`mplayer -vo null -ao null -frames 0 -identify "$original_name" 2>/dev/null | grep "ID_AUDIO_BITRATE" | sed -e 's/ID_AUDIO_BITRATE=0//' | grep "ID_AUDIO_BITRATE" | sed -e 's/ID_AUDIO_BITRATE=//'`
echo "audio bitrate = $audiobitrate"
videowidth=`mplayer -vo null -ao null -frames 0 -identify "$original_name" 2>/dev/null | grep "ID_VIDEO_WIDTH" | sed -e 's/ID_VIDEO_WIDTH=//'`
videoheight=`mplayer -vo null -ao null -frames 0 -identify "$original_name" 2>/dev/null | grep "ID_VIDEO_HEIGHT" | sed -e 's/ID_VIDEO_HEIGHT=//'`
videofps=`mplayer -vo null -ao null -frames 0 -identify "$original_name" 2>/dev/null | grep "ID_VIDEO_FPS" | sed -e 's/ID_VIDEO_FPS=//'`
videofps=`echo `${videofps:0:2}``
echo "FPS: $videofps"
aspect=`pc $videowidth/$videoheight`
aspect=`echo "$aspect" | tr -s [:digit:]`
echo "Aspect Ratio: $aspect"
#Calculate Video size
VideoSizeCalc
sleep 5
videosize=`pc $videosize*1024`
echo "Videosize = $videosize"

BITRATE_CALC_AUDIO=`pc $audiobitrate/8000`
CALC_1=`pc $BITRATE_CALC_AUDIO*$totalseconds`
CALC_2=`pc $videosize`
CALC_3=`pc $CALC_2-$CALC_1`
BITRATE_CALC_FINAL=`pc $CALC_3/$totalseconds`
BITRATE_CALC=`pc $BITRATE_CALC_FINAL/100`
BITRATE_CALC=`echo $BITRATE_CALC | sed 's/^\([^\.]*\...\).*/\1/'`
BITRATE=`echo $BITRATE_CALC`M
echo "Video bitrate will be = $BITRATE"

sleep 5

if [[ -e "$original_name" ]]; then
        echo start `date +%H:%M`>$file_name.runtime
        if [[ "$videofps" -eq "59" ]]; then
                cd "$OUTDIR"
                #
                nice -n 17 ffmpeg -i $original_name -an -pass 1 -vcodec libx264 -threads $threads -s $resolution -b $BITRATE -bt $BITRATE $encopts "$OUTDIR/$file_name.1stpass.mp4"
                #
                nice -n 17 ffmpeg -i $original_name -acodec libfaac -ab $audiobitrate -pass 2 -vcodec libx264 -threads $threads -s $resolution -b $BITRATE -bt $BITRATE $encopts "$OUTDIR/$2 - $SUBTITLE.mp4"
                #
        elif [[ "$videofps" -eq "29" ]]; then
                cd "$OUTDIR"
                #
                nice -n 17 ffmpeg -i $original_name -acodec libfaac -ab $audiobitrate -pass 1 -vcodec libx264 -threads $threads -s $resolution -b $BITRATE -bt $BITRATE $encopts "$OUTDIR/$file_name.1stpass.mp4"
                #
                nice -n 17 ffmpeg -i $original_name -acodec libfaac -ab $audiobitrate -pass 2 -vcodec libx264 -threads $threads -s $resolution -b $BITRATE -bt $BITRATE $encopts "$OUTDIR/$2 - $SUBTITLE.mp4"
                #
                # Cleanup!
                #rm $OUTDIR/$file_name.1stpass.mp4
                #rm $OUTDIR/x264_2pass.log
                #rm $OUTDIR/ffmpeg2pass-0.log
        fi
        echo stop `date +%H:%M`>>$file_name.runtime
        echo "Runtime"
        more $file_name.runtime
        rm $file_name.runtime
fi
exit
root@mythtv:~#