FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. This is my FFmpeg learning notes. Using ffmpeg as video editor and converter. Change the fps and convertÂ
.mp4
file toÂ.gif
 and other formats.
Overview#
FFmpeg is short for "Fast Forward Moving Pictures Experts Group". It is a huge project and it provides 3 command line tools as follow
fmpeg
: video and audio converter.ffplay
: media player.ffprobe
: gathers information from multimedia streams.
This note mainly review some of the usages of FFmpeg
, which is most useful. The documentation of ffplay
 and ffprobe
 is available in Internet and it's convenient to check.
Install#
Windows#
Enter the official site of FFmpeg and choose CODEX FFMPEG @ gyan.dev, find the "release build" section, download ffmpeg-release-full.7z
 file, then unpack the compressed package locally, and finally add ffmpeg
 to the system path, then type in your powershell
and get logs like this
$ ffmpeg -version
ffmpeg version 5.0-full_build-www.gyan.dev Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 11.2.0 (Rev5, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libdavs2 --enable-libuavs3d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
libavutil 57. 17.100 / 57. 17.100
libavcodec 59. 18.100 / 59. 18.100
libavformat 59. 16.100 / 59. 16.100
libavdevice 59. 4.100 / 59. 4.100
libavfilter 8. 24.100 / 8. 24.100
libswscale 6. 4.100 / 6. 4.100
libswresample 4. 3.100 / 4. 3.100
libpostproc 56. 3.100 / 56. 3.100
And that means you have completely install the FFmpeg
.
If there are previous versions of FFmpeg
, we need to delete them.
Ubuntu#
It's quite easy to install FFmpeg
 on Ubuntu compared with Windows, just use apt
 for installing
sudo apt install ffmpeg
Usage#
It's convenient to edit videos using ffmpeg
 with simple commands.
Information#
We can print the basic information of a video using command below
ffmpeg -i <input file>
If you want to hide the redundant1 info, add -hide_banner
ffmpeg -i <input file> -hide_banner
Transcoding#
Transcoding means converting video files from one encoding to another, like from H.264
encoding to H.265
, usually we use encoder libx265
, so the command goes like
ffmpeg -i <input file> -c:v libx265 <output file>
If you want to reverse from H.265 to H.264, just change the encoder to libx264
.
Transmuxing#
Transmuxing means transfer video files from one container to another. Notice that transfering container does not change encoding, so it's pretty fast. Here is an example from .mp4
 to .webm
ffmpeg -i <input file>.mp4 -c copy <output file>.webm
In the command above, -c copy
 means copy directly, without transcoding.
Transrating#
Transrating means adjusting bit rate, often used to reduce the size of a file. We can set the maximum rate, minimum rate and the buffer, like 3856K for maximum rate, 964K for minimum rate and 2000K for buffer size. Command like
ffmpeg -i <input file> -minrate <minimum rate> -maxrate <maximum rate> -bufsize <buffer> <output file>
Transsizing#
Transsizing means changing video resolution2, also called scale3Â Control.
If the video is a high definition file, and we want to scale it down to reduce the size, then we can use the scale control commands.
ffmpeg -i <input file> -vf scale=<width>:<height>:flags=lanczos <output file>
lanczos
is the algorithm of zoom, and if we want to set the width of the output file to half the width of the source video and the height to scale proportionally4, we will set the scale like this
ffmpeg -i <input file> -vf scale=iw/2:-1 :flags=lanczos <output file>
The iw
 means video width, obviously ih
 means video height, and -1
means zoom proportionally. And we can also set the pixels of width and height. If we wnat to get a 480p video, just set the width to 480.
ffmpeg -i <input file> -vf scale=480:-1 <output file>
Demuxing#
Demuxing means extracting audio from video. It goes like this
ffmpeg -i <input file>.mp4 -vn -c:a copy <output file>.aac
In the command above, -vn
means removing video, -c:a copy
 indicates that the audio encoding is not changed, and is copied directly.
Muxing#
Muxing means adding external audio to the video, such as adding background music or narration5.
ffmpeg -i <input file>.aac -i <input file>.mp4 <output file>.mp4
There are 2 input files, one audio and one video, and FFmpeg
combine them as one.
Screenshots#
Screenshots need a start time and a last time, and it means from the start time, get sreenshots continuosly till last time ends.
ffmpeg -y -i <input file> -ss <start time> -t <last time> <output file>_%3d.png
And if you only need a frame of screenshot, it's also available
ffmpeg -ss <cut time> -i <input file> -vframes 1 -q:v 2 <output>.png
In the above command, -vframes 1
 specifies that only one frame is to be captured, and -q:v 2
 indicates the output image quality, which is generally between 1
 and 5
 (1
 is the highest quality).
Cutting#
Usually we only need a part of video, so we can cut it with start time and last time or end time
ffmpeg -ss <start time> -i <input file> -t <last time> -c copy <output file>
ffmpeg -ss <start time> -i <input file> -to <end time> -c copy <output file>
Time unit is second, and we can also use format like 00:00:00.000
, which represents hours:minutes:seconds.points
. -c copy
 means do not change the encoding format of audio and video, copy directly, which will be much faster.
Loop#
If we convert to .gif
 file or other animation pictures, we can set the loop times, basically 1
for once, and 0
for infinity.
ffmpeg -i <input file> -loop <loop times> <output file>
Adjusting#
FFmpeg allows you to adjust video playback speed. To increase the video playback speed, just run
ffmpeg -i <input file> -vf "setpts=0.5*PTS" <output file>
This command will double the speed of the video. To slow down your video you need to use a parameter greater than 1
, like
ffmpeg -i <input file> -vf "setpts=4.0*PTS" <output file>
Compressing#
GIF#
It's direct to convert videos like .mp4
 files to .gif
 animations, just use commands below
ffmpeg -i <input>.mp4 <output>.gif
The default conversion is medium quality mode, to convert high quality gif, you can modify the bit rate
ffmpeg -i <input>.mp4 -b 2048k <output>.gif
ffmpeg -i <input>.mp4 -lossless 1 <output>.gi
We can also convert gif
 file to other formats
ffmpeg -f gif -i <input>.gif <output file
WEBP#
The following command line can convert the file named <input.mp4>
into a lossless, loop played file named <output.webp>
with size 800:600
and default persentation.
ffmpeg -i <input>.mp4 -vcodec libwebp -filter:v fps=fps=20 -lossless 1 -loop 0 -preset default -an -vsync 0 -s 800:600 <output>.webp
If you want the exported output.webp animation to be played only once, it is lossy, the compression level is 3 (0-6, the default is 4, the higher the better the effect), the quality is 70 (0-100, the default is 75, the higher The better the effect), rendering6Â as a picture.
ffmpeg -i <input>.mp4 -vcodec libwebp -filter:v fps=fps=20 -lossless 0 -compression_level 3 -q:v 70 -loop 1 -preset picture -an -vsync 0 -s 800:600 <output>.webp
Reference#
- See official documentation:Â FFmpeg Documentation â.
- Using FFmpeg convert video to gif.
- FFmpeg useful commands.