In this article I show you how to convert a video file to a .gif using a command line tool called FFMPEG.
I often use this when I have a video that I would like to upload to the internet, but I'm limited on file size, or I don't want to have to wait a long time for the video to upload. One of my most common use cases is for attaching a screen recording to a pull request in a git hosting tool.
Recording your screen
If you already have a video file that you would like to convert to a .gif then you can skip this step.
MacOS
- Open QuickTime Player
- Choose file > new screen recording
- Click the red record button
- Select the area you would like to record
- Click a screen to start recording the entire screen
- Drag over part of the screen to only record that area
- Do whatever you would like to record
- Click stop to stop recording
https://support.apple.com/en-us/102618
Windows 11
- Press
Windows
+SHIFT
+S
to open the clipboard - Click the video camera icon to change from screenshot to record
- Select the area that you would like to capture
- Click record to start recording
- Do whatever you would like to record
- Click the record button again to stop the recording
https://www.microsoft.com/en-us/windows/learning-center/how-to-record-screen-windows-11
Windows 10
You may be able to use Xbox Game Bar, or have to install a tool such as OBS.
Ubuntu
- Press
Shift
+CTRL
+ALT
+R
to start recording - Do whatever you would like to record
- Press the red button to staop the recording
https://help.ubuntu.com/stable/ubuntu-help/screen-shot-record.html#screencast
Installing FFMPEG
Go to the FFMPEG download page and download the proper version for your operating system: https://www.ffmpeg.org/download.html
Once FFMPEG has installed, open a shell (bash, CMD, PowerShell, etc. should all work), and run ffmpeg
to verify that it is installed.
If you get a message which lists the version number, similar to the one shown below, then you have successfully installed FFMPEG
ffmpeg version 6.0-essentials_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
If you get a message similar to this one, which mentions that the command could not be found, then you may need to manually add FFMPEG to your path
The term 'ffmpeg' is not recognized as the name of a cmdlet, function, script file, or operable program.
Detailed steps to install on Windows: https://phoenixnap.com/kb/ffmpeg-windows
Convert to GIF
Below are a few different settings that you may want to use when converting the image. The below commands assume that the file that you are converting is named "input.mp4" and that you want to name the new file "output.gif". Feel free to change the input/output file names as needed/desired.
Before running the command, you'll need to use cd
to change to the directory with the files. Alternatively you could use the full relative path to the input and output files, but I typically find it quicker to change to the directory.
Keep the current resolution and scaling
ffmpeg -i input-video.mp4 output.gif
Scale the resolution down (to reduce file size)
ffmpeg -i input-video.mp4 -s 960x540 output.gif
-s denotes scale to {width}x{height}
Reduce the frame rate (to reduce file size)
ffmpeg -r 15 -i input-video.mp4 output.gif
-r sets the new frame rate to 15 frames per second