Skip to content

Instantly share code, notes, and snippets.

@LordH3lmchen
Last active September 14, 2024 10:29
Show Gist options
  • Save LordH3lmchen/dc35e8df3dc41d126683f18fe44ebe17 to your computer and use it in GitHub Desktop.
Save LordH3lmchen/dc35e8df3dc41d126683f18fe44ebe17 to your computer and use it in GitHub Desktop.
termux-url-opener
#!/data/data/com.termux/files/usr/bin/bash
#
# This is a termux-url-opener script to do diffrent tasks on my Android phone
#
#
#
# How to use this script
#############################
#
# Install git
# ➜ ~ pkg install git
#
# clone this script
# ➜ ~ git clone https://gist.github.com/dc35e8df3dc41d126683f18fe44ebe17.git $HOME/termux-url-opener
#
# Create the bin directory
# ➜ ~ mkdir bin
#
# Link the script
# ➜ ~ ln -s $HOME/termux-url-opener/termux-url-opener $HOME/bin/termux-url-opener
# ➜ ~ ln -s $HOME/termux-url-opener/termux-url-opener $HOME/bin/termux-file-editor
#
# run the following command to enable the termux storage features
# ➜ termux-setup-storage
#
# https://wiki.termux.com/wiki/Termux-setup-storage
#
# the script downloads the needed tools on first use.
for PACKAGE in wget ffmpeg python
do
which $PACKAGE > /dev/null
if [ ! $? -eq 0 ]; then
echo "Installing $PACKAGE"
pkg install $PACKAGE
fi
done
which transmission-daemon > /dev/null
if [ ! $? -eq 0 ]; then
pkg install transmission
fi
for PYPI_PKG in yt-dlp scdl
do
pip show $PYPI_PKG
if [ ! $? -eq 0 ]; then
pip install $PYPI_PKG
fi
done
if [! -w $HOME/Downloads ]; then #create Downloads Link for transmission
ln -s $HOME/storage/downloads $HOME/Downloads
fi
url=$1
echo "What should I do with $url ?"
echo "y) download youtube video to movies-folder"
echo "u) download youtube video and convert it to mp3 (music-folder)"
echo "s) download with scdl (soundcloud)"
echo "t) download torrent"
echo "w) wget file to download-folder"
echo "x) nothing"
read CHOICE
case $CHOICE in
y)
yt-dlp -o "/storage/emulated/0/Movies/%(title)s.%(ext)s" $url
;;
u)
echo "Artist"
read artist
echo "Title"
read title
echo "Album"
read album
yt-dlp --extract-audio --audio-format mp3 --output "/storage/emulated/0/Music/$artist-$title.%(ext)s" $url
mid3v2 -a $artist -t $title -A $album /storage/emulated/0/Music/$artist-$title.mp3
;;
s)
scdl -l $url --path /storage/emulated/0/Music
echo "s need some work"
;;
w)
cd ~/storage/downloads
wget $url
;;
t)
echo "running torrents"
transmission-remote 127.0.0.1 -l
if [ ! $? eq 0 ]; then # start daemon if necessary
transmission-daemon
sleep 3
fi
echo "use transmission-remote to manage your downloads (read the manpage)"
transmission-remote 127.0.0.1 -a $(cat $url)
;;
x)
echo "bye"
;;
esac
@Dan1jel
Copy link

Dan1jel commented May 26, 2020

Why didn't I think about git clone.... I think "copy /paste" is not that great because something was broken when i paste it using termux. But when I was on my ubuntu, I noticed "raw" button up top, after I copy paste in ubuntu and then sent the file typ my phone using scp. I then modyfied it for my likings and now it works great :) but yes, copy on the phone is not recommended, GitHub clone is maybe the way to go. Thanks for the reply!

@titidelain
Copy link

Thanks for this script. I use a version modified by me, to download complete playlists, either in video or converted to audio. I added the download of metadata for the Id3 tag of the mp3. But there since my last modifications I have an error "/data/data/com.termux/files/home/bin/termux-url-opener:71: unmatched" and there I have no idea of the problem, and therefore not solutions. I updated the packages, it's still the same error.

@Dan1jel
Copy link

Dan1jel commented Dec 15, 2020

Thanks for this script. I use a version modified by me, to download complete playlists, either in video or converted to audio. I added the download of metadata for the Id3 tag of the mp3. But there since my last modifications I have an error "/data/data/com.termux/files/home/bin/termux-url-opener:71: unmatched" and there I have no idea of the problem, and therefore not solutions. I updated the packages, it's still the same error.

I'm no expert, but "71" isn't that row 71 in your script that is causing this error?!

@titidelain
Copy link

I'm no expert, but "71" isn't that row 71 in your script that is causing this error?!

Thank you for your reply. but I had already looked at line 71 and even though I comment out this part of the code I got the same error.

@Dan1jel
Copy link

Dan1jel commented Dec 15, 2020

I'm no expert, but "71" isn't that row 71 in your script that is causing this error?!

Thank you for your reply. but I had already looked at line 71 and even though I comment out this part of the code I got the same error.

Hmm well then it's hard to know what cause the error. Like I said I'm no expert but if you can share your code maybe someone can lookin to it?

