#!/bin/bash # TODO: # - Progress for VOB files, when we can't get number of frames # - Proper aspect ratio handling - use tcprobe to get input res? #Check for required software... ffmpeg_bin=`which ffmpeg | grep -c "ffmpeg"` ffmpeg_xvid=`ffmpeg -formats | grep -c "EV xvid"` ffmpeg_aac=`ffmpeg -formats | grep -c "DEA aac"` mencoder_bin=`which mencoder | grep -c "mencoder"` transcode_bin=`which transcode | grep -c "transcode"` # Check for ffmpeg if [ $ffmpeg_bin -eq "0" ]; then zenity --error --title="Error - Missing Software" \ --text="You do not have the ffmpeg package installed Please install it in order to use this script. Make sure that the Universe repositories are enabled and then type: 'sudo apt-get install ffmpeg' at a terminal." exit fi #Check for mencoder if [ $mencoder_bin -eq "0" ]; then zenity --error --title="Error - Missing Software" \ --text="You do not have the mencoder package installed Please install it in order to use this script. Make sure that the Multiverse repositories are enabled and then type: 'sudo apt-get install mencoder-586' at a terminal." exit fi #Check for transcode if [ $transcode_bin -eq "0" ]; then zenity --error --title="Error - Missing Software" \ --text="You do not have the transcode package installed Please install it in order to use this script. Make sure that the Multiverse repositories are enabled and then type: 'sudo apt-get install transcode' at a terminal." exit fi #Has a file been selected? if [ $# -eq 0 ]; then zenity --error --title="error" --text="You must select at least 1 file to process" exit 1 fi #Video Encoding Functions... get_frames () { #get video duration and frames total_frames=`tcprobe -i "$movie" 2>&1 |grep "length:" | awk '{gsub(/,/,"");gsub(/:/,"");print $2; fflush();}'` } #Video Encoding Functions... get_input_ar () { #get input aspect ratio ar_x=`tcprobe -i "$movie" 2>&1 | grep "aspect ratio:" | sed 's/^.*\s\([0-9][0-9]*\):\([0-9][0-9]*\).*$/\1/'` ar_y=`tcprobe -i "$movie" 2>&1 | grep "aspect ratio:" | sed 's/^.*\s\([0-9][0-9]*\):\([0-9][0-9]*\).*$/\2/'` } #Video Encoding Functions... get_input_res () { #get input resolution res_x=`tcprobe -i "$movie" 2>&1 | grep "import frame size:" | sed 's/^.*\s\([0-9][0-9]*\)x\([0-9][0-9]*\).*$/\1/'` res_y=`tcprobe -i "$movie" 2>&1 | grep "import frame size:" | sed 's/^.*\s\([0-9][0-9]*\)x\([0-9][0-9]*\).*$/\2/'` } calc_output_res () { # Algorithm here is: # 1) Do we have input aspect ratio. If yes, use that to calculate largest rectangle of that aspect ratio that fits into 320x240 without cropping. # 2) If no Aspect Ratio info, use input resolution's aspect ratio # 3) If we have neither Aspect Ratio, nor input resolution, default to 320x240 tmp_res_x=4 tmp_res_y=3 output_res_x=320 output_res_y=240 if [ -z "$ar_x" -a -z "$ar_y" ] then # We don't have aspect ratio - use resolution if we have it if [ -z "$res_x" -a -z "$res_y" ] then echo "Can't get aspect ratio or input resolution - using 320x240" else # We've got input resolution - use it tmp_res_x=$res_x tmp_res_y=$res_y fi else # We've got Apect Ratio tmp_res_x=$ar_x tmp_res_y=$ar_y fi # We have Aspect Ratio - use it tmp_x=$[$tmp_res_x+3] tmp_y=$[$tmp_res_y+4] echo "tmp_x is $tmp_x" echo "tmp_y is $tmp_y" if [ $tmp_x -eq $tmp_y ] then # If it's already exactly 4:3, just return (output will be 320x240) return; fi # Figure out the biggest box of this aspect ratio that will fit into 320x240 if [ $tmp_x -gt $tmp_y ] then # Apect ratio over 4:3 - it's a wide video # Recalc Y resolution echo "Over 4:3" output_res_y=$[320 * $tmp_res_y / $tmp_res_x] else # Apect ratio under 4:3 - it's 'tall' video # Recalc X resolution echo "Under 4:3" output_res_x=$[240 * $tmp_res_x / $tmp_res_y] fi } mpeg4_encode () { # Replace file extension with AVI out_movie=`echo "$movie" | sed 's/\.\w*$/.avi/'` echo "out_movie1 is $out_movie" # Add D2- in front doesmoviecontainaslash=`echo "$out_movie" | grep -c "/"` if [ $doesmoviecontainaslash -eq "0" ] then # Filename isn't a full path (no / character in it) # Just add D2- in front of whole thing out_movie=D2-$out_movie else out_movie=`echo "$out_movie" | sed 's/\(.*\/\)\(.*\)$/\1D2-\2/'` fi echo "out_movie2 is $out_movie" rm frameno.avi rm divx2pass.log rm stream.yuv rm "$out_movie" echo "# Converting $movie to MPEG4" echo "Total frames: $total_frames" echo "Total movies: $num_files" echo "res_x=$res_x - res_y=$res_y" echo "ar_x=$ar_x - ar_y=$ar_y" echo "Output res: $output_res_x"x"$output_res_y" # Has total_frames been found? if [ -z "$total_frames" ] then echo "Can't estimate progress" # 07/04/2007: Removed -f 25.000, hoping it might restore audio/video sync transcode --export_par 1,1 -H 10 -a 0 -x mplayer -q 1 -i "$movie" -w 700,50 -F mpeg4 -b 128,0,2 -s 1.311 --a52_drc_off -J smartyuv=threshold=10:Blend=1:diffmode=2:highq=1 -Z "$output_res_x"x"$output_res_y" -y ffmpeg -o "$out_movie" --print_status 25 2>&1 | awk -vRS='\n' '$0 ~ /\[[0-9]*-[0-9]*\]/ { gsub(/\[[0-9]*-/,"",$3); gsub(//,"",$3); print; fflush();}' | zenity --width=400 --progress --pulsate --auto-close --title="D2convert - no ETA (File $Video_Count of $num_files)" --text="Processing Video $movie" else echo "We know number of frames - we can estimate progress" transcode --export_par 1,1 -H 10 -a 0 -x mplayer -q 1 -i "$movie" -w 700,50 -F mpeg4 -b 128,0,2 -s 1.311 --a52_drc_off -J smartyuv=threshold=10:Blend=1:diffmode=2:highq=1 -Z "$output_res_x"x"$output_res_y" -y ffmpeg -o "$out_movie" --print_status 25 2>&1 | awk -vRS='\n' '$0 ~ /\[[0-9]*-[0-9]*\]/ { gsub(/\[[0-9]*-/,"",$3); gsub(/\]/,"",$3); print ($3*100)/total_frames; fflush();}' total_frames=$total_frames | zenity --width=400 --progress --percentage=0 --auto-close --title="D2convert - (File $Video_Count of $num_files)" --text="Processing Video $movie" fi } Video_Count=1 num_files=$# while [ $# -gt 0 ]; do total_frames=0 movie=$1 echo "# Processing $video_in_type Video $movie \n Total: $Video_Count" get_frames get_input_res get_input_ar calc_output_res mpeg4_encode let Video_Count=Video_Count+1 shift done