Content
#
# ######################################################################
# BATCH CONVERTER
#
# This script targets .avi as an input file(s) and converts into .mp4
# You can change input target from .avi to any like .mkv
# by replacing .avi to .mkv i.e INPUT_EXT=.avi to INPUT_EXT=.mkv
# ######################################################################
# Checking if ffmpeg is installed.
ffmpeg=`which ffmpeg`
ffprobe=`which ffprobe`
EXITCODE=101
if (test ! -n "$ffprobe");
then
clear
echo
echo "********************************"
echo "* You have to install ffprobe. *"
echo "********************************"
echo
exit "$EXITCODE"
fi
if (test -n "$ffmpeg");
then
# Specify input and output file extensions
INPUT_EXT=.avi # extension .avi can be replace with any valid video extension like .mkv
OUTPUT_EXT=.mp4
# Specify input and output directories.
# "/path/to/some/directory"
# Here input and output directory is `pwd` or "${PWD}" -> Current Directory.
INPUT_DIR=`pwd` # "$(PWD)" can be replaced with "/path/to/input/directory"
OUTPUT_DIR=`pwd` # "$(PWD)" can be replaced with "/path/to/output/directory"
# Counting total files for encoding.
# If count is zero then nothing to do!
COUNT=`ls -l "$INPUT_DIR/"*"$INPUT_EXT" | grep ^- | wc -l`
echo
echo "Total $COUNT file(s) to encode ..."
echo
if [ "$COUNT" -gt 0 ];
then
for f in "$INPUT_DIR/"*$INPUT_EXT; do
INPUT_FILE="$f" ; OUTPUT=`basename "$f"`
OUTPUT_FILE="$OUTPUT_DIR/${OUTPUT%$INPUT_EXT}$OUTPUT_EXT"
## USING ffprobe TO GET MEDIA INFORMATION
resolution=`ffprobe -v error -select_streams v:0 -show_entries stream=height,width -of csv=s=x:p=0 "$INPUT_FILE"`
frame_rate=`ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 "$INPUT_FILE"`
aspect=`ffprobe -v error -select_streams v:0 -show_entries stream=display_aspect_ratio -of default=noprint_wrappers=1:nokey=1 "$INPUT_FILE"`
channels=`ffprobe -v error -select_streams v:1 -show_entries format=nb_streams -of default=noprint_wrappers=1:nokey=1 -sexagesimal "$INPUT_FILE"`
## USING ffmpeg to COVERT
echo
echo "Now encoding file: " $INPUT_FILE
ffmpeg -i "$INPUT_FILE" -codec:v libx264 -crf 21 -bf 2 -r "$frame_rate" -s "$resolution" -aspect "$aspect" -flags +cgop -pix_fmt yuv420p -codec:a aac -b:a 256k -r:a 48000 -ac "$channels" -movflags faststart "$OUTPUT_FILE"
wait
done
else
echo "File with "$INPUT_EXT" extension does not exist."
fi
echo
echo "Total "$COUNT" file(s) has been encoded."
echo
else
clear
echo
echo "********************************"
echo "* You have to install ffmpeg. *"
echo "********************************"
echo
fi
exit 0
From
#
Links
#