Learn how to use ffmpeg to create a GIF from a video file. You will learn how to change the resolution and frame rate, so you can decide on quality vs file size depending on your needs. You can also select a portion of the video using a start and end time.
Follow this guide, for instructions to easily install ffmpeg. Once you have it installed, you will be able to use ffmpeg via any command line.
I will use this video for the tutorial:
Create a GIF
You can convert a video called ‘input.mp4’ to a gif using this command:
ffmpeg -i input.mp4 output.gif
This specifies the name of the input video. Or the video we want to convert to a GIF.
-i input.mp4
This will be the name of the output GIF.
output.gif
These settings might work great for you, or they might end up taking up too much space. Remember different social media sites, have different restrictions for resolution and file size.
The original video takes up 6.24mb. This new GIF takes up 1.66mb. Both files have a resolution of 1280×720.
Output video size
Lets try half the size of the output video from 1280×720 to 640×360.
ffmpeg -i input.mp4 -s 640x360 output.gif
This specifies the output video size:
-s 640x360
As expected the new GIF file is half the size in resolution and has a smaller file size of 723kb. WordPress is using auto size for the width and height though, so the video is likely scaled up on this post.
Output frame rate
Lets change the output video frame rate next. The original video has a frame rate of 82.75 frames per second. Lets change it to 10.
ffmpeg -i input.mp4 -r 10 output.gif
This specifies the output video frame rate:
-r 10
Lowering the frame rate can decrease file size, but it can also make the video more choppy. In this case the new gif is 1.42mb in file size. So only a tiny bit smaller.
Output duration
The original video is 13 seconds, lets try make a shorter output video.
I’ll start from 2 seconds into the video, and end at 8 seconds. So the new video should be 6 seconds long.
ffmpeg -ss 2 -to 8 -i input.mp4 output.gif
This sets the start time.
-ss 2
And this sets the end time.
-to 8
The new gif is 830kb.
You can also change the hour, minute, second, or millisecond of the start and end time. This would do the same as above. Start at 2 seconds, and end at 8 seconds.
ffmpeg -ss 00:00:02.000 -to 00:00:08.000 -i input.mp4 output.gif
Improve quality
A gif file is limited to 256 colours. So we have to be careful which colours we pick in order to make the gif look good. We can improve the gif quality by using a custom colour palette. There is an operation called palettegen which can analyse a video and generate a better colour palette. More information here.
However this operation takes a lot longer and leads to a much larger file size. In my case it is 83.1mb. So I don’t recommend you run this command!
ffmpeg -i input.mp4 -filter_complex "[0]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse" output.gif
Reduce file size
The previous command generates a gif file which looks much better than before, but is to large in file size. So we need a way to reduce the file size. We will do this by using what we learned about before:
- Setting a start and end time to make a shorter video.
- Reducing the frame rate.
- Lowering the video resolution.
ffmpeg -ss 3 -to 8 -i input.mp4 -filter_complex "fps=10,scale=-1:180[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse" output.gif
This command starts from 3 seconds, and ends at 8 seconds. Meaning the output video will be 5 seconds long.
-ss 3 -to 8
We are using filter_complex instead of the default option we used above.
-filter_complex "fps=10,scale=-1:180[s];
This sets the frame rate.
fps=10
This sets the scale.
scale=-1:180
The -1 means I want to keep the aspect ratio for the output video, the same as the input video. Since my original video is 1280×720, I know that 180 fits into 720 cleanly. So the -1 will pick 320 for the scale width. More information on scaling and aspect ratio here.
Feel free to experiment with the options until you get a good trade off between quality and file size. I tried 3 options for scale:
- scale = -1 : 720 which produced a 8.2mb gif.
- scale = -1 : 360 which produced a 2.66mb gif.
- scale = -1 : 180 which produced a 906kb gif.
Below is the 180 version. I chose the smaller one so that the web page can load quicker.
Conclusion
This tutorial has shown you how to create a gif from a video file using ffmpeg. We learned how to adjust start and end time. We adjusted frame rate, resolution and scale. We learned about an option which can improve the video quality, and more options to reduce file size. We have learned that creating a gif comes with compromises of quality vs file size.
Thank you for reading this tutorial. Let me know in the comments section if you enjoyed it, or have any questions!
Learn how to compress a video file using ffmpeg here.
Or learn how to combine video files using ffmpeg here.
7 responses to “Creating a GIF from a video file using ffmpeg”
Looking beautiful! 👌
LikeLiked by 2 people
Thank you Priti for your kind words 🙂
LikeLike
☺️🙏
LikeLike
Nice post 💚💛❤️🩵💙
Blessed and Happy Sunday from 🇪🇸
Greetings 👋
💮🌸🌷💞🏵️🌻🌼
David López pk 🌎
LikeLiked by 1 person
Thank you for visiting David!
LikeLike
Thanks 😊
LikeLiked by 1 person
Divine
LikeLiked by 1 person