FFmpeg Convert, Compress Videos and Images

~2 minutes read


This note summarizes couple ways to convert and compress videos and images.


In my previous notes, I have shortly mentioned ffmpeg since I wanted to save as much storage space as possible on my server and to also optimize the delivery time of the website's content.

Here, I will sum up various command lines I often use to switch from one file type to another on Debian distribution or to change encoding.

Installation of FFmpeg

Generally, ffmpeg comes with most of Linux distributions. To check it out:

apt search ffmpeg
i   ffmpeg                          - Tools for transcoding, streaming and playi

i means installed. If another letter arises, to install:

sudo apt install ffmpeg

Compress a Video

For each video that are encoded following a specific codec, there are different file containers (file extensions) that are compatible with the codec. For example. MPEG-4 (.mp4) is mostly used for the H.26x codec, while .webm files are used with the VPx codec.

Containers are compatible with a certain list of codecs.

Loseless and Same Format

ffmpeg -i my-input-video.mp4 my-output-video.mp4

Using VP9 Encoding (*.webm)

ffmpeg -i my-input-video.webm -codec:v libvpx-vp9 -codec:a libopus my-output-video.webm

Using H.265 Encoding (*.mp4, *.mov)

ffmpeg -i my-input-video.mov -codec:v libx265 -codec:a libopus my-output-video.mov

Conversion of a Video

To change the video from one extension to another, it is needed to put in the output file name the extension wanted.

Here is an example from .mp4 to .avi:

ffmpeg -i my-input-video.mp4 my-output-video.avi

Compress an Image

Loseless and Same Format

ffmpeg -i my-input-image.png my-output-image.png

Conversion of an Image

To change the image from one extension to another, it is needed to put in the output file name the extension wanted. Here below is an example with a lightweight alternative to .gif files.

Animated Image (.webp)

ffmpeg -y -i input.webm -quality 60 -loop 1 output.webp
  • -loop 1: for infinite loop.
  • -y: overwrite the output file.
  • -quality 60: 0 to 100 (100 - best quality).

Note: Most conversions can be followed by the Constant Rate Factor parameter (-crf). The range of values depends on the encoder used where closest to 0 tends to lossless and bigger value tends to diminish the output quality. Refer to FFmpeg documentation for more information.

Resources

Published

Category

Notes

Stay Connected