Skip to content

Instantly share code, notes, and snippets.

@lnfel
Forked from teocci/ffmpeg_install_latest.md
Created December 15, 2022 00:56
Show Gist options
  • Save lnfel/e46d74a317aef31eb69a185da725ba60 to your computer and use it in GitHub Desktop.
Save lnfel/e46d74a317aef31eb69a185da725ba60 to your computer and use it in GitHub Desktop.
Easy way to convert MKV to MP4 with ffmpeg

How to install FFmpeg on Ubuntu

FFmpeg is a free and open source command line tool for transcoding multimedia files. It contains a set of shared audio and video libraries such as: libavcodec, libavformat, and libavutil. With this tool, you can convert between various video and audio formats, set sample rates and resize videos.

In this document will show you how to install a stable version and the latest version of FFmpeg. This instructions apply for any Ubuntu based distribution, including Linux Mint and Elementary OS.

Prerequisites

In order to be able to add new repositories and install packages on your Ubuntu system, you must be logged in as a user with sudo privileges.

Installing FFmpeg 4.x on Ubuntu

In this section we will provide a step by step instructions about how to install FFmpeg version 4.x on Ubuntu.

The FFmpeg version 4.0 adds a number of new filters, encoders, and decoders. This version is available from the Jonathon F’s PPA.

The steps below describe how to install FFmpeg 4.x on Ubuntu 18.04:

  1. Start by adding the jonathonf/ffmpeg-4 PPA:
sudo add-apt-repository ppa:jonathonf/ffmpeg-4

If you get an error message saying add-apt-repository command not found, install the software-properties-common package.

  1. Once the PPA is added to your system install the FFmpeg package by typing:
sudo apt install ffmpeg
  1. Verify the FFmpeg installation by running the ffmpeg -version command:
ffmpeg -version
  1. The output should look something like this:
ffmpeg version 4.0.3-1~18.04.york0 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)

FFmpeg 4 is now installed on your system and ready to be used.

FFmpeg Examples

In this section, we will look at some basic examples on how to use the ffmpeg utility.

Basic conversion

When converting audio and video files with ffmpeg you do not have to specify the input and output formats. The input file format is auto detected and the output format is guessed from the file extension.

  1. Convert a video file from mp4 to webm:
ffmpeg -i input.mp4 output.webm
  1. Convert an audio file from mp3 to ogg:
ffmpeg -i input.mp3 output.ogg

Specifying codecs

When converting files you can specify the codecs you want to use with the -c option. The codec can be the name of any supported decoder/encoder or a special value copy which simply copies the input stream.

  1. Convert a video file from mp4 to webm using the libvpx video codec and libvorbis audio codec:
ffmpeg -i input.mp4 -c:v libvpx -c:a libvorbis output.webm
  1. Convert an audio file from mp3 to ogg encoded with the libopus codec.
ffmpeg -i input.mp3 -c:a libopus output.ogg

Conclusion

You have successfully installed FFmpeg on your Ubuntu 18.04. You can now visit the official FFmpeg Documentation page and learn how to use FFmpeg to convert and your video and audio files.

If you hit a problem or have feedback, leave a comment below.

Installing FFmpeg 3.x on Ubuntu

The official Ubuntu repositories contain FFmpeg packages that can be installed with the apt package manager. This is the easiest way to install FFmpeg on Ubuntu, however the version included in the repositories may lag behind the latest version of FFmpeg.

At the time of writing this article, the current version of FFmpeg available in the Ubuntu 18.04 repositories is 3.4.4.

Perform the steps below to install FFmpeg 3.x on Ubuntu 18.04:

  1. Start by updating the packages list:
sudo apt update
  1. Next, install FFmpeg by typing the following command:
sudo apt install ffmpeg

To validate that the package is installed properly use the ffmpeg -version command which will print the FFmpeg version:

ffmpeg -version

The output should look something like this:

ffmpeg version 3.4.4-0ubuntu0.18.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)

To print all available FFmpeg’s encoders and decoders type:

ffmpeg -encoders
ffmpeg -decoders

That’s it. FFmpeg 3 is now installed on your system and you can start using it.

Converting mkv to mp4 with ffmpeg

Essentially just copy the existing video and audio stream as is into a new container, no funny business!

The easiest way to "convert" MKV to MP4, is to copy the existing video and audio streams and place them into a new container. This avoids any encoding task and hence no quality will be lost, it is also a fairly quick process and requires very little CPU power. The main factor is disk read/write speed.

With ffmpeg this can be achieved with -c copy. Older examples may use -vcodec copy -acodec copy which does the same thing.

These examples assume ffmpeg is in your PATH. If not just substitute with the full path to your ffmpeg binary.

Single file conversion example

ffmpeg -i example.mkv -c copy example.mp4

Batch conversion example

If you want to batch convert multiple MKV files, you can switch into the directory that contains MKV files and run the following, depending on OS. This can be run directly from command line. All MKV files found in the directory will be converted with their original filename.

Unix one-liner example i.e. MacOS/Linux:
for f in *.mkv; do ffmpeg -i "$f" -c copy "${f%.mkv}.mp4"; done
Windows one-liner example:
for /R %f IN (*.mkv) DO ffmpeg -i "%f" -c copy "%~nf.mp4"

/R is used to recursively search within sub directories at the target location.

If running within a batch script, you will need to double the percentage signs %% in order for the command to run properly.

In both examples, the original .mkv extension is removed from the final output to avoid the filename ending up as example.mkv.mp4 which would be a bit weird, although harmless.

how to convert mkv to mp4 ffmpeg audio stream 2 | ffmpeg merge video and audio | best open source video editor | "ffmpeg -crf" | ffmpeg youtube 1080p 60fps | best configuration ffmpeg for youtube 1080p 60fps 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment