Friday, July 30, 2010
 

Video file conversion to flv, bash script

This time i will share small script, that does automatic conversion of mpg, avi files to flv plus it indexes it and prepares for video streaming. First i will explain how it works.

Couple things you should now about this script.

  1. You should change "/1/flvtool++" tool location. It's Facebook flv indexing tool for adding key indexes. It can be found at http://developers.facebook.com/opensource.php
  2. script find's all files from current shell directory. See cronjob script example at the bottom.
  3. It has these features:
    1. It searches *_convert.flv files and indexes them. Indexed files is renamed to *_done.flv if indexing failes flv file is renamed to *_failed_conversion.flv
    2. It searches for *_toflv.mpg and first converts them to flv and then indexes it.
    3. It searches for *_toflv.avi and first reoncodes it with mencoder (necessary for canopolus encoder, because ffmpeg does not understand it) and then convert's to to flv and finaly indexes it with flvtool++.
    4. While conversion is processing to avoid multiple file conversion at the same time. lock file is created and deleted after successfully conversion.

 

#!/bin/sh
flvToolPath="/1/flvtool++"
mfiles=$(find -mmin +1 -name "*_convert.flv")

for fileReplace in $mfiles
do
$flvToolPath $fileReplace ${fileReplace/convert/done}

if [ -f ${fileReplace/convert/done} ];
then
   rm -f $fileReplace
   chmod 666 ${fileReplace/convert/done}
else
   mv $fileReplace ${fileReplace/convert/failed_conversion}
   chmod 666 ${fileReplace/convert/failed_conversion}
fi

done


echo "Starting mpg to flv conversion"

mfiles=$(find -mmin +1 -name "*_toflv.mpg")

for fileReplace in $mfiles
do

fileLock=${fileReplace/_toflv.mpg/_toflv.lock}

if [ -f $fileLock ];
then

echo "Lock file exists, skipping konversion"

else

touch $fileLock;

/usr/bin/ffmpeg -y -deinterlace -i $fileReplace -s 640x480 -b 900k -r 25 -qmin 2 -qmax 4 -ar 44100 -ab 32 -f flv ${fileReplace/_toflv.mpg/_done.flv}

if [ -f ${fileReplace/_toflv.mpg/_done.flv} ];
then  

   mv $fileReplace ${fileReplace/_toflv.mpg/_original_conversion.mpg}
   
   $flvToolPath ${fileReplace/_toflv.mpg/_done.flv} ${fileReplace/_toflv.mpg/_done_indexed.flv}
   chmod 666 ${fileReplace/_toflv.mpg/_done.flv}
   
   if [ -f ${fileReplace/_toflv.mpg/_done_indexed.flv} ];
   then
      rm -f ${fileReplace/_toflv.mpg/_done.flv}
      chmod 666 ${fileReplace/_toflv.mpg/_done_indexed.flv}
   fi   
   
else
   mv $fileReplace ${fileReplace/_toflv.mpg/_failed_conversion.mpg}
   chmod 666 ${fileReplace/_toflv.mpg/_failed_conversion.mpg}
fi

rm -f $fileLock;

fi

done


echo "Starting avi to flv conversion"

mfiles=$(find -mmin +1 -name "*_toflv.avi")

for fileReplace in $mfiles
do

fileLock=${fileReplace/_toflv.avi/_toflv.lock}

if [ -f $fileLock ];
then

echo "Lock file exists, skipping konversion"

else

touch $fileLock;

/usr/bin/mencoder $fileReplace -ovc copy -oac mp3lame -o ${fileReplace/_toflv.avi/_aviready.avi}
/usr/bin/ffmpeg -y -deinterlace -i ${fileReplace/_toflv.avi/_aviready.avi} -s 640x480 -b 900k -r 25 -qmin 2 -qmax 4 -ar 44100 -ab 32 -f flv ${fileReplace/_toflv.avi/_done.flv}

if [ -f ${fileReplace/_toflv.avi/_done.flv} ];
then  

   mv $fileReplace ${fileReplace/_toflv.avi/_original_conversion.avi}
   chmod 666 ${fileReplace/_toflv.avi/_original_conversion.avi}
   
   $flvToolPath ${fileReplace/_toflv.avi/_done.flv} ${fileReplace/_toflv.avi/_done_indexed.flv}
   chmod 666 ${fileReplace/_toflv.avi/_done.flv}
   
   if [ -f ${fileReplace/_toflv.avi/_done_indexed.flv} ];
   then
      rm -f ${fileReplace/_toflv.avi/_aviready.avi}
      rm -f ${fileReplace/_toflv.avi/_done.flv}
      chmod 666 ${fileReplace/_toflv.avi/_done_indexed.flv}
   fi   
   
else
   mv $fileReplace ${fileReplace/_toflv.avi/_failed_conversion.avi}
   chmod 666 ${fileReplace/_toflv.avi/_failed_conversion.avi}
fi

rm -f $fileLock;

fi

done

Cronjob script example

* * * * * cd /var/www/domains/exampledomain/video && ../path/to/addmetatag.sh > /dev/null 2>&1

 

Back »

Comments: 0

Leave a reply »

 
  • Leave a Reply
    Your gravatar
    Your Name
     
     
     
     
     
 
About Remdex site

Simple site for simple peoples.

Get in touch

E-mail: remdex@gmail.com