When to use it?
Well, at Plusgrade we use BitBucket
and we have a custom template when creating PR’s where we describe the Context, Changes, Validation and Comments.
Under Validation we tend to put pictures of before and after the changes. Sometimes, a video of a change would be nice, but BitBucket doesn't support videos! Hence, I decided to add this to my PC.
P.S you can just use it if you want to convert videos to GIFs lol.
Steps to setup
- Make sure you have
ffmpeg
installed, if not simply run:brew install ffmpeg
- Press Command + Space and type “Automator”
3. A popup will show to select the type of document you want to create, select Quick Action
4. Set Workflow Receives current as movie files
and In as Finder
4. Look for Run Shell script
& double click it
Set Pass input as as arguments
(yes this GIF was generated with my shell script)
5. Copy this following shell script
#!/bin/bash
for f in "$@"
do
filename="${f%.*}"
/opt/homebrew/bin/ffmpeg -i "$f" -vf "fps=15,scale=720:-1:flags=lanczos" -c:v gif "${filename}.gif"
done
Some breakdownfor f in “$@”
loop through all arguments passed in the script (the original video file)filename=”${f%.*}
regex to get the file’s name without its exention$f
holds the video -vf “fps=15,scale=720:-1:flags=lanczos”
vf is video filter which allows to apply the following effects at once:
fps=15
frames per secondscale=720:-1
width of 720px, the -1 is to maintain aspect ratioflags=lanczos
Lanczos resampling filter, for high quality resizing and scaling-c:v gif
tellsffmpeg
to encode the output as a GIF
Note: I run which ffmpeg
provides a path to the ffmpeg
executable. In my case: /opt/homebrew/bin/ffmpeg
6. Click File
and press Save
Now you should be able to use it like so:
But can we make it better?
GIFs are low quality, they have a limit of 256 colors only!
#!/bin/bash
for f in "$@"; do
filename="${f%.*}"
/opt/homebrew/bin/ffmpeg -i "$f" -vf "fps=15,scale=720:-1:flags=bicubic,palettegen" -y "${filename}_palette.png"
/opt/homebrew/bin/ffmpeg -i "$f" -i "${filename}_palette.png" -filter_complex "fps=15,scale=720:-1:flags=bicubic[x];[x][1:v]paletteuse=dither=bayer" -y "${filename}.gif"
done
Yes this is a much better quality.
Breakdown:
We do some very similar steps like the first script, the change is in generting a color palette.
/opt/homebrew/bin/ffmpeg -i "$f" -vf
"fps=15,scale=720:-1:flags=bicubic,palettegen"
-y "${filename}_palette.png"
- We use Bicubic Scaling instead of Lanczos
palettegen
will generate an optimized color palette for better GIF quality-y
is to override existing file forcefully
/opt/homebrew/bin/ffmpeg -i "$f" -i "${filename}_palette.png" -filter_complex
"fps=15,scale=720:-1:flags=bicubic[x];[x][1:v]paletteuse=dither=bayer"
-y "${filename}.gif"
-filter_complex”fps=15,scale=720:-1:flags=bicubic[x]
we apply a complex-filter and store the result of our new scaled video at 720px with 15fps in variable x[x][1:v]paletteuse=dither=bayer
we apply the color palete (input 1, recall input 0 was the video in varaible f)- The
paletteuse
filter optimizes the video’s colors for GIF output, using the Bayer dithering method to reduce color banding.
The latest script will generate a GIF and a PNG image, you may want to add the following command to your script to remove this temp PNG:
rm -f "${filename}_palette.png"
-Lara