#!/bin/bash
# 28.04.2009
if [ $# -eq 0 ] 
then
  echo "Usage:$0 videofile"
  echo "	Convert an video to mp4."
  echo "	It also resize and  watermark the video"
  echo "	$0 have no option, you need to edit the script. :-)" 
 exit 
fi  
#This script is designed to convert avi files from my wifes Canon DIGITAL IXUS 70 to flv files. 
#The script resize and watermark and sharpen the movie.
#I wrote this one because ffmpeg vhook now longer works for watermarking.

#Cut the file to the proper length before running this
#I found out that "avisplit -t 00:00:22-00:00:33 -i mvi_0196.avi  -o 10sek.avi"
#is a nice way to do this
#
#REQUIRED TOOLS
#This are required tools on ubuntu jaunty.
#mencoder and avidemux-cli lives in the multiverse repo
#sudo apt-get install ffmpeg mencoder imagemagick avidemux-cli
file=$1
fileprefix=$(echo $file|rev|cut -f 2- -d .|rev)
mkdir video2webdir/
#extracting png from avi file
echo "EXTRACTING PNG FILES"
#the -r value can be found with  exiftool -framerate  mvi_0196.avi 
#or mediainfo filename.avi|grep Frame
ffmpeg -i $file -y -r 30 -f image2 video2webdir/images%05d.png

#extracting sound track to a wav file
echo "EXTRACTING SOUND TO WAW FILE"
ffmpeg -i $file -y -acodec pcm_s16le -ar 44100 -ac 2 video2webdir/$fileprefix.wav

#imageprocessing, resize and watermark
#-bordercolor black     -border  0x4 
#adds a border to this  paricular image to be dividable by 16
#must be adjusted
#Add full path to fontfile if it missing from ~/.magick/type.xml
echo "IMAGE PROSESSING, WATERMARKING; RESIZING ....."
mogrify -verbose -filter sinc -resize 480x -unsharp 0.5x0.3+4+0.0  -font FreeSansb -pointsize 14 -draw "gravity NorthWest  fill black  text 20,12 'bikuben.mulebakken.net'" -background white  -bordercolor black     -border  0x4 +matte  video2webdir/*.png


echo "MAKING AVI FROM PNG FILES, AND ADD SOUND"
#This one creates a uncompressed avi file :-)
mencoder   -ovc raw -oac pcm -vf format=yuy2  -mf type=png:fps=30 -o video2webdir/$fileprefix.avi mf://\video2webdir/*.png  -oac pcm -audiofile video2webdir/$fileprefix.wav
#This one compress the avi file
#mencoder   -ovc lavc -mf type=png:fps=30 -o video2webdir/$fileprefix.avi mf://\video2webdir/*.png -oac mp3lame -audiofile video2webdir/$fileprefix.wav


echo "CONVERTING TO MP4 FORMAT FOR WEB"
#Creating avidemuxer script
cat > video2webdir/mp4.js <<- _EOF_
//AD  <- Needed to identify//
//--automatically built--
var app = new Avidemux();

//** Video Codec conf **
app.video.codec("X264","2PASSBITRATE=500","196 00 00 00 00 00 00 00 00 00 00 00 00 28 00 00 00 1e 00 00 00 3c 00 00 00 0a 00 00 00 33 00 00 00 04 00 00 00 01 00 00 00 28 00 00 00 19 00 00 00 fa 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 05 00 00 00 10 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5a 00 00 00 01 00 00 00 64 00 00 00 ");

//** Audio **
app.audio.reset();
app.audio.codec("aac",56,4,"80 00 00 00 ");
app.audio.normalizeMode=0;
app.audio.normalizeValue=0;
app.audio.delay=0;
app.audio.mixer("NONE");
app.audio.scanVBR();
app.setContainer("MP4");
setSuccess(1);
//app.Exit();

//End of script
_EOF_


avidemux2_cli --nogui --run video2webdir/mp4.js --load video2webdir/$fileprefix.avi --save $fileprefix.mp4

echo "creating a splash image for flowplayer"
#preview image for  player
#With a play button
wget -N  http://flowplayer.org/img/player/btn/play_text.png
composite -gravity center play_text.png  video2webdir/images00001.png $fileprefix.png
#More stuff
#http://www.flowplayer.org

#HTML HTML HTML
echo "Syntax for flowplayer"
cat  <<- _EOF_
<script type="text/javascript" src="/wp-content/uploads/flowplayer/example/flowplayer-3.1.0.min.js"></script>
<script type="text/javascript" src="/wp-content/uploads/flowplayer/example/flashembed.js"></script>

<a href="/wp-content/uploads/2009/04/$fileprefix.mp4" style="display:block;width:460;height:398px;" id="player4"><img src="/wp-content/uploads/2009/04/$fileprefix.png"  width="480" height="368" alt="Video" /></a>

_EOF_

#rm video2webdir/ -rf
#totem $fileprefix.mp4

