Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Sharing youtube video to Metube on Android

I recently established my own local metube instance, which facilitates passing links to youtube-dl (er, yt-dlp). It works very well, particularly when I got the watchtower bit running so it reloads with an updated image, because yt-dlp is a fast-moving target.

So, in order to get this link as a sharing target in Android, I needed to set up a few things.

On the Android device in question, install Termux from F-Droid. No additional packages/plugins are needed.

In the terminal on the android device:

mkdir -p bin
cd bin
nano metube.sh

#!/bin/sh
#set -x
curl --insecure --request POST --url 'https://server3.ipa.example.com/metube/add' --data "{\"url\":\"${1}\", \"quality\":\"best\"}"
printf 'press enter to leave. '
read none

chmod +x metube.sh
ln -sf metube.sh termux-url-opener

The symlink termux-url-opener is defined in the official Termux docs.

Now, for any link I wish to send, select the "Share" button, and select Termux. I added a prompt so I can see the output of the request, in case it fails so I can investigate.

Alternatives

Use the bookmarklet in a desktop web browser:

javascript:(function(){xhr=new XMLHttpRequest();xhr.open("POST","https://server3.ipa.example.com/metube/add");xhr.send(JSON.stringify({"url":document.location.href,"quality":"best"}));xhr.onload=function(){if(xhr.status==200){alert("Sent to metube!")}else{alert("Send to metube failed. Check the javascript console for clues.")}}})();

One additional parameter metube uses:

curl --request POST --url 'http://localhost:8081/add' --data '{"url":"https://www.youtube.com/watch?v=XXXXXXX", "quality":"720p", "format":"mp4"}

Making a PWA with a sharing target

If Termux is unavailable, I could try making a progressive web app.

  1. https://chodounsky.com/2019/03/24/progressive-web-application-as-a-share-option-in-android/
  2. https://developer.chrome.com/articles/web-share-target/
  3. https://stackoverflow.com/questions/38189160/can-a-progressive-web-app-be-registered-as-a-share-option-in-android

References

  1. https://wiki.termux.com/wiki/Intents_and_Hooks
  2. https://github.com/alexta69/metube/issues/71
  3. https://github.com/alexta69/metube/issues/133

Comments