You are not logged in.

#1 2022-09-22 11:31:46

From: Denmark
Registered: 2012-01-18
Posts: 539

Open/Play youtube link from clipboard (script review)

NB. If you don't care to read a lot of unnecessary information, please skip the Preface


Being the happy owner of one of them super ultra wide monitors, I've had an issues, with not being able to to limit full screen applications to a specific portion of the screen. thus rendering the unused space, null and void.
I've tried virtual desktops, fakexrandr, fooling applications into acting full screen, but with different dimensions, and so on and so forth. Nothing has worked, though some approaches might have been possible to make work.
Then I thought long and hard, that on the whole, the only issue I have, in this regard, right now, on a day to day basis, is youtube, which should be possible to stream to a media player.
Again I tried a number of solution, vlc, youtube-dl, but the best I could do, was way too choppy video, or way too long waits for sufficient buffering. I had actually given up, but hen I did a last google search, and discovered yt-dlp, a youtube-dl fork, and mpv, and absolutely brilliant little media player, and the instructions where simple. Just add


to .config/mpv/mpv.conf, and you're golden.
And so I was.
Then I thought, it really is a bit impractical, having to copy paste to the terminal, so I discovered xclip, and started writing a script
Then I thought I might as well be able to queue videos, so I figured that out.
Then I thought, that it would really be nice, if the player would remember the position and size of the window, so I figure that out, which was surprisingly difficult, and of course also between sessions.

In Short

I've wasted quite a lot of time on this, so I thought I might as well share it, and ask for suggestions for improvements, not least in regards to the script. I will never learn bash, or shell scripting, or whatever the correct term is, seriously, but I try my best, and would appreciate suggestions, comments, etc.
As it is, it seems to work flawlessly, but I'm quite sure there are numerous mistakes and oversights, not to mention room for general improvement.
Lastly, if anyone has any idea how one might get the browser, currently chromium, to automatically copy certain links to clipboard, rather than opening then, I am very interested indeed.
mpv steal focus. Been trying to fix it. Seems simple enough, but for now, every solution result in undefined behavior.

# mpv
# yt-dlp - may not be needed after all?
# xclip - is there anoither way?
# xwininfo - **
# xdotool - **
# **a single tool should suffice, if not none, though I do not know how.
unset WID tmp
# Last size and position, from previous session.
if test -f "./yt.conf"; then
  WG=$(head -n 1 ./yt.conf)
  # Default value, for mpv
# The condition in the while loop, is potentially without effect and necessity.
while [ ! -z "$DISPLAY" ]; do
  # Look for changes in clipboard
  if [[ $tmp != $(xclip -o) ]]; then
    tmp=$(xclip -o)
	# Is it a youtube link? If so, add to playlist
    if [[ "$tmp" == ""* || \
          "$tmp" == ""* ]]; then
  # Is mpv running? If so, get current size ad position
  if [[ $(ps aux | grep 'mpv' | wc -l) > 1 ]]; then
    # NB: I don't really understand this. Copy/Paste, with small changes made by
    # trial and error.
    eval $(xwininfo -id $WID |
      sed -n -e "s/^ \+Absolute upper-left X: \+\([0-9]\+\).*/pos=\1'+'/p" \
             -e "s/^ \+Absolute upper-left Y: \+\([0-9]\+\).*/pos+=\1/p" \
             -e "s/^ \+Width: \+\([0-9]\+\).*/WG=\1'x'/p" \
             -e "s/^ \+Height: \+\([0-9]\+\).*/WG+=\1'+'\$pos/p" )
    # sleep because lazy, but also to avoid some obscure xwininfo error?
    sleep 1;
    # Save WG to file
    grep -qxF "$WG" yt.conf || echo "$WG" > ./yt.conf
  # If mpv is not running, and the playlist is not empty...
  elif (( ${#playlist[@]} )); then
    # execute mpv
    mpv --geometry=$WG --no-keepaspect-window --ontop ${playlist[0]} &
    # below seem to make no difference, afterall. Very confusing.
    # --force-window=yes
    # --script-opts=ytdl_hook-ytdl_path=/usr/bin/yt-dlp
    # Temporarily set WID to PID
    # Wait for window to be created
    while [[ -z "$(xdotool search --pid $WID)" ]]
      sleep 1
    #set WID and remove current item
    WID=$(xdotool search --pid $WID)
  # if nothing else is going on, sleep for a bit, to avoid unnecessary work.
    sleep 1

Best regards and enjoy.

Last edited by zacariaz (2022-09-22 14:05:52)

I am a philosopher, of sorts, not a troll or an imbecile.
My apologies that this is not always obvious, despite my best efforts.


Board footer

Powered by FluxBB