MKV scripts

#!/bin/bash
#Encode DVD in current directory to h.264 mkv
tmpprefix="./i"
 
#Locate DVD image/files
shopt -s nullglob # enable
files=(*.{iso,img,mdf,ISO,IMG,MDF})
if [[ "$files" != "" ]]; then	 
	echo "Found CD/DVD images file(s) in current directory";
elif [ -d video_ts ]; then
	echo "Found DVD video subdirectory";
	files=video_ts;
elif [ -d VIDEO_TS ]; then
	echo "Found DVD video directory";
	files=VIDEO_TS;
elif [[ -e VTS_01_1.VOB ||  -e *.vob ]]; then
	echo "Found Video Object (VOB) file(s) in current directory";
	files="";
else 
	echo "No movies found. Doing nothing";
	exit 1;
fi
files="$PWD/$files"
echo "Using location $files"
shopt -u nullglob # disable
 
#Selecting title (the longest)
echo "Selecting the longest title using lsdvd. You will see some errors."
lsdvdinfo="${tmpprefix}lsdvd"
lsdvd "$files" > "$lsdvdinfo"
longest=`grep ongest "$lsdvdinfo" | sed 's/^.*: \([0-9]*\).*/\1/'`
longest=$(( $longest + 0 )) #Deleting leading zeroes
echo "Longest track on DVD is $longest"
 
#getting chapters
chaptersinfo="${tmpprefix}chapters"
dvdxchap -t $longest "$files" > "$chaptersinfo"
 
#Get crop settings
cropinfo="${tmpprefix}crop"
echo "Get crop settings. Once again don't worry about errors"
mplayer -dvd-device "$files" dvd://$longest -ss 300 -endpos 5 -nosound -vo null -vf cropdetect > "$cropinfo" 
crop=`sed -n '/[CROP]/p' "$cropinfo" | sed '$!d' | sed 's/.*(\(.*\))./\1/'`
echo "Crop settings: $crop"
vf="$crop,pullup,softskip,harddup "
echo "Complete video filter: $vf"
 
#Get DVD info
#tmpprefix="/tmp/dvd2avi_$$_"
encoderinfo="${tmpprefix}encoder"
echo "Collecting information about this DVD. Dont worry about errors..."
mencoder -dvd-device "$files" dvd://$longest -v -o /dev/null -nosound > "$encoderinfo"
 
 
#Get audio stream ID. Trying nl, then en, then first stream
echo "Available audio streams:"
grep "audio stream" "$encoderinfo"
stream=`grep "audio stream.*language: nl" "$encoderinfo"`
alang="dut"
if [[ "$stream" = "" ]]; then
    stream=`grep "audio stream.*language: en" "$encoderinfo"`;
    alang="eng"
    if [[ "$stream" = "" ]]; then
        stream=`grep "audio stream" "$encoderinfo"`;
        alang=""
    fi
fi	
aid=`echo "$stream" | sed -n 's/^.*aid: \([0-9]*\)\./\1/;1p'`
echo "Using audio stream ID $aid"
 
echo "Determining optimal bitrate"
#                 $videobitrate * 1000       
#         $bpp = -----------------------
#                $width * $height * $fps
echo "$crop" | sed 's/-vf crop=\([0-9]*\):\([0-9]*\):\([0-9]*\):\([0-9]*\)/\1 \2 \3 \4/' 
read width height down right <<<`echo "$crop" | sed 's/-vf crop=\([0-9]*\):\([0-9]*\):\([0-9]*\):\([0-9]*\)/\1 \2 \3 \4/'` 
fps=`grep "fps:" "$encoderinfo" | sed 's/^.*fps:\([0-9.]*\).*/\1/'` 
echo "Fps=$fps, width=$width, height=$height, down=$down, right=$right"
bitrate=`echo " scale = 0; i = 0.15 * $width * $height * $fps / 1000 ; 100 + i - i % 100 " | bc -l`
echo "Suggested bitrate for getting bpp=0.15+: $bitrate"
#It is suggested that bpd (bits per diagonal) might be a better measure
bpd=`echo " d=e(l($width^2+$height^2)/2) ; ($bitrate * 1000)/(d * $fps)" | bc -l`
echo "Corresponding bpd=$bpd"; 
bpd=65               
diagonal=`echo "e(l($width^2+$height^2)/2)" | bc -l`
bitrate=`echo "scale = 0; i = $bpd * $diagonal * $fps / 1000 ; 100 + i - i % 100 " | bc -l`
echo "Suggested bitrate for getting bpd=$bpd+: $bitrate"
bpp=`echo "($bitrate * 1000)/($width * $height * $fps)" | bc -l`
echo "Corresponding bpp=$bpp"
 
 
#Create conversion script
echo "Constructing job script"
videocodec="-ovc x264 -x264encopts bitrate=$bitrate:subq=5:8x8dct:frameref=3:bframes=3:b_pyramid=normal:weight_b:qcomp=0.7:threads=auto"
audiocodec="-oac copy"
 
 
echo "#!/bin/bash" > job
echo "nice -n 15 mencoder -dvd-device \"$files\" dvd://$longest -aid $aid -oac copy -ovc copy -o movie.avi" >> job
echo "nice -n 15 mencoder movie.avi $audiocodec $videocodec:turbo=1:pass=1 $vf -o /dev/null" >> job
echo "nice -n 15 mencoder movie.avi $audiocodec $videocodec:pass=2 $vf -o movie2.avi" >> job 
chmod a+x job
echo "Created job script" 
 
echo "Creating script for extracting additional audio streams"
echo "#!/bin/bash" > audio
echo "(nice -n 15 mencoder -dvd-device \"$files\" dvd://$longest  -of rawaudio -aid \$1 $audiocodec  -ovc copy -o audio\$1.mp3 > audio\$1.log 2>&1) &" >> audio
chmod a+x audio
echo "Created audio script. Pass audio straam# as argument." 
 
echo "Getting subtitle info"
allsubs=`grep "( sid )" iencoder | sed 's/^.*): \([0-9]*\).*/ \1/' | sed -E -e :a -e '$!N; s/\n//g; ta'`
nlsubs=`grep "( sid )" iencoder | grep "nl" | sed 's/^.*): \([0-9]*\).*/\1/'` 
echo "Available subtitles (sid): $allsubs ($nlsubs is nederlands)"
echo "#!/bin/bash" > subs
echo "for i in $allsubs" >> subs
echo "do" >> subs
echo "  nice -n 15 mencoder -dvd-device \"$files\" dvd://$longest -o /dev/null -nosound -ovc frameno -vobsubout subs -vobsuboutindex \$i -sid \$i" >> subs
echo "done" >> subs
chmod a+x subs
echo "Created subtitle rip script"
 
echo "Creating merge to matroska (mkv) script"
echo "#!/bin/bash" > merge
echo "mkvmerge --title \"Hier komt de titel\" \\" >> merge
echo "  -o \"Titel (jaar).mkv\" \\" >> merge
echo "  --chapters $chaptersinfo \\" >> merge
echo "  --language 1:$alang movie2.avi \\" >> merge
echo "  --language 0:dut subs.srt"  >> merge
echo "#  --default-track $nlsubs:1 subs.idx"  >> merge
chmod a+x merge
echo "Created matroska merge script"
/share/Web/dokuwiki/data/pages/beheer/mkv-scripts.txt ยท Laatst gewijzigd: 2013/12/23 13:45 door pieter
CC Attribution-Share Alike 4.0 International
Powered by PHP Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0 Valid HTML5