Work With FFmpeg on Linux

This tutorial will show you how to Work with FFmpeg on Linux Terminal with Examples. As you know, FFmpeg is the most valuable and popular video and image processing software. You can use FFmpeg to handle transcoding, video and image resizing, denoising, etc., packaging, streaming, and playback.

In this guide, you will learn to work with FFmpeg to reduce video size (compressing), convert MKV to MP4, and automate multiple FFmpeg commands with Bash.

Steps To Work With FFmpeg on Linux

Before you start to work with FFmpeg, you must install the software on your Linux server. You can visit the Orcacore website and search for FFmpeg and get the installation guides.

Here we provide you with some of the FFmgep installation guides:

FFmpeg installation on AlmaLinux 9

FFmpeg installation on Ubuntu 22.04

In this guide, we will use FFmpeg on AlmaLinux 9 to show you the guide steps.

Step 1 – Reduce or Compress Video Size with FFmpeg from Linux Terminal

One of the best features of FFmpeg is to use it for reducing the size of videos. It is a good choice for those who are running out of storage and want to reduce their memory usage.

At this point, you can use the following example to see how you can compress your video size. You should use a good codec and constant rate factor for this purpose.

In this example, we want to use the x265 codec. It is a free library that is used for encoding videos. Also, we use CRF (Constant Rate Factor) between 0 to 51 which is a good option for x265 codec.

To get more information about the available decoders, you can use the command below:

ffmpeg -encoders

For the x265, you can see the compression type is:

V....D libx265              libx265 H.265 / HEVC (codec hevc)

In the following Linux command, we use FFmpeg to compress the file. We have a Video file named media.y4m and want to compress it into a file named media.mp4. You can replace the media file that you want to compress.

ffmpeg -i media.y4m -vcodec libx265 -crf 28 media.mp4

As you can see in the example, the -i option is used for the input file (media.y4m), -vcodec is used for the video encoder which is libx265, and the value for -crf is defined as 28, and the media.mp4 is used for the output file.

With this option of FFmpeg, you can compress your video file size and reduce your memory usage in Linux.

Step 2 – Convert MKV files to MP4 with FFmpeg on the Linux Terminal

At this point, you can easily use two methods with FFmpeg to convert MKV files to MP4 which are:

  1. Copy streams into desired containers
  2. Re-encode the streams

Let’s how you can do this.

Work with FFmpeg – Converting MKV to MP4 without Re-encoding

The easiest way to convert MKV to MP4 is to do it without re-encoding. You just need to copy the streams into your desired containers. For example:

ffmpeg -i Orca.mkv -c copy Orca_wout.mp4

As you can see, the -i option is used for the input file, and the -c option does the copy process in the output file given. With this option, the video and audio quality will not be reduced and the compression process is too fast.

Also, you can specify the streams you want to convert. For example:

ffmpeg -i Orca.mkv -c:v copy -c:a copy Orca_vid-aud.mp4

Here you will see the -c:v option is used for coping only video and -c:a is used for coping audio in your MP4 output file.

Work with FFmpeg – Converting MKV to MP4 with Re-encoding

In this step, you can convert MKV to MP4 by using the encoders. For example, you can use the libvorbis encoder to convert your files:

ffmpeg -i Orca.mkv -c:v copy -c:a libvorbis Orca_encode-AUD.mp4

In this way, you will copy your files into a new container.

Also, you can set up an encoder for your video stream. To do this, you can use mpeg4 instead of copy command:

ffmpeg -i Orca.mkv -c:v mpeg4 -c:a libvorbis Orca_ENCODE_vid-aud.mp4

There are many encoders that you can use. You can list them by using the command below:

ffmpeg -encoders

Step 3 – Use Bash Scripting To Automate Multiple FFmpeg Commands

In this step, you can use bach scripting to work with FFmpeg in Linux. It is a good way to large media files that you want to compress or convert. To do this, you can follow the steps below.

First, you must create a bash file for your FFmpeg on your Linux server with the command below:


Then, open your FFmpeg bahs file with your desired text editor like Vi Editor:


In the file, add the following line to add the file to the bash and the example script:

#! /bin/bash

for mediafile in *.mkv; do
echo  $mediafile

When you are done, save and close the file.

This command script makes use of a for loop to iterate over the .mkv files of the current directory, additionally, it prints out the $mediafile variable’s value which holds the name of the file.

Next, make the file executable and run it with the following Linux Commands:

# chmod +x
# ./

This will show your MKV files.

As you saw this is a simple example of FFmpeg bash scripting. Now you can use the FFmpeg command in your bash file to do your multiple tasks.

For example, you can convert MKV files to MP4, by defining the following script in your file:

#! /bin/bash

for mediafile in *.mkv; do
ffmpeg -i $mediafile -c copy  "${mediafile%.mkv}.mp4"

Then, run your bash file:


This command will convert your MKV files to MP4. You can do this for any script you want to use for FFmpeg.

For more information, you can visit the FFmpeg Documentation.


At this point, you have learned how to work with FFmpeg commands on your Linux server such as reducing video size (compressing), converting MKV to MP4, and automating multiple FFmpeg commands with Bash.

Hope you enjoy using it. Do you need any help? Please comment for us.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!