Bash Scripting to Convert Mp4 Files into GIF

With a simple script we can quicly convert a folder of MP4 files into GIF file using Bash file and FFMPEG.

First we will need to get a list of all MP4 files under the *.mp4 directory

mp4_dir="./mp4"
file_names=`ls $mp4_dir`

We will then loop over each file and echo only the file name without the extension

for name in $file_names
do
	echo ${name%.mp4}
done	

Finally we can run the ffmpeg command directly inside our Bash script

ffmpeg 
    -ss 0 
    -t 6 
    -i "./mp4/${name%.mp4}.mp4" 
    -vf "fps=10,scale=640:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" 
    -loop 0 
    "./gif/${name%.mp4}.gif"

The FFMPEG options let us specify:

  • START SECOND with -ss param
  • TIME DURATION with -t param
  • INPUT FILE with -i
  • VIDEO FORMAT -vf
  • LOOP INPUT MP4 or NOT with -loop.

And the output GIF file name. ${name%.mp4} will take the mp4 file name and assign it to the gif file.

Want to Receive Updates On Fastest AI Models, Successful AI Startups and New Hiring Candidates. Subscribe To My Newsletters
Subscribe