Skip to content

Instantly share code, notes, and snippets.

@nambok
Last active December 20, 2015 17:08
Show Gist options
  • Save nambok/6166124 to your computer and use it in GitHub Desktop.
Save nambok/6166124 to your computer and use it in GitHub Desktop.
#!/bin/bash
#brew install ffmpeg --with-theora --with-libvorbis --with-fdk-aacc --with-libvpx
#default varialbes
remove_file=false
file_or_dir=false
#new_file_path=false
new_file_name=false
RED="\e[0;31m"
NOCOLOR="\e[0m"
GREEN="\e[0;32m"
convertFile () {
local _filename=$1
local _newfilename=$new_file_name
if ( ! [ -f "$_filename" ] ); then
printf "$RED%s $NOCOLOR" invalid file $_filename
printf "\n"
exit 1
fi
if ( [ -z "$new_file_name" ] || [ $new_file_name == false ] ); then
_newfilename=$(getFileName "$_filename")
fi
ffmpeg -i $_filename -acodec libfaac -ab 96k -vcodec libx264 -level 21 -refs 2 -b 345k -bt 345k -threads 0 -s 640x360 $_newfilename.m4v
ffmpeg -i $_filename -acodec libvorbis -ac 2 -ab 96k -ar 44100 -b 345k -s 640x360 $_newfilename.webm
ffmpeg -i $_filename -acodec libvorbis -ac 2 -ab 96k -ar 44100 -b 345k -s 640x360 $_newfilename.ogv
#ffmpeg -i $_filename -acodec libvorbis -ac 2 -ab 96k -ar 44100 -b 345k -s 640x360 $_newfilename.mp4
if [ $remove_file == true ]; then
printf "removing files...\n"
rm $_filename
fi
}
changeDirectory () {
if ( [ -z "$new_file_path" ] || ! [[ -d "$new_file_path" ]] ); then
printf "$GREEN%s $NOCOLOR" changing directory to $file_or_dir
cd $file_or_dir
else
printf "$GREEN%s $NOCOLOR" changing directory to $new_file_path
cd $new_file_path
fi
}
getFileName () {
filename=$(basename "$1")
extension="${filename##*.}"
filename="${filename%.*}"
echo "$filename"
}
while getopts :i:n:p:r optname; do
case "$optname" in
i)
file_or_dir=${OPTARG%/}
echo "$file_or_dir"
;;
n)
new_file_name=$OPTARG
echo "New filename: $new_file_name"
;;
p)
new_file_path=$OPTARG
echo "New filepath: $new_file_path"
;;
r)
remove_file=true
echo "****this script will remove the original file when done****"
;;
?)
echo "Unknown option $OPTARG"
;;
:)
echo "No argument value for option $OPTARG"
;;
*)
# Should not occur
printf "$RED%s $NOCOLOR" ERROR: Unknown error while processing options
exit 1
;;
esac
done
if [[ -d $file_or_dir ]]; then
echo "$file_or_dir is a directory"
changeDirectory
file_or_dir="$file_or_dir/*.mp4";
for file in $file_or_dir; do
printf "%s\n"
echo "Processing $file...."
convertFile $file
done
elif [[ -f $file_or_dir ]]; then
echo "$file_or_dir is a file"
changeDirectory
convertFile $file_or_dir
else
printf "$RED%s $NOCOLOR" ERROR: $file_or_dir is not a valid directory or file
exit 1
fi
printf "$GREEN%s $NOCOLOR\n" done:]
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment