Download YouTube Videos using Termux

themountain
2 min readAug 31, 2018

--

I like termux a lot, because of all the things that it supports and all the things that it can do. Here I will be mentioning some of the steps to Download YouTube videos directly from YouTube app using Termux. So, let’s get going.

  • First of all download the application from here.(Sorry iPhone users. 😝)
  • Now give termux storage access by running following command :

termux-setup-storage (Allow when android pops for confirmation)

This step is necessary as we are going to store our downloaded videos on our Phone Storage.

  • Download python from command line by executing: apt install Python
  • Now install YouTube-DL by executing: pip install youtube-dl
  • Once that is done let’s create a folder where videos will be stored:mkdir /data/data/com.termux/files/home/storage/shared/YouTube
  • pkg install nano
    Note​: To edit things in nano you might need a powerful keyboard. Install ​Hacker’s Keyboard​ from here.
  • mkdir -p ~/.config/youtube-dl
  • Now to Save the file press Ctrl+x. It will ask for a confirmation if you want to save the file. Type Y. After that press enter and nano will save the file.
  • Following steps will help you make the termux-url-opener file which will do all the magic.
  • mkdir ~/bin
  • cd ~/bin
  • nano termux-url-opener

This command will open nano editor just like notepad in windows. Just copy paste the below given things in it.
#! /bin/bash
clear
if [[ $1 =~ ^.*youtu.*$ ]] || [[ $1 =~ ^.*youtube.*$ ]]; then
echo -e “youtube-dl\n> ${1}\n”
echo “-F” > ~/.config/youtube-dl/config
youtube-dl $1
echo -e “\nPlease enter a format code or extension:”
read option
echo “-no-mtime -o
/data/data/com.termux/files/home/storage/shared/YouTube/%(title)s.%(ext)s -f $option” >
~/.config/youtube-dl/config
youtube-dl $1
elif [[ $1 =~ ^.*nourlselected.*$ ]]; then
echo “ERROR1”
else
echo Unhandled URL type: $1
fi

  • Now Ctrl+x and it will ask for a confirmation to save the file. Press Y and enter to save the file.
  • That’s it. Now go to your YouTube app and share the video you want to download with termux. It will open a new window and ask you which format you want to download your video in. Enter the code of the resolution in which you want to download the video and the download will start. Happy Downloading.

Note: If you don’t want to do all these things manually you can directly get a shell script which i created from here. Let me know what you think of this post in comments down below.

--

--