Wednesday, May 29, 2013

Linux/Unix Shell scripting: Convert video file to gif image in Linux

This is a small script which will convert your video file into a gif file so that we can share it across Internet so that video player is not required to play it. The script is written in Linux Bash shell

#Author: Surendra Anne(
#Purpose: To convert any video file into a GIF image file. The script will take one video file from command line arguments and do the conversion using mplayer and convert. So please install these two software's before running this script.
#Licence: GPL v.3

[[ "$#" -ne 1 ]] && { echo -e "Script should be run as below\n bash $0 video-file\n Exiting the script..!";exit 1; }
mkdir /tmp/images/
mplayer -ao null $1 -vo jpeg:outdir=/tmp/images/
convert /tmp/images/* /tmp/abc.gif
convert /tmp/abc.gif -fuzz 10% -layers Optimize /home/surendra/Desktop/opt-gif.gif
rm -rf /tmp/{abc.gif images/}

First line in the script will check if a file is passed to script or not, if not it will through some meaningful error message to user.
mplayer command will convert video file into jpeg images and the first convert command will club these image files to single animated gif file. The second convert command will compress the gif file considerably to 10% of actual size. And the file will be stored on desktop. You may have to change the locations to suite your requirements. 


Post a Comment