Got a cool or funny video clip in your movie file and want to extract it into an animated GIF? Linux can do the trick by running a single command.
Most video editor applications can do the job. They are precisely but a bit heavy for this thing. For choice, Linux user can run a single FFmpeg command as a workaround.
Install FFmpeg
In case you don’t have FFmpeg library installed on your system, open terminal and run one of the commands below to install it.
- For Debian, Ubuntu, Linux Mint based system, run command:
sudo apt install ffmpeg
- For Fedora, RHEL, Rocky Linux, use command:
sudo dnf install ffmpeg
- And Arch, Manjaro Linux, etc, can install it via:
sudo pacman -S ffmpeg
Convert / Extract Video to Animated GIF
Now, open terminal and navigate to the folder that contains the video file. Or, right-click on that folder and select “Open in Terminal“.
To convert the whole video into GIF, use command:
ffmpeg -i input-file.mp4 output-file.gif
In the command above:
-i input-file.mp4
– specify the original video file. Change filename accordingly. And it supports most common video file formats, such as .avi, .mov.output-file.gif
– means the output file. Also, change the name as you want.
To extract video to GIF with certain start and end time, use following flags:
-ss
– follow with time (e.g., 00:15:01) for the start point.-to
– follow with time (e.g., 00:18:30) for the end point.
For example, extract the video clip from 15 minute and 01 second, to 18 minute and 30 second, use command:
ffmpeg -i input-file.mp4 -ss 00:15:01 -to 00:18:30 output-file.gif
To set how much loop times for the output GIF, use:
-loop
follow with a number (-1 for once, 0 for infinitely, 1 for two times, 2 for 3 times, …).
For example, extract the video clip into GIF and loop for 3 times, run command (By default, it loops infinitely):
ffmpeg -i input-file.mp4 -ss 00:15:01 -to 00:18:30 -loop 2 output-file.gif
To specify the frame rate for smaller output file size, use flag:
-r
follow with the number of frame rate.
For example, set frame rate to 10, use command:
ffmpeg -i input-file.mp4 -ss 00:15:01 -to 00:18:30 -loop 2 -r 10 output-file.gif
To resize the width and height in the output GIF, use flag:
-vf scale=720:400
– change the width number 720, height number 400 (or use -1 to auto-resize according to width) as you want.
For example, extract video clip and resize output GIF to 720 width:
ffmpeg -i input-file.mp4 -ss 00:15:01 -to 00:18:30 -vf scale=720:-1 output-file.gif
To change the color depth, use flag:
-sample_fmt
follow with color depth name.
First, you need to run -sample_fmts to output all supported color depth names
. Then, run the command below as example:
ffmpeg -i input-file.mp4 -ss 00:15:01 -to 00:18:30 -sample_fmt s16 output-file.gif
That’s all. If you have more tips about extract video to GIF, feel free to leave comment here.