Linux Articles, Shell Scripts October 8, 2009 0

Convert Videos AVI to MPG

Convert Videos AVI to MPG

#!/bin/sh

# avi2mpgconv.sh - video converter avi2mpg
# Purpose: To convert digital camera's avi videos to mpg format,
# it allows you to convert avi videos to mpg format which is much smaller
# Howto use: avi2mpgconv.sh <videos' directory path>
# assumption: video extensions are *.avi or *.AVI

#SET THE SIZE
#320x240
#800X600
#1280x960...

CONV_SIZE="640x480"

#Check, whether ffmpeg is being installed
if [ ! -x /usr/bin/ffmpeg ]
then    echo "ffmpeg is not installed"

    if [ -x /usr/bin/apt-get ]
    then     echo; echo "Let's try to install it..."
        sudo apt-get update; sudo apt-get -y install ffmpeg
        if [ $? != 0 ]
        then echo "Installation Error... please verify it and try to install ffmpeg manualy"; sleep 3; exit
        fi
    else    echo "Package ffmpeg must be installed on your system"; sleep 3; exit
    fi
fi

#statemant validation
if [ -z $1 ]
    then
    echo "Target directory not specified"
    echo "Usage: avi2mpgconv.sh <target directory>"
    sleep 1
    exit 1
fi

echo "Eliminate all spaces from the directories and files names"
echo "Processing directories names..."

find $1 -type d -exec rename 's/ /_/g' {} ; > /dev/null 2>&1

echo "Done..."
echo "Processing filenames..."

find $1 -type f -exec rename 's/ /_/g' {} ;

echo "Done..."
sleep 2

#total_files=`find $1 -name '*.[Jj][Pp][Gg]' -print0 | xargs -0 ls -1 |wc -l`
total_files=`find $1 -type f -name '*.[Aa][Vv][Ii]' | grep -v converted_files |wc -l`
dir_list=`find $1 -type d |egrep -v '^.{1,2} |grep -v converted_files`
clear

for z in $dir_list; do
    cd $z
    dir1=`pwd`

    #Check whether any AVI files exist in the directory
    ls -1 *.[Aa][Vv][Ii]  > /dev/null 2>&1
    if [ $? != 0 ]
    then cd - > /dev/null
    continue
    fi

    ls -1 converted_files/*.[Aa][Vv][Ii]  > /dev/null 2>&1
    if [ $? = 0 ]
    then cd - > /dev/null
    continue
    fi

    if [ ! -d converted_files ]
            then mkdir converted_files
    fi

    #Let's roll
        nr_of_files=`ls -1 *.[Aa][Vv][Ii] | wc -l` > /dev/null 2>&1
    g=0
    for i in *.[Aa][Vv][Ii]; do
        echo  "<<<< START >>>>"
        h="$(($h + 1))"
        g="$(($g + 1))"
        echo "Transcoding: [$h from $total_files] in total"
        echo
        echo "Transcoding to MPG $i: [$g from $nr_of_files] within: $dir1"
        ffmpeg -sameq -s $CONV_SIZE -i $i converted_files/$i.mpg
    clear
    done
    cd - > /dev/null

done

echo ">>>> Job done for $1<<<<"
exit

#End-Script#