How to merge video and audio with youtube-dl

Youtube-DL is a powerful tool that serves almost all your needs when you want to download a video from the internet. It cannot only perform the crawling, parsing, and finding real video URLs, youtube-dl can also be used in conjunction with other open-source programs such as FFmpeg. This article will show you how to manually merge separate video and audio files into one single file.

To follow this tutorial, you obviously need youtube-dl installed on your Windows/Linux/macOS machine. In addition to that, FFMPEG also needs to be present in the system path.

An alternative to FFMPEG supported by youtube-dl is avconv, which is just another fork of FFMPEG with a different set of features. In this article, we will only use FFMPEG as it is more feature completed and widely supported among the open-source community.

Step 1: Install FFMPEG

On FFMPEG homepage, there are official packages for major distros such as Debian/Ubuntu or Fedora/RHEL/CentOS.

Install FFMPEG in Fedora/RHEL/CentOS

If you are running Fedora/RHEL/CentOS, you can install FFMPEG directly from the RPM Fusion repository.

The RPM Fusion repository depends on the EPEL software repository. Please ensure EPEL is enabled by typing:

sudo yum install epel-release
Code language: Bash (bash)

Then you need to install the RPM packages to enable RPM Fusion

sudo yum localinstall --nogpgcheck
Code language: Bash (bash)

After that, install FFMPEG from RPM Fusion using this command :

sudo yum install ffmpeg ffmpeg-devel
Code language: Bash (bash)

Install FFMPEG in Debian/Ubuntu

If you are running Ubuntu/Debian, you can simply install it using apt as follows :

sudo apt-get install ffmpeg -y
Code language: Bash (bash)

You can also get FFMPEG from snap, but I personally do not recommend this method. There have been numerous problems reportedly caused by snap’s heavy limitations of accessing system resources such as the infamous “permission denied”. But if you still want easy installation, here’s the command to install FFMPEG from snap :

sudo snap install ffmpeg
Code language: Bash (bash)

Install FFMPEG in Windows.

Windows users will have to get community builds from or BtbN to install FFMPEG.

After downloading and extracting the build, you have to browse to bin folder, then copy them to one of the system paths. If you are unsure about system paths, the easiest way is to copy ffmpeg.exe,ffplay.exe and ffprobe.exe into C:WindowsSystem32.

Install FFMPEG in macOS

MacOS users can easily install FFMPEG using Homebrew.

Once you have Homebrew installed, perform the installation of FFMPEG from the Terminal with the following command :

brew install ffmpeg
Code language: Bash (bash)

Step 2 : Merge audio and video using youtube-dl

youtube-dl can automatically detect whether ffmpeg is installed in the system and use it as part of the script to join/merge the separate video and audio files.

This functionality is often used when you download from Youtube. New videos from Youtube are streamed to the user in HD as separate files.

Let’s look at this example where we automatically merge audio and video of two formats into a single file :

youtube-dl -f bestvideo+bestaudio
Code language: Bash (bash)

The important part of the command is the argument for joining files : -f bestvideo+bestaudio.

The output should look something like this :


As you can see from the image, downloaded audio and video in the best available formats have been automatically downloaded and merged into one single MKV file named Duterte's Drug War (full film) _ FRONTLINE-qugduxazBBg.mkv.

Youtube-dl by default outputs merged files in Matroska (MKV) container. If you want to output into a different format, pass a --merge-output-format following by a supported file extension like this :

youtube-dl -f bestvideo+bestaudio --merge-output-format mp4
Code language: Bash (bash)

Supported formats are mkv, mp4, ogg, webm, flv.

Leave a Comment