[MPlayer-dev-eng] yet another dvd2divx

Stijn De Weirdt stdweird at carl.ugent.be
Tue Jul 27 15:27:15 CEST 2004


hello,

i've written a small dvd2divx script and i'd like some comments from you 
on it. especially the cropping part.
it's written in bash and it's certainly not finished yet, but i'd just 
like to know if i'm heading in the right direction. (i've only just found 
the makedivx.sh by Sebastien Beaugrand and will take a look at it asap)

stijn
-------------- next part --------------
#!/bin/bash
#this is yet another dvd2lavc script. it works for me but use at your own risk
#oh, this is pal-oriented. no interlace stuff (yet)

#the input of this script is the name of a .vob file, 
# being the dvd on your harddisk. 
#you can get this file by uncommenting the following line:
#mencoder dvd://1 -ovc copy -oac copy -o $1
#it is always very good to test this script first by a small sample.
#mencoder dvd://1 -ss xx:yy -endpos z -ovc copy -oac copy -o $1
#where you replace xx:yy with some starttime and z the duration 
#see the manual for more on -ss and -endpos

#this parameter defines your final filesize
end_size=1400 ## in MB (if more than 2000, some old version of bash i have $((...)) overflows :( --> now uses dc, old code available

#audio pass
mencoder -quiet -ovc frameno -oac mp3lame -lameopts preset=medium:vol=3 -o frameno.avi $1 

#mencoder saves the audio in the special audio-only-file frameno.avi
#something about frameno.avi and the size of the video and audio headers
fram_size_text=$(ls -s --block-size=1 frameno.avi)
audio_size=${fram_size_text%frameno.avi}

mv frameno.avi temp_f_file #mencoder & frameno.avi
 
#length of movie/sample in seconds, based on the audio track
info=$(mencoder -v $1 2> /dev/null|grep byte/sec)
avg_byte_sec=${info#*:}
info=$(mencoder -v $1 2> /dev/null|grep "audio size")
tmp=${info##*(}
vob_audio_size=${tmp%)}
abl=$(($vob_audio_size/$avg_byte_sec)) #audio based length


#crop (ugly but works)
#we want to sample some frames for crop detection in sample_time secs for a maximum of max_frames
#maybe this test should be run a few times, just got a -720:-576:720:576 for max_fps=2000
#the sample time isn't the time it takes, but still gives small value (say < 30s)
sample_time=5
max_fps=1000 #computer acts strange for fps=40000 :)

info=$(mencoder -v $1 2> /dev/null|grep "video size")
tmp=${info#*(}
total_number_of_frames=${tmp%%)*}
new_fps1=$(($total_number_of_frames/$sample_time)) 
if (( $new_fps1 < $max_fps));
then new_fps=$new_fps1;
else new_fps=$max_fps;
fi
max_frames=$(($sample_time*$new_fps)) 

#as i understand, mplayer tries to play the movie at an insane fps, fails to do so, drops lots of frames and the remaining
#ones will be examined with cropdetect. if mplayer succeeds, only plays $max_frames.
mplayer -quiet -fps $new_fps -frames $max_frames -framedrop -vf cropdetect $1 -vo null -ac null -ao null 2> /dev/null > crop_info

max_w=0
max_h=0
count=0

#rm crop_info2
for i in $(grep crop= crop_info) #dunno why this doesn't work out of the box :(
  do   echo $i >> crop_info2;
done

for i in $(grep crop= crop_info2)
do
  count=$(($count + 1))
  #We'll now look for maximum over all samples
  crop1=${i#*=}
  crop2=${crop1%)} #gives the aa:bb:cc:dd value
  l_crop=${crop2%:*:*}
  r_crop=${crop2#*:*:}
  aa=${l_crop%:*}
  cc=${r_crop#*:}
  bb=${l_crop#*:}
  dd=${r_crop%:*}

  if [[ $(( $aa  > $max_w)) || $(( $bb > $max_h )) ]]; #new larger crop values (width or height)
  then
      max_w=$aa #i'm going with the old values here
      #thanks to the not-really-arbitrary-precision calculator $((...)) this looks a bit strange :)
      naa=$(($aa/16*16)); #this is the closest lower multiple of 16
      dif=$(($aa - $naa)); #difference from closest lower multiple of 16 
      ncc=$((($cc + $dif/2)/2*2)); #calculate new even offset value

      max_h=$bb
      nbb=$(($bb/16*16));
      dif=$(($bb - $nbb));
      ndd=$((($dd + $dif/2)/2*2));

      crop=$naa:$nbb:$ncc:$ndd;
  fi
done      


#determine video bitrate
#video_bytes=$(($end_size * 1024 * 1024  - $audio_size))
#ivb=$(($video_bytes/$abl/125)) #ideal video bitrate; 1/125=8/1000

echo $end_size" 1024 * 1024 * "$audio_size" - 8 * 1000 / "$abl" / p q" > dc_instr
ivb=$(dc dc_instr) 


echo "***************************************************"
echo "video bitrate= "$ivb
echo "number of frames checked with cropdetect= "$count
echo "found crop: "$crop
echo "***************************************************"

mv temp_f_file frameno.avi #mencoder & frameno.avi

#the lavcopts are a part of the opts found in the MPlayer docs

#1st vid pass
mencoder -quiet -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$ivb:v4mv:mbd=2:trell:cmp=3:subcmp=3:mbcmp=3:autoaspect:vpass=1 -vf crop=$crop -oac copy -o $1.avi $1

#2nd vid pass
mencoder -quiet -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$ivb:v4mv:mbd=2:trell:cmp=3:subcmp=3:mbcmp=3:autoaspect:vpass=2 -vf crop=$crop -oac copy -o $1.avi $1

#remove temp_files
rm -f frameno.avi divx2pass.log dc_instr crop_info crop_info2


More information about the MPlayer-dev-eng mailing list