Page 1 of 3

View YouTube without ads

Posted: Wed Dec 02, 2020 3:03 am
by MochiMoppel

Watching videos on YouTube can be really annoying. On a slow netbook the page containing the video takes forever to load, and then there are these pesky and long intro ads. For a demo let's take this video as example.

Wouldn't it be much nicer to view the video without delay and without ads?. Can be done. Try the same video with this link.
Much better, isn't it? And notice how the video always fills the whole browser page. Scales nicely when the window is resized and reaches (almost) the same size as in fullscreen mode when the browser window is maximized.

The 2nd link type uses YouTube's special URL for embedded videos. Using a little script I can redirect almost any "normal" YT link to the fast and ad-free second type:

Code: Select all

#!/bin/sh
BROWSER=defaultbrowser
YTURL=$(xclip -o -sel clip) || YTURL=$(xsel -b)
(($?)) && exec Xdialog -msg "xclip or xsel not installed" x
case $YTURL in
*youtube.com*v%3D*) VIDID=${YTURL#*v%3D};;
  *youtube.com*v=*) VIDID=${YTURL#*v=};;
  *youtu.be*      ) VIDID=${YTURL#*.be/};;
  *               ) exec Xdialog -msg "Not a recognized YouTube URL:\n\n${YTURL::50}" x;;
esac
VIDID=${VIDID::11}
exec $BROWSER "https://www.youtube.com/embed/$VIDID?autoplay=1"

- Copy the code into a file and make the file executable.
- Drag the file onto the desktop to create a desktop icon

This is sufficient to start the script with the mouse, but it's more convenient to use a keyboard shortcut when the desktop is covered, so
, right-click the created icon and select "Edit Item"
- Push the "(click to set)"button and enter a keyboard shortcut for the script. I use Ctrl+Shift+A

Now let's use the script:
Go to a search engine, e.g. Google or DuckDuckGo or even YouTube's search, and search for something. Clicking on the top "Videos" category normally provides many YT links.

Do not left click on such a link! Instead right click to trigger the context menu.
When using Palemoon the menu looks like in the screenshot. We need to copy the link address to the clipboard, which (in Palemoon!) can be done by selection "Copy Link Location" or by pressing the A key (note the underlined a in the menu).
Then enter Ctrl+Shift+A. This will start the script. The script reads YT URL from the clipboard, then converts the URL into the "embedded" type and finally passes it to the browser. If the browser is Palemoon a new tab will open and the video will start.

The videos end with a couple of preview images for related videos. YouTube tries hard to lure users from an embedded video to the main site. OK, fair enough. But of course those follow up videos can also be viewed stress free using above script. All that has to be done is saving the URL into the clipboard.

Limitations:
Though most uploaders allow their YT videos to be embedded, some don't (e.g. ABC Australia, Fox and some music channels ). In this case you will see the message "Video unavailable" and have to use the "full featured" website.

That's all. Have fun!

[EDIT] Enhanced version
Apart from YouTube videos this enhanced script additionally supports Dailymotion videos (see here for details):

Code: Select all

#!/bin/sh
BROWSER=defaultbrowser
CLIP=$(xclip -o -sel clip) || CLIP=$(xsel -b)
(($?)) && exec Xdialog -msg "xclip or xsel not installed" x

case $CLIP in
    *youtube.com*v%3D*)             SITE=YT; VIDID=${CLIP#*v%3D}    ;;
    *youtube.com*v=*)               SITE=YT; VIDID=${CLIP#*v=}      ;;
    *youtu.be*)                     SITE=YT; VIDID=${CLIP#*.be/}    ;;
    *youtube.com/embed/*)           SITE=YT; VIDID=${CLIP#*embed/}  ;;
    *dailymotion.com/video*)        SITE=DM; VIDID=${CLIP#*video/}  ;;
    *dailymotion.com%2Fvideo%2F*)   SITE=DM; VIDID=${CLIP#*video%2F};;
esac

case $SITE in
    YT) VIDID=${VIDID::11}
        CMD="https://www.youtube.com/embed/$VIDID?autoplay=1" ;;
    DM) VIDID=${VIDID::7}; VIDID=${VIDID%&}         #sometimes only 6 digit ID, with trailing &
        CMD="https://www.dailymotion.com/embed/video/$VIDID?autoplay=1" ;;
    *)  gxmessage -name "$0" -c -fn 'bold 11' -bg '#f00' -fg '#fff' -de okay $'\n Clipboard content is not a valid video URL \n'
        exit ;;
esac
exec "$BROWSER" "$CMD"
mm_youtube.png
mm_youtube.png (373.45 KiB) Viewed 3334 times

Re: View Youtube without ads

Posted: Wed Dec 02, 2020 9:14 am
by p310don

This is pretty great, except for one thing. If you don't have a youtube url in the clipboard, it opens a whole bunch of stuff in tabs from the directory it is in. I had it in the root directory, and it opened about 100 tabs :shock:


Re: View Youtube without ads

Posted: Wed Dec 02, 2020 9:38 am
by JASpup

Hai

MoshiMoshi

1st Attempt:

YT.png
YT.png (29.3 KiB) Viewed 3287 times

Re: View Youtube without ads

Posted: Wed Dec 02, 2020 9:47 am
by JASpup

It might be this browser (Firefox Light):

BB.png
BB.png (226.12 KiB) Viewed 3287 times

Re: View Youtube without ads

Posted: Wed Dec 02, 2020 10:37 am
by JASpup

Test #3 in Chrome. Background is video playing with ads:

3.png
3.png (98.64 KiB) Viewed 3275 times

Re: View Youtube without ads

Posted: Wed Dec 02, 2020 11:39 am
by MochiMoppel
p310don wrote: Wed Dec 02, 2020 9:14 am

This is pretty great, except for one thing. If you don't have a youtube url in the clipboard, it opens a whole bunch of stuff in tabs from the directory it is in

Thanks for the feedback. Yes, I can reproduce it. Can happen if you don't run the script after copying the YT URL
If the stuff that the script fetches from the clipboard does not contain a YT URL, you should see the "Not a recognized YouTube URL" message, and that's the end. However if your clipboard does contain a YT URL as part of a larger text portion, then this test may fail and it can happen what you described. I changed the code. Should be OK now. Please let me know if this fixes your issue.

@JASpup Thanks for testing, however please stop posting screenshots from videos that failed. Post the URL instead if you like. I mentioned already that some URLs may fail. The one you tried is no exception. Do as they say: "Watch on YouTube".
The funny part here is that YT's right click menu contains a "Copy embed code". This code fails too. There is nothing I can do about it.

If your default browser is not capable of rendering HTML5 videos you should change the code. The browser after BROWSER= must be HTML5 capable. Normally something like BROWSER=palemoon fixes the issue.


Re: View Youtube without ads

Posted: Wed Dec 02, 2020 11:54 am
by JASpup
MochiMoppel wrote: Wed Dec 02, 2020 11:39 am

@JASpup Post the URL instead if you like. I mentioned already that some URLs may fail. The one you tried is no exception. Do as they say: "Watch on YouTube".

I could have done that, but browser might have something to do with it not working?

I read your caveat. Some Youtube videos don't play ads, so I chose the screenshots out of familiarity. The last one is both the video playing and the script not working -- same video. I'm not really trying to watch these videos right now, but since the rave is on, I'm letting it play.

I'm in a puplet with Light instead of Pale Moon. It's not easy to switch default browsers. I chose a portable Firefox and it defaulted to Chrome instead. I went to Firefox because I couldn't choose Chrome.

Your script is a neat trick if it works.


Re: View Youtube without ads

Posted: Thu Dec 03, 2020 7:13 am
by MochiMoppel
JASpup wrote: Wed Dec 02, 2020 11:54 am

The last one is both the video playing and the script not working -- same video.

The screenshot shows that the script is working ;)


Re: View Youtube without ads

Posted: Thu Dec 03, 2020 8:09 am
by greengeek

I'm testing on Tahr32 at the moment and get a message saying "xclip or xsel not installed".

Which of these would you recommend best?

cheers!

EDIT : Ahhh, ok i see it needs both.

Brilliant utility !!

Works great on both Palemoon and Vivaldi on Tahr32. Excellent!


Re: View Youtube without ads

Posted: Thu Dec 03, 2020 9:44 am
by JASpup
MochiMoppel wrote: Thu Dec 03, 2020 7:13 am
JASpup wrote: Wed Dec 02, 2020 11:54 am

The last one is both the video playing and the script not working -- same video.

The screenshot shows that the script is working ;)

How? It correctly opens a browser window in the default browser that won't play.

The background playing is just like you wrote:

Do as they say: "Watch on YouTube".

I manually enter the URL in a different window. I don't believe that is the script. You think so?


Re: View Youtube without ads

Posted: Thu Dec 03, 2020 10:45 am
by mikewalsh
JASpup wrote: Wed Dec 02, 2020 11:54 am

I'm in a puplet with Light instead of Pale Moon. It's not easy to switch default browsers. I chose a portable Firefox and it defaulted to Chrome instead. I went to Firefox because I couldn't choose Chrome.

@JASpup :-

One thing you learn in Puppy, very early on, is NOT to try setting the default browser FROM the browser's own settings page. This never, EVER works in Puppy.....something to do with Puppy's implementation of the xdg protocols, I believe. Instead, you use the 'Default Applications Chooser', under Menu->Setup.

And don't worry if the browser you want doesn't appear in the appropriate drop-down box. Just manually edit the entry, and set the $PATH yourself to the browser's launcher. The other way this can be done - same thing, different method - is to go into /usr/local/bin, open the 'defaultbrowser' entry with Geany, then change the $PATH there, instead.

You almost always need to do this with any of the portables, unless you've linked the launcher into one of your 'bins' and re-named it to one of the 'standard' names.

Mike. ;)


View Youtube without ads

Posted: Thu Dec 03, 2020 11:51 am
by MochiMoppel
greengeek wrote: Thu Dec 03, 2020 8:09 am

I'm testing on Tahr32 at the moment and get a message saying "xclip or xsel not installed".

Which of these would you recommend best?

cheers!

EDIT : Ahhh, ok i see it needs both.

No, it needs only one of them. I checked Tahr6.0.6 and found xclip documentation but no trace of the binary, According to /root/.packages/builtin_files/xclip the binary is not in the list of installed files. Very strange. So I assume that it worked for you after installing xsel, right?


Re: View Youtube without ads

Posted: Thu Dec 03, 2020 12:32 pm
by JASpup
mikewalsh wrote: Thu Dec 03, 2020 10:45 am

set the $PATH yourself to the browser's launcher. The other way this can be done - same thing, different method - is to go into /usr/local/bin, open the 'defaultbrowser' entry with Geany, then change the $PATH there, instead.

Not sure the specifics there, but it looks like it could be useful, thanks.

The default chooser was finicky but seemed redeemable (hopeful).

The late 32bit Chrome (in my last screenshot), won't even let you make it default in its settings. There might be an option first run but I never choose it.


Re: View Youtube without ads

Posted: Thu Dec 03, 2020 5:43 pm
by greengeek
MochiMoppel wrote: Thu Dec 03, 2020 11:51 am

No, it needs only one of them. I checked Tahr6.0.6 and found xclip documentation but no trace of the binary, According to /root/.packages/builtin_files/xclip the binary is not in the list of installed files. Very strange. So I assume that it worked for you after installing xsel, right?

Yes, xsel from PPM did the trick perfectly thank you.

Strange thing though - when I first saw the dependencies error message I tried to run xclip and xsel from cli to confirm functionality but found neither available in my system yet when I tried to install xclip the ppm indicated xclip is already installed.
Some minor weirdness within my Tahr.

Very handy way to play videos thanks.


View Youtube without ads

Posted: Fri Dec 04, 2020 1:45 am
by MochiMoppel
greengeek wrote: Thu Dec 03, 2020 5:43 pm

Strange thing though - when I first saw the dependencies error message I tried to run xclip and xsel from cli to confirm functionality but found neither available in my system yet when I tried to install xclip the ppm indicated xclip is already installed.
Some minor weirdness within my Tahr.

xclip is installed in Tahr6.0.5. It got lost in Tahr6.0.6. Newer is not always better.
Hang on to your trusted Slacko5.6 ;)


Re: View Youtube without ads

Posted: Thu Dec 10, 2020 6:30 pm
by fredx181

Here's a little variation of MochiMoppel's script, attached script "yt-dnd", depends on having yad installed.
I thought it'd be nice to have a little box to drag n' drop any youtube link onto, running the attached script, there will appear an icon in upper left corner, example usage:

Drag n' drop youtube links
Drag n' drop youtube links
yt-dnd.gif (452.07 KiB) Viewed 2269 times

Fred


Youtube without ads

Posted: Fri Dec 18, 2020 5:49 am
by MochiMoppel

@fredx181 Thanks. Works with my old yad version. Could be handy when keyboard is not accessible or when using a tablet (no idea if this would be feasible). Drag n' drop can be a bit tricky when dragging linked preview images as YT preview images are bigger than the drop icon, making it hard to get visual feedback.


Re: View Youtube without ads

Posted: Sat Dec 26, 2020 2:11 pm
by josejp2424

MochiMoppel thank you very much, for this simple script that works very well.


Re: View Youtube without ads

Posted: Sat Dec 26, 2020 11:55 pm
by amethyst

Just a mention. I installed the Adblock Latitude add-on in Palemoon. Blocks youtube ads nicely. Too many videos not playing with the embedded method. Another trick to block ads in youtube is to add a rest sign [.] after com in the video url (so you have .com. in the url). Unfortunately the last mentioned method does not always work. And another suggestion. When searching for youtube videos use microsoft bing/DuckDuckGo/Yahoo search engines instead. Click on the videos tab and search for your video, eg: youtube tina turner concerts. Results will display. Now the nice - clicking a link will play the video in the same window (without the ads even if you don't have an ad blocker), it does not go to the youtube page with all its crap loadings.


Re: View Youtube without ads in defaultmediaplayer

Posted: Sat Feb 20, 2021 11:30 am
by foxpup

I got the idea to put defaultmediaplayer in for the BROWSER.

In Fossapup the defaultmediaplayer is mpv.
mpv needs youtube-dl to play url's and that was not in my Fossapup, so downloaded that and put it in /usr/bin.
And then this nice script works with mpv as well :thumbup:


Re: View Youtube without ads

Posted: Sat Feb 20, 2021 8:00 pm
by williams2

I disable the mpv youtube-dl hook by putting

ytdl=no

in the file /root/.config/mpv/mpv.conf
so that mpv will play a streaming file directly and will not try to download it first using youtube-dl, so youtube-dl does not need to be installed.

Or you can use the mpv command line option --ytdl=no

This might break something that expects the mpv youtube hook to be enabled by default.


Re: View Youtube without ads

Posted: Mon Feb 22, 2021 9:21 am
by fredx181
williams2 wrote: Sat Feb 20, 2021 8:00 pm

I disable the mpv youtube-dl hook by putting

ytdl=no

in the file /root/.config/mpv/mpv.conf
so that mpv will play a streaming file directly and will not try to download it first using youtube-dl, so youtube-dl does not need to be installed.

Or you can use the mpv command line option --ytdl=no

This might break something that expects the mpv youtube hook to be enabled by default.

For me there are many videos that won't play this way.
Running mpv with youtube-dl enabled, I can play almost all.


Re: View Youtube without ads

Posted: Mon Feb 22, 2021 4:53 pm
by foxpup
fredx181 wrote: Mon Feb 22, 2021 9:21 am

For me there are many videos that won't play this way.
Running mpv with youtube-dl enabled, I can play almost all.

I second your findings @fredx181
@williams2 It does not work for me with ytdl=no


Re: View Youtube without ads

Posted: Mon Feb 22, 2021 6:42 pm
by williams2

ytdl=no is just convenient when mpv can play directly, by itself, an online stream, and does not need youtube-dl.
By default, mpv will try to download the URL first using youtube-dl and then it will try to play the URL directly.

For example,
mpv http://simplexstream.com:8058/
mpv --no-ytdl http://simplexstream.com:8058/

If mpv can not play a URL directly, but youtube-dl can, then the youtube-dl hook should work (if it is enabled.)

Personally, I tend to download a media file first, using wget or youtube-dl, then play it with mpv.
And you can usually watch/listen to the the file while it is still downloading.


Re: View Youtube without ads

Posted: Tue Feb 23, 2021 2:49 pm
by Zuzia

Just install firefox + ublock plugin :) No adds at all.


Re: View Youtube without ads

Posted: Fri Mar 19, 2021 5:02 pm
by Grey

When using mpv, you can shorten the script to this:

Code: Select all

#!/bin/sh
PLAYER=defaultmediaplayer
YTURL=$(xclip -o -sel clip) || YTURL=$(xsel -b)
(($?)) && exec Xdialog -msg "xclip or xsel not installed" x
exec $PLAYER "$YTURL"

In this case, links work not only from youtube, but also from other well-known sites, for example xvideos, spankbang, etc. Don't laugh, it was just necessary to check somewhere :)


Re: View Youtube without ads

Posted: Fri Mar 19, 2021 5:33 pm
by foxpup

:thumbup2:
Thanks Grey! Works well on lbry.tv or odysee.com as well.
(Not laughing, just smiling ;-) )


Re: View Youtube without ads

Posted: Fri Mar 19, 2021 6:01 pm
by amethyst

You can easily play all your youtube videos (unless youtube restricts to only playing the video on the youtube site only, which happens sometimes) directly from a search engine site like DuckDuckGo without ads and without going to the youtube site. In cases where youtube forces you to stream from their site only, install an adblock plugin. To actually download a file, a site like 9xbuddy.com works great.


Re: View Youtube without ads

Posted: Fri Mar 19, 2021 7:33 pm
by Grey
amethyst wrote: Fri Mar 19, 2021 6:01 pm

You can easily play all your youtube videos

Yes we can :thumbup: . But specifically mpv is needed for what the arrow points to:

mpv_hwdec.jpg
mpv_hwdec.jpg (157.73 KiB) Viewed 2030 times

Re: View Youtube without ads

Posted: Sat Mar 20, 2021 6:01 am
by MochiMoppel
amethyst wrote:

You can easily play all your youtube videos (unless youtube restricts to only playing the video on the youtube site only, which happens sometimes) directly from a search engine site like DuckDuckGo without ads and without going to the youtube site.

If it were that easy I would do it, but IIRC DuckDuckGo does technically what my script does, only less flexible.
I can't test it again because I don't know how to enforce this mode. I remember that DuckDuckGo proposes to play YT videos in DDG for security/privacy reasons, but once I declined this offer and chose to go directly to YT I was never able to play in DDG again. There must be a setting for this option somewhere. Where is it?