Skip to content

Instantly share code, notes, and snippets.

@baradhiren
Last active September 10, 2022 16:51
Show Gist options
  • Save baradhiren/03d174b974f88088909227f85a5b352e to your computer and use it in GitHub Desktop.
Save baradhiren/03d174b974f88088909227f85a5b352e to your computer and use it in GitHub Desktop.
This script will automatically create necessary environment to download videos using Termux+YoutubeDL.
#!/bin/bash
###############################################################################################################
######
###### Author : Hiren Barad
###### Date : 22-Aug-2018
###### Version : 1.0
###### FileName : Termux_Youtube_Downloader.sh
###### Description : This script will create a folder with name as Youtube in your Phone's main memory,
###### and store all of your downloads there. At the time of downloading it will ask you for
###### the resolution details you can just mention the code displayed on the left side and press enter.
##############################################################################################################
##
#These are all necessary functions to run the script.
##
#This function checks if given package is installed on the target machine or not.
isInstalled(){
packageName=$1
dpkg -s $packageName &> /dev/null
if [ $? -eq 0 ]; then
echo "$packageName is installed!"
return 0
else
echo "$packageName is NOT installed!"
return 1
fi
}
#This function checks if the software exists already and if not installs it.
installThis(){
packageName=$1
isInstalled $packageName
if [ $? -eq 0 ]; then
echo "$packageName already exists."
else
echo "$packageName is not Installed on this device."
echo "Installing $packageName......"
echo "If it asks for input please give 'Y'"
apt-get -y install $packageName
fi
}
#This script waits for given time in seconds
waitAbout(){
sleep $1
}
##
#Main Script body starts here.
##
#All the required variables
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m'
WORKING_DIR=$(echo $(pwd))
YOUTUBE_FOLDER="$WORKING_DIR/storage/shared/Youtube"
YDL_CONFIG_DIR="$WORKING_DIR/.config/youtube-dl"
YDL_CONFIG_FILE="$WORKING_DIR/.config/youtube-dl/config"
CONFIG_FILE_DATA="--no-mtime\n-o /data/data/com.termux/files/home/storage/shared/Youtube/%(title)s.%(ext)s\n-f \"best[height<=1080]\""
BIN_FOLDER="$WORKING_DIR/bin"
URL_OPENER="$WORKING_DIR/bin/termux-url-opener"
echo "Hi, This script will setup an environment to download youtube videos using termux. "
waitAbout 1
echo ""
echo ""
echo ""
echo -e "${BLUE}Here are the things you will need to complete this setup successfully:"
echo -e "${NC} 1. Allow storage access to Termux"
echo -e "${NC} 2. Make sure you have working internet connection."
echo -e "${NC} 3. Now sit tight and enjoy. :)"
echo ""
echo ""
echo ""
read -p " When you are ready just press enter:" j
#Getting Storage Access
termux-setup-storage
#Performing update command
apt-get -y update
#Performing upgrade command
apt-get -y upgrade
#Removing unnecessary packages
apt autoremove
#Installing Python
installThis "python"
#getting youtube-dl
if [ $? -eq 0 ]; then
echo -e "${BLUE}Installing Youtube-dl library...."
pip install youtube-dl
echo -e "${NC}Youtube-dl Installed successfully."
else
echo -e "${RED}Python not installed yet. Rerun the script to install python."
return 1
fi
waitAbout .5
#Creating a directory where videos will be stored
echo -e "${NC}Creating destination folder where your Videos will be stored."
echo -e "${BLUE}By default it will be a folder named 'Youtube' in your Phone Storage"
mkdir $YOUTUBE_FOLDER
waitAbout .5
#Making config folder for Youtube-dl to store its config file
echo -e "${BLUE}Making configuration folder for Youtube-dl....."
mkdir -p $YDL_CONFIG_DIR
waitAbout .5
#Making config data file if it does not exist
if [ ! -f $YDL_CONFIG_FILE ]; then
echo "Creating Youtube-dl Config file...."
touch $YDL_CONFIG_FILE
fi
#Entering data in youtube-dl config file
echo -e "${BLUE}Writing data to youtube-dl config file"
echo "$CONFIG_FILE_DATA" >> $YDL_CONFIG_FILE
waitAbout 1
#Making bin directory in User's home to store termuxurlopener file
if [ ! -d $BIN_FOLDER ]; then
echo -e "${BLUE}Making bin directory.."
mkdir $BIN_FOLDER
fi
#Making termux-url-opener file if it does not exist
if [ ! -f $URL_OPENER ]; then
touch $URL_OPENER
fi
#Entering data in termux-url-opener file
echo -e "${BLUE}Writing data to termux-url-opener file"
echo "#!/bin/bash" > $URL_OPENER
echo "clear\n" >> $URL_OPENER
echo "if [[ \$1 =~ ^.*youtu.*\$ ]] || [[ \$1 =~ ^.*youtube.*\$ ]]; then" >> $URL_OPENER
echo 'echo -e "youtube-dl\\n>${1}\\n"' >> $URL_OPENER
echo "echo \"-F\" > ~/.config/youtube-dl/config" >> $URL_OPENER
echo "youtube-dl \$1" >> $URL_OPENER
echo 'echo -e "\\nPlease enter a format code or extension:"' >> $URL_OPENER
echo "read option" >> $URL_OPENER
echo "echo \"-no-mtime -o" >> $URL_OPENER
echo "~/storage/shared/YouTube/%(title)s.%(ext)s -f \$option\" > ~/.config/youtube-dl/config" >> $URL_OPENER
echo "youtube-dl \$1" >> $URL_OPENER
echo "elif [[ \$1 =~ ^.*nourlselected.*$ ]]; then" >> $URL_OPENER
echo "echo \"ERROR1\"" >> $URL_OPENER
echo "else" >> $URL_OPENER
echo 'echo "Unhandled URL type: $1"' >> $URL_OPENER
echo "fi" >> $URL_OPENER
waitAbout 2
echo -e "${BLUE}Congratulations!!! Your setup is complete."
echo ""
echo ""
echo -e "${RED}:::::::::How to Use the Script:::::::::::::"
echo -e "${BLUE}1. Try to share a video from Youtube app to termux app like you share with whatsapp"
echo -e "${BLUE}2. Enter the resolution code from left hand side in which you want to download the video"
echo -e "${BLUE}3. Let the script do the rest."
echo ""
echo ""
echo ""
echo -e "${RED}Let me know if you face any issues."
echo -e "${BLUE}Happy Downloading. :)"
echo ""
echo ""
read -p "When you are ready just press enter" k
@ChromeGenesis
Copy link

Not working please
Help ASAP

@luiztessadri
Copy link

Not working please
Help ASAP

what is the issue?

@iam-Shashank
Copy link

Does it give option to download audio only?

@NobleSausage
Copy link

Hello was wondering what is the thing supposed to do after u share to termux for me it just opens the app and nothing happens

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment