Use FFMPEG to optimization video, convert video file to gif in Terminal
FFMPEG is a powerful tool for converting different video files from one format to another. It is usually used in many video-converting websites and software.
We can use FFMPEG directly to convert video files or make them smaller. All works run on our local machine without uploading.
You need some basic knowledge of the Terminal. In this tutorial, we’ll install FFMPEG through the package management tools. And add two scripts into the config file of our command line interface shell.
To check whether FFMPEG is already on the computer, we can open the Terminal and type then press enter:
$ ffmpeg -version
If the Terminal shows the message: command not found, it means we don’t have FFMPEG installed on our computer. Otherwise, the Terminal will display the version of FFMPEG installed on the computer.
Installing FFMPEG with a package management tool is simple. If you use a mac computer, we can use the package management tool, homebrew, to install FFMPEG. We can use the command below to get FFMPEG:
$ brew install ffmpeg
It is very similar to Linux. For example, I’m using a Fedora PC. And I can use dnf to install it. Just find out which package management tools ship with your distribution.
Add scripts to use FFMPEG easily
After Apple released macOS Catalina, the z shell (zsh) became the system’s default shell. We can add scripts into zsh’s config to use FFMPEG conveniently.
.zshrc file, which is the settings file of zsh. Add this script to the settings.
Restart your Terminal to take effect. This script is for optimizing the video file. We can open the directory which contains our target file in Terminal. And type this command to optimize a target video:
$ optimizeVideo yourVideoFileName.mp4
A large video file can benefit from it. For example, I have a 5-second mov video(Yes, FFMPEG can also optimize the mov video format), which file size is 2.2MB. Take this file as an example. After optimization, The file size is shrunk to 509kb. And you’ll find the video format is changed to mp4.
Another helpful feature is converting a video file into a GIF image. We can add script below to the
After restarting the Terminal, we can type this command to convert a video file into a gif image.
$ video2gif yourVideoFileName.mp4 (CustomScale) (CustomFPS)
This script can input variables to customize the result (They are optional). For example, we can resize the image to have a proper size or change the fps to make it smoother. The default behavior of this script will scale the image into 320-pixel width with the height calculated based on the original image aspect ratio.