Script ffmpeg.sh

From campisano.org
Jump to navigation Jump to search
#!/bin/bash



if [ -z "$1" ] || [ -z "$2" ]; then
    echo	"Usage: $0 <input-file> <output-file> [other-opts]";
    exit -1;
fi;

if [ ! -x "`which ffmpeg`" ]; then
    echo	"Can't find ffmpeg command";
    exit;
fi;

if [ ! -f "$1" ]; then
    echo	"Can't find input file";
    exit;
fi;



OTHEROPTS=""

ARRAY=("$@")
COUNT=2
#while test $COUNT -lt $#; do
while test $COUNT -lt "$#"; do
    OTHEROPTS="$OTHEROPTS ${ARRAY[COUNT]}";
    COUNT=$((COUNT+1));
done;

echo -e "input-file=\t$1"
echo -e "output-file=\t$2"
echo -e "other-opts=\t$OTHEROPTS"

echo



nice -5 ffmpeg -t 1 -i "$1" -f null - &> ___time.log;
TIME=`cat ___time.log | grep Duration | cut -d . -f 1 | cut -d ' ' -f 4`;
if [ $? -ne 0 ]; then
    echo	"qualcosa e' andato storto durante la lettura della durata del filmato";
    exit -1
fi;
rm -f ___time.log;
echo "movie time               :	$TIME";

SECONDS=$(( $(echo $TIME | cut -d : -f 1)*3600 + $(echo $TIME | cut -d : -f 2)*60 +  $(echo $TIME | cut -d : -f 3) ));
if [ $? -ne 0 ]; then
    echo	"qualcosa e' andato storto durante il calcolo della durata in secondi";
    exit -1
fi;
echo "movie time in seconds    :	$SECONDS";

MAXSIZE=700
AUDIOBITRATE=128;
VIDEOBITRATE=$(( ($MAXSIZE*1024*8) /$SECONDS - $AUDIOBITRATE));
# tolgo il 10% per essere sicuro che rientri in $MAXSIZE
VIDEOBITRATE=$(($VIDEOBITRATE- ($VIDEOBITRATE/10)));



echo "defined audio bitrate    :	$AUDIOBITRATE";
echo "extimated vidio bitrate  :	$VIDEOBITRATE";
echo
echo
echo





### Encoding in ONE pass

nice -5 ffmpeg -i "$1" \
-f avi -deinterlace \
-vcodec mpeg4 -b $VIDEOBITRATE \
-acodec mp3 -ar 44100 -ac 1 -ab $AUDIOBITRATE \
$OTHEROPTS \

"$2"





### Encoding in TWO pass

# 1st pass: the statistics of the video are recorded
#nice -5 ffmpeg -i "$1" \
#-f avi -deinterlace \
#-vcodec mpeg4 -b $VIDEOBITRATE \
#-acodec mp3 -ar 44100 -ac 1 -ab $AUDIOBITRATE \
#-pass 1 -passlogfile ___ffmpeg.log \
#$OTHEROPTS \
#___pass1.avi

# 2nd pass: the video is generated at the exact requested bitrate
#nice -5 ffmpeg -i "$1" \
#-f avi -deinterlace \
#-vcodec mpeg4 -b $VIDEOBITRATE \
#-acodec mp3 -ar 44100 -ac 1 -ab $AUDIOBITRATE \
#-pass 2 -passlogfile ___ffmpeg.log \
#$OTHEROPTS \
#"$2"




echo

ls -sh "$2"





#echo test
#ffmpeg -t 10 -v 5 -i "$2" -f null -


#rm -f ___test.avi ___ffmpeg.log

#ffmpeg -i "$1" \
#-acodec mp3 -ar 44100 -ab $AUDIOBITRATE \
#-vcodec mpeg4 -hq -4mv -b $VIDEOBITRATE \
#-pass 1 -passlogfile ___ffmpeg.log \
#___test.avi

##ffmpeg -i "$1" \
##-acodec copy \
##-vcodec mpeg4 \
##-pass 1 -passlogfile ___ffmpeg.log \
##___test.avi

#if [ $? -ne 0 ]; then
#    echo	"qualcosa e' andato storto durante il primo passaggio della codifica!";
#    exit -1
#fi;

#ffmpeg -i "$1" \
#-acodec mp3 -ar 44100 -ab $AUDIOBITRATE \
#-vcodec mpeg4 -hq -4mv -b $VIDEOBITRATE \
#-pass 2 -passlogfile ___ffmpeg.log \
#"$2"




# -s 352x288 \
#-an \
#-acodec mp3 -ar 44100 -ab $AUDIOBITRATE \
#-vcodec mpeg4 -hq -4mv -b $VIDEOBITRATE \
#-deinterlace -async 1 -s 640x460 \
#-croptop 110 -cropbottom 110 -cropleft 6 -cropright 6 \
#-pass 1 -passlogfile ___ffmpeg.log \
# End