Visit Jeremy's Blog.
Go Back > Blogs > linux-related notes
User Name


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Another "Bash script for queueing youtube links in mpv", adapted for hovering + shortcut keys on possibly multiple contexts

Posted 05-25-2019 at 03:46 PM by the dsc
Updated 10-11-2020 at 06:05 PM by the dsc

This script is partly based on this one, referred on the post title:

It was originally an independent development, so it relies in some aspects more on mpv.conf (to define its input pipe/fifo file), so one may want to modify it according to whatever preference regarding that.

The main difference is a gimmick to get the URL from where the cursor is hovering at the moment. For while it has only the code for doing that for links on Chrome/Chromium, but it can be reasonably easily extended. It just requires to see what the shortcut keys for that would be in a given browser/context, phrase it as xdotool expressions, and how would this window be detected by xdtool's "activewindow windowname" funtion.

Then, of course, one needs to have the script set up to be called by some keyboard shortcut on whatever desktop environment one is using. At least for Openbox it works, I assume it must work on most other X11 DEs, can't tell anything abouy Wayland or whatever else there may be these days.

I also have two slightly different versions, one that will run a default mpv, and another that has a longer command line that overrides defaults, for whenever I'm interested only in the audio itself, low quality. The enqueued URLs will "inherit" the MPV instance and such parameters, regardless of what script is called, though, there may be some room for improvement here, but for me it's good enough as it is.


echo | xsel -i          # clears the clipboard

# add "case" section to change shortcuts for other browsers depending on which one is under the cursor

# use the output of this command to add new context windows on the "case" conditions:

 window=$(xdotool getactivewindow getwindowname)

 case $window in

*"- Chrom"*)          # not 100% fail-proof, but the odds are good enough for me

# I have the impression I need a stupid loop like that because my PC can be somewhat slow and sluggish, and so
# the browser won't always respond readily enough to xdotool's commands. If you have anything newer than a
# 10 year old PC you may want to try removing the loop and even using an all-in-one xdotool command

until xclip -o | grep "http" ; do             

# may be adapted along the lines of grep "http\|.mp4\|.mkv\|.avi" and so on, for http-less contexts such as file browsers

xdotool click 3 ; xdotool key e

# xdotool is right-clicking (button 3) and then hitting key "e" on keyboard, which is Chrome's linux shortcut to copy URL
# that will vary from browser to browser or different applications


((a>10)) && exit 1



*"- Falkon"*) # somewhat buggy/works less smoothly, unfortunately, I think

#env DISPLAY=:0
until ((okay==1)) ; do

xdotool click 3 ; xdotool key C

xsel -b | grep http  > /dev/shm/mpvurl
grep http  /dev/shm/mpvurl && okay=1

#; sleep 0.3 ; xdotool key Return

a=$((a+1)) #&& notify-send "fail:" "$(xsel -b | grep http)" && exit 1

((a>10)) && notify-send fail && exit 1


notify-send okay
#xsel -b | grep http  > /dev/shm/mpvurl


*) exit 0

# just exits without error if it's invoked while the cursor is not hovering anything to which it's configured to deal with
# change to 1 if for some reason exitting with error is preferable
# perhaps a notify-send "this script doesn't know how to deal with this context" would be desirable



url=$(xclip -o)


            if [[ ! -p $socketfile ]] ; then
                mkfifo $socketfile

if xclip -o | grep "http\|.mp3\|.webm\|.mp4\|mkv\|m4a\|flv" ; then

    if ! pidof mpv ; then            # if there's no MPV instance running, will open a new one

    mpv --ytdl-format worstaudio/249/250/251/bestaudio/17/36/92/5/18/best --force-window --no-keepaspect-window --script-opts=osc-vidscale=no,osc-visibility=always -geometry 700x47+560+910 "${url}"

# here mpv's line sets it up to just get the audio if possible, at the same time the window is a rather compact one
# change to just "mpv" or whatever overrides you'd like 


        # if there's a running MPV instance, it will enqueue the url or file, inherinting whatever the extant instance has defined
        printf "%s\n" "loadfile \"${url}\" append-play" >   $socketfile
        notify-send "added ${url} to mpv playlist"



notify-send "no url on clipboard"

# this situation is actually a remnant of an older version and may not even be useful at this point

exit 0
It just ocurred to me that, rather than having two different scripts, one solution could be to have one copy that's just a symlink to "audio" in its name, with this code here:

   if ! pidof mpv ; then
        if [[ "$0" == *"audio"* ]] ; then
        mpv --ytdl-format worstaudio/249/250/251/bestaudio/17/36/92/5/18 --force-window --no-keepaspect-window --script-opts=osc-vidscale=no,osc-visibility=always -geometry 700x47+560+910 "${url}"
        mpv "${url}"
But that's kind of ugly and some sort of flag or whatever is generally preferred. I think it can be adapted from that by changing "$0" there to "$*" or "$@" and "audio" to something more unambiguous and flag-like, such as "--audionlyplease".

One can also perhaps add a "music" version with elif or case, with higher sound quality, at the same time having the "worst audio" version/flag starting with some arbitrarily accelerated speed.
Posted in Uncategorized
Views 1780 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 10:24 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration