Prince Mathew (

Installing youtube-dl in Android using Termux

There are plenty of applications available for Android to download videos from streaming services. Videoder, TubeMate, KeepVid, Snaptube ect are some of the examples. They are not available on Google Play Store because of the Youtube policy regarding downloading of copyrighted material. Problem with these apps are we cannot trust them because of their closed source nature.

This is where youtube-dl excels. It is a tiny command line program which is open to anyone to check its source code on Github. It is a community driven development and most of the above mentioned apps use youtube-dl at the backend. Even some of them are just the GUI version of youtube-dl.

Even though the name suggests only the Youtube, it supports more than 1,000 services and it has a generic downloader built-in which can download any streaming media. Facebook, Instagram, Twitter, Reddit, Twitch, Udemy are some of the supported sites. It can download an entire playlist or chapter which comes handy when you are downloading a course or study material from Udemy or Youtube.

Youtube-dl requires the Python interpreter (2.6, 2.7, or 3.2+), and it is not platform specific. That means you can install it on your Linux distros, Windows, Mac OS or Android.

Installing Termux

To install youtube-dl in Android, we need to install an app called Termux

Developer: Fredrik Fornwall
Price: Free
Developer: Unknown
Price: Free

Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. A minimal base system is installed automatically – additional packages are available using the APT package manager. Using Termux we can install youtube-dl and ffmpeg in Android.

After the installation of Termux open it like any other app. You will be greeted with a command line interface.

You need to provide storage permission to Termux. Either open the app settings of Termux and allow Storage permission or run the command

termux-setup-storageCode language: Bash (bash)

and accept the permission request when prompted.

Then run the command

apt update && apt upgradeCode language: Bash (bash)

apt update will resynchronize the package index files from their sources and apt upgrade will install the newest versions of all packages currently installed on the system from the sources.

Installing Pyhton and FFmpeg

Now we nee to install Python and FFmpeg

apt install pythonCode language: Bash (bash)
apt install ffmpeg

Now we can use pip to install youtube-dl. Pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes. Pip installs additional libraries and packages that are not part of the standard Python library. The Python installer installs pip automatically, so it is ready for you to use now.

Installing Youtube-dl

pip install youtube-dlCode language: Bash (bash)

Configuring Youtube-dl

We need to create a directory for youtube-dl config file. Run the following command:

mkdir -p ~/.config/youtube-dlCode language: Bash (bash)

and we will create a configuration file for YouTube-DL.

vi ~/.config/youtube-dl/configCode language: Bash (bash)

This configuration file works sort of like the preferences section of other apps. Here we can pass various options for youtube-dl such as preferred format, resolution, download location etc. For simplicity’s sake I am not going to explain all the option. Copy and paste the following in vi editor.

# Default Output Directory and Pattern
-o ~/downloads/youtube-dl/%(extractor_key)s/%(extractor_key)s_%(title)s_%(id)s.%(ext)s
-f 'bestvideo[height<=480]+bestaudio/best[height<=480]'Code language: Bash (bash)

Let me explain the above options

-o ~/downloads/youtube-dl/%(extractor_key)s/%(extractor_key)s_%(title)s_%(id)s.%(ext)s
Here we pass the location and naming format of output file. ~/downloads/youtube-dl/%(extractor_key)s/ will save the file in downloads ⇨ youtube-dl ⇨ ‘site_name’.

extractor_key (string) : Key name of the extractor
title (string) : Video title
id (string) : Video identifier
ext (string) : Video filename extension

After pasting the content in config file, you can save it by pressing ESC key. This will switch vi to command mode. Type :wq to save the file and exit vi.

Now you can download videos from various streaming services by opening Termux and typing

youtube-dl [OPTIONS] URL [URL…]Code language: Bash (bash)

For example:

youtube-dl language: Bash (bash)

Bonus Tip

We can create a Bash Script to download the videos using Android share feature. This way you can share an url to Termux using android share and the download will start automatically.

In Termux run the following command to make another directory

mkdir ~/binCode language: Bash (bash)

Then we will create a shell script

vi ~/bin/termux-url-openerCode language: Bash (bash)

Copy an paste the following commands in editor

youtube-dl $urlCode language: Bash (bash)

Save the file by pressing ESC key then type :wq

We need to make script executable

chmod +x ~/bin/termux-url-openerCode language: Bash (bash)

Installing youtube-dl in Ubuntu

Youtube-dl is a command-line program to download videos from and a few more sites (approximately 1,155 listed sites). It requires the Python interpreter (2.6, 2.7, or 3.2+), and it is not platform specific. That means you can install it on your Unix box, Windows or Mac OS.

Installation in Windows

In Windows, you easily install it with Chocolatey with all the dependencies using the command:

choco install youtube-dlCode language: PowerShell (powershell)

Installation in Linux

To install it right away for all Linux users, type:

sudo apt-get install youtube-dlCode language: Bash (bash)

You can also use pip:

sudo -H pip install --upgrade youtube-dlCode language: Bash (bash)

Youtube-dl uses -f bestvideo+bestaudio/best as the default format selection which results in downloading bestvideo and bestaudio separately. So youtube-dl needs ffmpeg or avconv to mux the files together into a single file giving the best overall quality available.

sudo apt-get install ffmpegCode language: Bash (bash)

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

ffmpeg -versionCode language: Bash (bash)

The output should look something like this:

ffmpeg version 4.2.2-1ubuntu1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 9 (Ubuntu 9.3.0-3ubuntu1)Code language: Bash (bash)

Configuring Youtube-dl

Now that we have installed all the neccessary packages, it is time to configure the youtube-dl by placing any supported command line option to a configuration file. This will avoid the need to pass the command line arguments each and everytime.

First we will create a config file and move it to /etc

touch youtube-dl.confCode language: Bash (bash)

Open the file in your favourite command line text editor. I use vi here,

vi youtube-dl.confCode language: Bash (bash)

This is what my config file looks like. Copy and paste this in your config file

# Default Output Directory and Pattern
-o ~/downloads/youtube-dl/%(extractor_key)s/%(extractor_key)s_%(title)s_%(id)s.%(ext)s
-f 'bestvideo[height<=480]+bestaudio/best[height<=480]'
--download-archive ~/.config/youtube-dl/downloaded.txtCode language: Bash (bash)

I will explain the above options

-o ~/downloads/youtube-dl/%(extractor_key)s/%(extractor_key)s_%(title)s_%(id)s.%(ext)s
Here we pass the location and naming format of output file. ~/downloads/youtube-dl/%(extractor_key)s/ will save the file in downloads ⇨ youtube-dl ⇨ ‘site_name’.

extractor_key (string) : Key name of the extractor
title (string) : Video title
id (string) : Video identifier
ext (string) : Video filename extension

Full list of template arguments
  • id (string): Video identifier
  • title (string): Video title
  • url (string): Video URL
  • ext (string): Video filename extension
  • alt_title (string): A secondary title of the video
  • display_id (string): An alternative identifier for the video
  • uploader (string): Full name of the video uploader
  • license (string): License name the video is licensed under
  • creator (string): The creator of the video
  • release_date (string): The date (YYYYMMDD) when the video was released
  • timestamp (numeric): UNIX timestamp of the moment the video became available
  • upload_date (string): Video upload date (YYYYMMDD)
  • uploader_id (string): Nickname or id of the video uploader
  • channel (string): Full name of the channel the video is uploaded on
  • channel_id (string): Id of the channel
  • location (string): Physical location where the video was filmed
  • duration (numeric): Length of the video in seconds
  • view_count (numeric): How many users have watched the video on the platform
  • like_count (numeric): Number of positive ratings of the video
  • dislike_count (numeric): Number of negative ratings of the video
  • repost_count (numeric): Number of reposts of the video
  • average_rating (numeric): Average rating give by users, the scale used depends on the webpage
  • comment_count (numeric): Number of comments on the video
  • age_limit (numeric): Age restriction for the video (years)
  • is_live (boolean): Whether this video is a live stream or a fixed-length video
  • start_time (numeric): Time in seconds where the reproduction should start, as specified in the URL
  • end_time (numeric): Time in seconds where the reproduction should end, as specified in the URL
  • format (string): A human-readable description of the format
  • format_id (string): Format code specified by --format
  • format_note (string): Additional info about the format
  • width (numeric): Width of the video
  • height (numeric): Height of the video
  • resolution (string): Textual description of width and height
  • tbr (numeric): Average bitrate of audio and video in KBit/s
  • abr (numeric): Average audio bitrate in KBit/s
  • acodec (string): Name of the audio codec in use
  • asr (numeric): Audio sampling rate in Hertz
  • vbr (numeric): Average video bitrate in KBit/s
  • fps (numeric): Frame rate
  • vcodec (string): Name of the video codec in use
  • container (string): Name of the container format
  • filesize (numeric): The number of bytes, if known in advance
  • filesize_approx (numeric): An estimate for the number of bytes
  • protocol (string): The protocol that will be used for the actual download
  • extractor (string): Name of the extractor
  • extractor_key (string): Key name of the extractor
  • epoch (numeric): Unix epoch when creating the file
  • autonumber (numeric): Five-digit number that will be increased with each download, starting at zero
  • playlist (string): Name or id of the playlist that contains the video
  • playlist_index (numeric): Index of the video in the playlist padded with leading zeros according to the total length of the playlist
  • playlist_id (string): Playlist identifier
  • playlist_title (string): Playlist title
  • playlist_uploader (string): Full name of the playlist uploader
  • playlist_uploader_id (string): Nickname or id of the playlist uploader

You can customize the output file name or directory using these template arguments. Really comes handy if you are a data hoarder like me.

-f 'bestvideo[height<=480]+bestaudio/best[height<=480]'
-f or --format is the general syntax for format selection. Above argument will download the best video with the maximum of 480p height.

Format selection examples

# Download best mp4 format available or any other best if no mp4 available
youtube-dl -f ‘bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best’

# Download best format available but no better than 480p
youtube-dl -f ‘bestvideo[height<=480]+bestaudio/best[height<=480]'

# Download best video only format but no bigger than 50 MB
youtube-dl -f ‘best[filesize<50M]'

# Download best format available via direct link over HTTP/HTTPS protocol
youtube-dl -f ‘(bestvideo+bestaudio/best)[protocol^=http]’

# Download the best video format and the best audio format without merging them
youtube-dl -f ‘bestvideo,bestaudio’ -o ‘%(title)s.f%(format_id)s.%(ext)s’

--download-archive ~/.config/youtube-dl/downloaded.txt
--download-archive FILE record the IDs of all downloaded videos in a file. This way youtube-dl downloads only the videos not listed in the archive file and skip duplicates.

After you are done with all the customization, you can save the config file. To save a file and/or leave vi you must switch to command mode, if you are not already in it. You can always enter command mode by pressing ESC key. There are several ways to save files and leave vi, each of which begins with you typing a colon character (:).

Save the current file (write file) but do not exit. This command fails if the file is read-only.
Quit vi. This command fails if you have made changes to a file since the last time you saved it.
Save the current file and exit vi. The command :x is equivalent to this, except that it only saves the current file if you have changed it.

Now we need to move the config file to /etc to use it as the global config.

sudo cp ~/youtube-dl.conf /etcCode language: Bash (bash)

You can directly create a config file and save it in /etc directory rather than going this route.

Usage Example

Now you can download videos using the command

youtube-dl [OPTIONS] URL [URL…]Code language: Bash (bash)

For example:

youtube-dl language: Bash (bash)

You can download multiple videos by passing urls separated by space.