
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)