Digital media transcoding – Part 1 – FFmpeg

François Téchené

François Téchené

Director of Creative at Purism
François Téchené

Latest posts by François Téchené (see all)

Still ready to switch to Free Software for your mulitmedia creations?
Let’s start with understanding the files and formats that we are going to manipulate in our workflow.

Codecs and Formats

vcr-1221156_640
When it comes to multimedia creation and publishing, using the right format, the right codec, converting, scaling, compressing, can be a real pain. Thankfully, in the world of Freedom, we have some of the best tools to help us manipulate media files and avoid a lot of frustration.

By the way, what’s a Format? And what’s a Codec?

You may wonder what’s the difference between a format and a codec? Well see it as the format being the container of the entire media file’s data representing both audio and video, and the codec being the way to encode and decode this data. The same format can hold data described by different codecs. Also a codec can be used with different formats.

As an example, the Matroska (.mkv) format can store either H264 or Theora encoded video and Opus or FLAC audio. Now the OGG format, can also hold Theora video and Opus or Vorbis audio… Is this making sense?

These are just examples but I have to admit that there are so many different formats and codecs that it is very difficult to see clear sometimes. What I suggest is to use only a few formats and codecs. The ones we really need. I will come back to this point in a future article.

FFmpeg

The software that I use for digital media transcoding is FFmpeg.

FFmpeg is a command line based software that manipulates formats and codecs. Many free software already rely on FFmpeg so you may not need to ever use the commands directly but if you are comfortable with the terminal, FFmpeg can be very useful for quick conversions.

I will cover the basic usage of FFmpeg in this post.

If don’t like using the terminal or don’t easily remember commands (just like me), don’t worry, I will cover media conversions with a clean GUI, in a future post.

Installing FFmpeg

FFmpeg being very popular is pretty easy to install and should be directly available in the most common GNU/Linux distributions repositories.

When using PureOS, FFmpeg should be installed by default but if it is not the case, just use the following command in a terminal :

sudo apt install ffmpeg

Using FFmpeg

In order to get a list of formats supported by FFmpeg, open up a terminal window and type the following command :

ffmpeg -formats

And for a list of supported codecs, type the following command :

ffmpeg -codecs

As you can see, the list is quite impressive! FFmpeg can manipulate the most common free and proprietary digital audio and visual formats.

Converting a video can be achieved by a simple command line :

ffmpeg -i input.mov output.webm

This command converts a Quicktime .mov file to a .webm format with the default encoders (keeping the same scale, framerate and bitrate).

If you need to use a specific codec’s encoder for the chosen format just specify it in -vcodec (for video) and -acodec (for audio):

# Outputs the video in an OGG format using Theora codec for video and Vorbis codec for audio
ffmpeg -i input.mov -vcodec libtheora -acodec libvorbis output.ogg

# to get a list of all encoders
ffmpeg -encoders

In order to convert a video to an image sequence, you may do the following :

# I create a directory to store the files from my image sequence
mkdir sequence
ffmpeg -i input.mov sequence/output_%05d.png

If you wish to rescale the picture of the video, you can use the -vf option (video filter) and set the “scale” value :

# Rescales width and height
ffmpeg -i input.mov -vf scale=320:240 output.webm

# Rescales width keeping aspect ratio
ffmpeg -i input.mov -vf scale=320:-1 output.webm

# Doubles width keeping aspect ratio
ffmpeg -i input.mov -vf scale=scale=iw*2:ih output.webm

# Forces the image to fit into a 320×240 box
ffmpeg -i input.mov -vf scale=w=320:h=240:force_original_aspect_ratio=decrease output.webm

If you wish to force a constant bitrate to your video, use the -b option (use -b:v for the video and -b:a for the audio) :

# 8Mbit/s for video and 128kbit/s for the audio
ffmpeg -i input.mov -b:v 8000k -b:a 128k output.webm

You may also use -minrate and -maxrate to control the minimum and maximum bit rate tolerance (in bits/s)

These are the conversions that I use the most and represent a very short part of the real potential of FFmpeg. There are many more options and filters so if you want to know more about it, I suggest you browse the FFmpeg documentation.

Coming next, will be a tutorial on media conversion using the great GUI from Kdenlive. To complete this series of articles about media files manipulation, I will share with you the different free formats, codecs and params that I use in my entire video production workflow.

Stay tuned! 😉

One last thing…

As you were patient enough to read this article till the end, here is a little present. It is a script that I use to quickly convert short videos animations to animated GIFs.
This script generates a good quality GIF that is scaled down to 640px wide and ready to be embedded in any webpage. I found it in this excellent tutorial.

#!/bin/sh

palette=”/tmp/palette.png”

filters=”fps=24,scale=640:-1:flags=lanczos”

ffmpeg -v warning -i $1 -vf “$filters,palettegen” -y $palette
ffmpeg -v warning -i $1 -i $palette -lavfi “$filters [x]; [x][1:v] paletteuse” -y $2

To call this script, just use :

./gifenc.sh input.mkv output.gif

Happy free transcoding! 🙂

Thoughts? Send them to feedback(at)puri.sm