
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
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-storage
Code language: Bash (bash)
and accept the permission request when prompted.
Then run the command
apt update && apt upgrade
Code 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 python
Code 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-dl
Code 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-dl
Code language: Bash (bash)
and we will create a configuration file for YouTube-DL.
vi ~/.config/youtube-dl/config
Code 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 extractortitle
(string) : Video titleid
(string) : Video identifierext
(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 https://www.youtube.com/watch?v=dQw4w9WgXcQ
Code 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 ~/bin
Code language: Bash (bash)
Then we will create a shell script
vi ~/bin/termux-url-opener
Code language: Bash (bash)
Copy an paste the following commands in editor
#!/bin/bash
url=$1
youtube-dl $url
Code language: Bash (bash)
Save the file by pressing ESC
key then type :wq
We need to make script executable
chmod +x ~/bin/termux-url-opener
Code language: Bash (bash)

Installing youtube-dl in Ubuntu
Youtube-dl is a command-line program to download videos from YouTube.com 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-dl
Code language: PowerShell (powershell)
Installation in Linux
To install it right away for all Linux users, type:
sudo apt-get install youtube-dl
Code language: Bash (bash)
You can also use pip:
sudo -H pip install --upgrade youtube-dl
Code 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 ffmpeg
Code language: Bash (bash)
To validate that the package is installed properly use the ffmpeg -version
command which prints the FFmpeg version:
ffmpeg -version
Code 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.conf
Code language: Bash (bash)
Open the file in your favourite command line text editor. I use vi here,
vi youtube-dl.conf
Code 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.txt
Code 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
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.
--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 (:
).
:w
Save the current file (write file) but do not exit. This command fails if the file is read-only.:q
Quit vi. This command fails if you have made changes to a file since the last time you saved it.:wq
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 /etc
Code 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 https://www.youtube.com/watch?v=dQw4w9WgXcQ
Code language: Bash (bash)
You can download multiple videos by passing urls separated by space.