@titidelain
Copy link

Ok, I can't get my code into the thread. I put a share link from my Google Drive cloud.

https://drive.google.com/file/d/1NPwQmNAGWnV_XhaoKFOovXtlqQ6AwbOE/view?usp=sharing

@canxx99
Copy link

canxx99 commented Jan 20, 2021

@titidelain try this to download videos

$ apt update && apt upgrade -y
$ apt install python -y
$ pip install --upgrade pip
$ pip install youtube-dl
$ mkdir -p /sdcard/Youtube
$ mkdir ~/bin
$ echo 'youtube-dl $1' > ~/bin/termux-url-opener
$ mkdir -p ~/.config/youtube-dl
$ echo '--no-mtime -o /sdcard/Youtube/%(title)s.%(ext)s -f "best[height<=1080]"' > ~/.config/youtube-dl/config

@titidelain
Copy link

Good evening. Thank you for your comment. So I placed the orders in a terminal. So I have the video download in the Youtube folder. On the other hand the Youtube folder is on the phone's memory and not on the external card (mkdir -p / sdcard / Youtube ???), it is perhaps due to the particular management of Android? But it works can you tell me what led you to have me test this "simplified" code?
Thank you

@canxx99
Copy link

canxx99 commented Jan 21, 2021

@titidelain
/sdcard on Android is the command used to use the phone's internal memory. The /sdcard here is not showing an external card.
So /sdcard is your phone's internal storage directory.

/storage/emulated/0/ = /sdcard

These two commands point to the same directory.

@titidelain
Copy link

Hi.
Thank you.
I am not a programming expert. Can you explain the command "echo 'youtube-dl $ 1'> ~ / bin / termux-url-opener" to me. I understand the command "echo" which will "write" the line "youtube-dl $ 1" but this $ 1 that I do not understand. I will also see to better understand the "termux-url-opener" file. And if I understand your code correctly, it is nothing more or less than creating a config file for youtube-dl?
Thank you.

@titidelain
Copy link

I answer myself. $ 1 represents the url of the link, well from what I understand. From then on we could also write:
$ apt update && apt upgrade -y
$ apt install python -y
$ pip install --upgrade pip
$ pip install youtube-dl
$ mkdir -p /sdcard/Youtube
$ mkdir ~/bin
$ echo 'youtube-dl --no-mtime -o /sdcard/Youtube/%(title)s.%(ext)s -f "best [height <= 1080]"' $1' > ~ / bin / termux- url-opener

Are we not using the youtube-dl "config" file? That's right, isn't it?
Thank you.

@titidelain
Copy link

Good evening. It's good my script works. Resuming what I had written, and with your answers, I resumed the script on my phone. I noticed a lot of writing errors because of .... my bluetooth keyboard. He repeated characters to me !!! I rewrote with the virtual keyboard of the phone by simplifying and it works!
Thanks to those who answered me, problem solved!

@nasseef20
Copy link

Hello,
Since I cannot do a pull request so I'm suggesting that you add this function right after url=$1:

function yt_shorts(){
    if [[ "$url" == *"shorts"* ]]; then
        url=${url%?feature=share}
        url=${url#https://youtube.com/shorts/}
        url="https://youtube.com/watch?v="$url
    fi
}

And then call yt_shorts right before the youtube-dl call in switch cases y) and u).

Reason: youtube-dl cannot work on "youtube shorts" links directly, so some modification is needed given that the url is from a youtube short video.

@Dan1jel
Copy link

Dan1jel commented Sep 17, 2022

I'm not OP but could you maybe do a gits so i could see how it should be?

@nasseef20
Copy link

Copy link

ghost commented Feb 20, 2023

I have a problem where s the script bro , sorry I am new In this field , Problem = when I give command nvim termux-url-opener
Here comes a interface with purple ~ sign .
I don't what to do next .pls sir tell me.

@LordH3lmchen
Copy link
Author

Neovim works like vi or vim.

Use your preferred Editor. nano is probably simpler.

Instead of youtube_dl you could use yt-dlp its a fork that works better.

If you don't want to copy pasta the script. Install git and clone the gist.

@TehDomic
Copy link

Does anyone know any forks that use pre-installed bash instead of zsh?

@LordH3lmchen
Copy link
Author

LordH3lmchen commented Mar 20, 2023

Does anyone know any forks that use pre-installed bash instead of zsh?

I think it should work with bash as it is. Just replace the zsh with bash. There is nothing zsh specific in this script. Zsh and Bash are POSIX shells.

#!/data/data/com.termux/files/usr/bin/bash

I've updated the script.

I switched to yt-dlp. It is more reliable and works with more websites. The script installs needed tools automatically now. I've added transmission as option t) in the script

Also if you install the script with git you can update it with "git pull" to the latest version. I modified the instructions to use git. This should eliminate the problems with encoding issues. (Who uses MS Windows anyway?)

install git & copy the git clone command from the instructions o a termux session

@TehDomic
Copy link

Cool, I'll give it a try this week and see if I love it. Thanks for the script ♥

@naranyala
Copy link

open source is awesome

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