3 Easy Steps To Work With FFmpeg on Linux with Examples

Posted on

3 Easy Steps To Work With FFmpeg on Linux with Examples

3 Easy Steps To Work With FFmpeg on Linux with Examples

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 on the Orcacore website, you will learn to Work With FFmpeg on Linux to reduce video size (compressing), convert MKV to MP4, and automate multiple FFmpeg commands with Bash.

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

Here we provide you with some of the FFmpeg 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 to Work With FFmpeg on Linux.

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

To Work With FFmpeg on Linux, you can easily use two methods with FFmpeg to convert MKV files to MP4 which are:

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

To Work With FFmpeg on Linux, 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

To Work With FFmpeg on Linux, 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:

touch auto_ffmpeg.sh

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

vi auto_ffmpeg.sh

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
done

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 auto_ffmpeg.sh
# ./auto_ffmpeg.sh

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:

vi auto_ffmpeg.sh
#! /bin/bash

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

Then, run your bash file:

./auto_ffmpeg.sh

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 on Work With FFmpeg on Linux, you can visit the FFmpeg Documentation.

Conclusion

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 this guide on Work With FFmpeg on Linux. Do you need any help? Please comment for us.

Alternative Solutions

While the article provides a good starting point for using FFmpeg, here are two alternative approaches to achieve the same goals, offering different levels of control and potentially better results in specific scenarios:

1. Advanced Compression with Two-Pass Encoding

The article mentions CRF (Constant Rate Factor) as a way to compress video. While CRF is a single-pass encoding method and relatively easy to use, a more efficient method for achieving a specific file size or quality level is two-pass encoding.

Explanation:

In two-pass encoding, FFmpeg analyzes the entire video file in the first pass to determine the complexity of each scene. This information is then used in the second pass to allocate bits more efficiently, resulting in better overall quality for a given file size compared to CRF. It is helpful to know the target output file size for this method.

How to implement two-pass encoding:

First Pass:

ffmpeg -i media.y4m -vcodec libx265 -pass 1 -an -f null /dev/null

Second Pass:

ffmpeg -i media.y4m -vcodec libx265 -pass 2 -crf 23 -acodec aac -b:a 128k media_two_pass.mp4

Breakdown:

  • -pass 1: Specifies the first pass.
  • -an: Disables audio encoding during the first pass (optional but recommended).
  • -f null /dev/null: Outputs the first pass data to a null file (discarding the actual video).
  • -pass 2: Specifies the second pass.
  • -crf 23: This is the target video quality factor for the second pass. You may need to adjust this depending on your needs.
  • -acodec aac: Specifies the audio codec (AAC is widely supported).
  • -b:a 128k: Sets the audio bitrate to 128kbps.
  • media_two_pass.mp4: The output file name.

Benefits:

  • Better quality at a given file size compared to single-pass encoding.
  • More control over the final output size and quality.

Drawbacks:

  • Takes twice as long to encode the video.
  • Requires understanding of encoding parameters for optimal results.

2. Using a GUI for Complex FFmpeg Operations

While command-line FFmpeg is powerful, it can be daunting for beginners or for complex tasks. Several GUI (Graphical User Interface) tools exist that wrap around FFmpeg, providing a more user-friendly experience.

Explanation:

These GUIs typically offer a visual way to set encoding parameters, preview results, and manage multiple conversions. They abstract away the complexity of the command-line syntax.

Examples of FFmpeg GUIs:

  • HandBrake: A popular open-source video transcoder with a user-friendly interface. It’s excellent for converting videos to various formats with predefined profiles.
  • Avidemux: A free video editor designed for simple cutting, filtering, and encoding tasks. It offers more advanced editing features than HandBrake.
  • FFmpeg Batch AV Converter: Designed specifically for batch processing video and audio files using FFmpeg.

How to use HandBrake (example):

  1. Install HandBrake on your Linux system (usually available through your distribution’s package manager, e.g., sudo apt install handbrake-cli on Ubuntu/Debian). The command-line interface is installed with the GUI install of HandBrake.
  2. Open HandBrake.
  3. Select the input file (MKV in this case).
  4. Choose a preset (e.g., "Fast 1080p30").
  5. Adjust the video quality settings (e.g., CRF value).
  6. Specify the output file name and location.
  7. Click "Start Encode."

Benefits:

  • Easier to use, especially for beginners.
  • Visual preview of results.
  • Predefined profiles for common encoding tasks.
  • Reduced risk of errors due to incorrect command-line syntax.

Drawbacks:

  • Less flexibility compared to command-line FFmpeg.
  • May not expose all FFmpeg options.
  • Relies on the GUI’s interface, which may not be ideal for advanced users.

In conclusion, while the original article provides a good introduction to using FFmpeg from the command line, exploring two-pass encoding and utilizing GUI tools can significantly enhance your video processing workflow, offering better quality, control, and ease of use depending on your specific needs.

Leave a Reply

Your email address will not be published. Required fields are marked *