View YouTube without ads

Moderator: Forum moderators

User avatar
MochiMoppel
Posts: 1249
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 448 times

View YouTube without ads

Post 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 4439 times
Last edited by MochiMoppel on Fri Oct 07, 2022 2:04 pm, edited 4 times in total.
User avatar
p310don
Posts: 370
Joined: Tue Dec 03, 2019 7:17 am
Location: Brisbane, Australia
Has thanked: 73 times
Been thanked: 107 times

Re: View Youtube without ads

Post 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:

User avatar
JASpup
Posts: 1653
Joined: Sun Oct 04, 2020 10:52 am
Location: U.S.A.
Has thanked: 70 times
Been thanked: 89 times

Re: View Youtube without ads

Post by JASpup »

Hai

MoshiMoshi

1st Attempt:

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

On the Whiz-Neophyte Bridge
Linux Über Alles
Disclaimer: You may not be reading my words as posted.

User avatar
JASpup
Posts: 1653
Joined: Sun Oct 04, 2020 10:52 am
Location: U.S.A.
Has thanked: 70 times
Been thanked: 89 times

Re: View Youtube without ads

Post by JASpup »

It might be this browser (Firefox Light):

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

On the Whiz-Neophyte Bridge
Linux Über Alles
Disclaimer: You may not be reading my words as posted.

User avatar
JASpup
Posts: 1653
Joined: Sun Oct 04, 2020 10:52 am
Location: U.S.A.
Has thanked: 70 times
Been thanked: 89 times

Re: View Youtube without ads

Post by JASpup »

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

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

On the Whiz-Neophyte Bridge
Linux Über Alles
Disclaimer: You may not be reading my words as posted.

User avatar
MochiMoppel
Posts: 1249
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 448 times

Re: View Youtube without ads

Post 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.

User avatar
JASpup
Posts: 1653
Joined: Sun Oct 04, 2020 10:52 am
Location: U.S.A.
Has thanked: 70 times
Been thanked: 89 times

Re: View Youtube without ads

Post 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.

On the Whiz-Neophyte Bridge
Linux Über Alles
Disclaimer: You may not be reading my words as posted.

User avatar
MochiMoppel
Posts: 1249
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 448 times

Re: View Youtube without ads

Post 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 ;)

User avatar
greengeek
Posts: 1411
Joined: Thu Jul 16, 2020 11:06 pm
Has thanked: 548 times
Been thanked: 196 times

Re: View Youtube without ads

Post 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!

User avatar
JASpup
Posts: 1653
Joined: Sun Oct 04, 2020 10:52 am
Location: U.S.A.
Has thanked: 70 times
Been thanked: 89 times

Re: View Youtube without ads

Post 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?

On the Whiz-Neophyte Bridge
Linux Über Alles
Disclaimer: You may not be reading my words as posted.

User avatar
mikewalsh
Moderator
Posts: 6226
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 813 times
Been thanked: 2006 times

Re: View Youtube without ads

Post 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. ;)

User avatar
MochiMoppel
Posts: 1249
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 448 times

View Youtube without ads

Post 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?

User avatar
JASpup
Posts: 1653
Joined: Sun Oct 04, 2020 10:52 am
Location: U.S.A.
Has thanked: 70 times
Been thanked: 89 times

Re: View Youtube without ads

Post 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.

On the Whiz-Neophyte Bridge
Linux Über Alles
Disclaimer: You may not be reading my words as posted.

User avatar
greengeek
Posts: 1411
Joined: Thu Jul 16, 2020 11:06 pm
Has thanked: 548 times
Been thanked: 196 times

Re: View Youtube without ads

Post 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.

User avatar
MochiMoppel
Posts: 1249
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 448 times

View Youtube without ads

Post 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 ;)

User avatar
fredx181
Posts: 3149
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 383 times
Been thanked: 1341 times
Contact:

Re: View Youtube without ads

Post 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 3409 times

Fred

Attachments
yt-dnd.gz
Remove fake .gz and make executable
(8.09 KiB) Downloaded 149 times
User avatar
MochiMoppel
Posts: 1249
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 448 times

Youtube without ads

Post 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.

User avatar
josejp2424
Posts: 246
Joined: Sun Jul 12, 2020 11:40 pm
Has thanked: 263 times
Been thanked: 155 times

Re: View Youtube without ads

Post by josejp2424 »

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

User avatar
amethyst
Posts: 2425
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 57 times
Been thanked: 506 times

Re: View Youtube without ads

Post 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.

User avatar
foxpup
Posts: 186
Joined: Fri Jul 24, 2020 1:23 pm
Location: Europe near the Northsea
Has thanked: 75 times
Been thanked: 32 times

Re: View Youtube without ads in defaultmediaplayer

Post 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:

williams2
Posts: 1065
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 306 times

Re: View Youtube without ads

Post 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.

User avatar
fredx181
Posts: 3149
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 383 times
Been thanked: 1341 times
Contact:

Re: View Youtube without ads

Post 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.

User avatar
foxpup
Posts: 186
Joined: Fri Jul 24, 2020 1:23 pm
Location: Europe near the Northsea
Has thanked: 75 times
Been thanked: 32 times

Re: View Youtube without ads

Post 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

williams2
Posts: 1065
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 306 times

Re: View Youtube without ads

Post 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.

Zuzia
Posts: 106
Joined: Wed Oct 21, 2020 2:32 pm
Has thanked: 13 times
Been thanked: 6 times

Re: View Youtube without ads

Post by Zuzia »

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

User avatar
Grey
Posts: 2032
Joined: Wed Jul 22, 2020 12:33 am
Location: Russia
Has thanked: 76 times
Been thanked: 377 times

Re: View Youtube without ads

Post 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 :)

Fossapup OS, Ryzen 5 3600 CPU, 64 GB RAM, GeForce GTX 1050 Ti 4 GB, Sound Blaster Audigy Rx with amplifier + Yamaha speakers for loud sound, USB Sound Blaster X-Fi Surround 5.1 Pro V3 + headphones for quiet sound.

User avatar
foxpup
Posts: 186
Joined: Fri Jul 24, 2020 1:23 pm
Location: Europe near the Northsea
Has thanked: 75 times
Been thanked: 32 times

Re: View Youtube without ads

Post by foxpup »

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

User avatar
amethyst
Posts: 2425
Joined: Tue Dec 22, 2020 6:35 am
Has thanked: 57 times
Been thanked: 506 times

Re: View Youtube without ads

Post 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.

User avatar
Grey
Posts: 2032
Joined: Wed Jul 22, 2020 12:33 am
Location: Russia
Has thanked: 76 times
Been thanked: 377 times

Re: View Youtube without ads

Post 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 3262 times

Fossapup OS, Ryzen 5 3600 CPU, 64 GB RAM, GeForce GTX 1050 Ti 4 GB, Sound Blaster Audigy Rx with amplifier + Yamaha speakers for loud sound, USB Sound Blaster X-Fi Surround 5.1 Pro V3 + headphones for quiet sound.

User avatar
MochiMoppel
Posts: 1249
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 448 times

Re: View Youtube without ads

Post 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?

Post Reply

Return to “Tips & Tweaks”