Background
In the last post, we saw to Resizing videos and images using ffmpeg. In this post, we will see how we can convert a video into a gif with good quality.
Create a gif from a video using FFmpeg
You can use the following shell script to convert your video to Gif -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #!/bin/bash #---------------------------------------------------- # Author : athakur # Version : 1.0 # Create Date : 27 / 02 / 2018 # Update Date : 27 / 02 / 2018 # Description : Create gif from a video # Sample usage : ./creategif.sh input.mp4 output.gif # ffmpeg static build can be downloaded from https: //johnvansickle.com/ffmpeg/ #---------------------------------------------------- echo "Converting $1 to $2" if [ -z "$1" ] || [ -z "$2" ] then echo "Incorrect arguments supplied. Format - ./creategif.sh input.mp4 output.gif" exit fi palette= "/tmp/palette.png" filters= "fps=10,scale=1024:-1:flags=lanczos" ffmpeg -v warning -i "$1" -vf "$filters,palettegen" -y "$palette" ffmpeg -v warning -i "$1" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$2" echo "Completed gif creation" |
You can find this code in my GitHub gists section-
To run you can simply execute following command -
- ./creategif.sh input.mp4 output.gif
Sample video and gif are as follows -
Video :
Gif :
That's it your high-quality gif is ready.
You can do more with FFmpeg like scaling and cropping of video before converting to gig. Eg.
1 | ./ffmpeg -loglevel warning -y -i input.mp4 -i palette.png -filter_complex "crop=800:500:0:0[x];[x][1:v]paletteuse=dither=bayer:bayer_scale=3[tl];[tl]fps=10,scale=1024:-1:flags=lanczos" target.gif |
No comments:
Post a Comment