#!/bin/sh loop(){ while true;do sleep 4 pos=$(echo '{ "command": ["get_property", "time-remaining"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^\.]*).*_\1_p') while [ -n "$pos" ];do pos=$(echo '{ "command": ["get_property", "time-remaining"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^\.]*).*_\1_p') [ -z "$1" ] || printf "\033[2K\r$pos" sleep 2 done pos=$(echo '{ "command": ["get_property", "time-remaining"]}' | socat - /tmp/mpvsocket | sed -nE 's_.*data":([^\.]*).*_\1_p') [ -z "$pos" ] && pgrep mpv > /dev/null && play "$(cat "$logdir/next" | sed -n 2p | cut -f2)" done } play(){ id=$(printf "$1" | cut -d"=" -f2 | cut -d"/" -f4 | cut -d'&' -f1) [ -z "$id" ] && printf "[ youtube ] Invalid link\n" && exit 1 json="{ \"context\": { \"client\": { \"clientName\": \"ANDROID\", \"clientVersion\": \"17.31.35\", \"androidSdkVersion\": 30, \"userAgent\": \"$agent\", \"hl\": \"en\", \"timeZone\": \"UTC\", \"utcOffsetMinutes\": 0 } }, \"videoId\": \"$id\", \"playbackContext\": { \"contentPlaybackContext\": { \"html5Preference\": \"HTML5_PREF_WANTS\" } }, \"contentCheckOk\": true, \"racyCheckOk\": true }" #main logic curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.115 Safari/537.36" -s "https://www.youtube.com/watch?v=$id&bpctr=9999999999&has_verified=1" -c "$cookie" | sed 's/compactVideoRenderer/\n/g' | sed -nE 's_.*videoId":"([^"]*)".*label":"([^"]*)".*_\2\t\1_p' | sed "/$id/d" > "$next_data" data=$(curl -X POST -A "$agent" -s "https://www.youtube.com/youtubei/v1/player?key=AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w&prettyPrint=false" -H "content-type:application/json" -H "x-youtube-client-name:21" -H "x-youtube-client-version:17.31.35" -d "$json" -b "$cookie" | sed -nE 's_.*itag":[0-9]*,"url":"([^"]*)".*"title":"([^"]*)".*_\1\n\2_p') title=$(printf "%s" "$data" | tail -1) audio_url=$(printf "%s" "$data" | head -1) printf "Name >> %s\n" "$title" printf "referer >> %s\n" "$*" printf "Audio URL >> %s\n" "$audio_url" curl -s "https://i.ytimg.com/vi/$id/default.jpg" -o "$logdir/default.jpg" && notify-send -i "$logdir/default.jpg" "Now Playing" "$title" -t 5000 pgrep mpv > /dev/null || (setsid -f mpv --really-quiet --input-ipc-server=/tmp/mpvsocket --idle --quiet >/dev/null && sleep 3) echo "{ \"command\": [\"loadfile\", \"$audio_url\", \"replace\"] }" | socat - /tmp/mpvsocket echo "currently playing : $title" > "$logdir/current" } search(){ context=$(curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.115 Safari/537.36" -s "https://www.youtube.com/" -c "$cookie" | sed -nE 's|.*INNERTUBE_CONTEXT":(.*),"INNERTUBE_CONTEXT_CLIENT_NAME.*|\1|p') json_search="{ \"context\" : $context, \"query\": \"$1\", \"params\": \"EgWKAQIIAWoKEAMQBBAJEAoQBQ%3D%3D\" }" curl -X POST -A "$agent" -s "https://www.youtube.com/youtubei/v1/search?key=AIzaSyA8eiZmM1FaDVjRy-df2KTyQ_vz_yYM39w&prettyPrint=false" -H "content-type:application/json" -H "x-youtube-client-name:21" -H "x-youtube-client-version:17.31.35" -d "$json_search" -b "$cookies" | sed 's/videoRenderer/\n/g' | sed -nE 's_.*videoId":"([^"]*)".*\],"accessibility":\{"accessibilityData":\{"label":"([^"]*)".*_\2\t\1_p' | bemenu --fn 'Roboto 15' -i -c -W 0.5 -B 3 -p "Play: " -l 30 | cut -f2 } search_play(){ res=$(search "$1") play "$res" } logdir="${XDG_CACHE_HOME:-$HOME/.cache/yt-music}" [ -d "$logdir" ] || mkdir "$logdir" cookie="$logdir/cookies" next_data="$logdir/next" agent="com.google.android.apps.youtube.music/5.16.51 (Linux; U; Android 1$(head /dev/urandom | tr -dc '0-3' | cut -c1)) gzip" $1 "$2"