Downloading YouTube videos using Termux and youtube-dl on Android
Last updated: Dec 8, 2023
Problem
How to download YouTube videos from my Android phone to watch offline.
A solution
Use the youtube-dl command line utility from the Termux app.
Background
I work on ships where watching YouTube videos is difficult. So I want to save videos to watch offline on my Android phone. The Android apps I’d used to do this stopped being able to download YouTube videos. I use youtube-dl from my Linux laptop to download from YouTube. The Termux app on Android gives a Linux like terminal which enables me to install and run youtube-dl.
Setup
Install Termux from the F-Droid app. Don’t install Termux from Google Playstore. The version on Playstore is old and not supported. There are many websites giving tutorials on Termux.
Open up Termux. Now we need to install the Python programming language which is used to install and run youtube-dl. From the command line in Termux type:
pkg install python3
Wait a minute or two while python is installed.
Python has a package installation tool called pip. We use this to install youtube-dl.
Type:
pip3 install youtube-dl
Almost there. To download a YouTube video, you copy and paste the YouTube video url and use it with the youtube-dl command like this:
youtube-dl <youtube-video-url>
Marvel as the video is downloaded to your Termux drive. But… how do we view this video? Use the Files file explorer app that comes with Android. The Termux drive shows up on this. Many file browsers don’t allow access to the Termux drive. I don’t know why either. You can access your newly downloaded video on the Termux drive.
Keeping youtube-dl up to date
Often youtube-dl stops working as YouTube changes something somewhere. Updating youtube-dl fixes this. Update youtube-dl using this command:
pip3 install youtube-dl -U
Extra
There are many options with youtube-dl to can specify the quality of your video and to rename it. You can download only audio which is useful for music videos. You can give a playlist link to youtube-dl and all of the playlist will be downloaded. Have a look for tutorials on youtube-dl if you want to learn more.
The alias I set up in Linux to download videos at the top quality available and rename them is:
alias youtubevid='youtube-dl -f 'best' --ignore-errors --output "%(uploader)s_%(title)s.%(ext)s"'
The alias I use to download audio only at the best quality available and rename the files is:
alias youtubemp3='youtube-dl --extract-audio --audio-format mp3 --ignore-errors --output "%(uploader)s_%(title)s.%(ext)s"'