How do I block ads on YouTube?

I usually download the YouTube videos offline.
Allavsoft can download YouTube videos and music wihtou ads

I added an regex in my pinole a few days ago as followed
^r.+sn.+(.googlevideo.com)$
It work fine until today my YouTube app on my Samsung smart TV not playing any video anymore. The query list have all the rxxx.googlevideo.com blocked by regex. I remove the entry and YouTube video playing again but when I put it in again the same incident occurs.
For now I do not have YouTube block and the ads are showing up occasionally. Do you know what should I do the preserve the functionality I enjoy a few days ago
Thanks
Edit. The . In regex () group have escape character \ but it not show in the post not sure why

I used a REGEX which blocks all these domains, but unfortunately it also blocks the video playback:
(r)[0-9]---sn-([0-z]){8}.googlevideo.com
This basically means block domains with the following pattern:
r#---sn-XXXXXXXX.googlevideo.com
Where, # is any numer from 0 to 9, and XXXXXXXX is any string of 8 alpha-numerics.

The problem is that I don't know the how to distinguish between the video playback domains and the ad domains.

Any hints on what the pattern is so that I can fix the REGEX?

@Alecz
Hello

I've been studying this topic for some time and came to the conclusion that there are no recognizable features that could be used to uniquely identify Adds.
Youtube does a very good job of misleading DNS-based add-blockers.
I made a regex, which - for me - works quite well.
grafik
But that does not mean that it gives the same result to other users as well. It even makes a difference in which country you are at home or at what time you watch Youtube.
In short. With Pi-Hole Youtube will never be 100% ad-free.
But a combination of add-blocker in the browser and pi-hole as a DNS filter gives very good results.

Speedy 70

Forgive my ignorance, but what is the mechanism behind ublock origin that allows it to recognize ads impregnated into YT video's? Could that mechanism be used as basis for auto-generating blocklists?

1 Like

I don't believe Pi-Hole receives entire address, just the domain, hens it's impossible for it to recognize ads that resides on the same domain as regular videos.

Auf den Bildern erkennt man Nix, bitte korrigieren.

A little off topic but also on topic because I am about to not talk about pi-hole however I think this is a interesting topic. I read this whole thread. After looking at this I think a better way to block ads on YouTube would be to attack the html/css code in the browser. Instead of using a browser plugin though this could be done at the network level using squid proxy and squirm. Of course would that give any advantage over a browser plugin? Maybe not. At least I can't think of any right now. Still interesting subject non the less.

Honestly, I'm okay with ads served up directly from the content provider. There's symmetry in this case; the server and the client both use bandwidth. My motivation for ad blocking is the separate downloads from ad servers. The asymmetry here is the server only incurs the bandwidth of the content, but the client must also incur the bandwidth of the ads.

I'm a total Linux noob and confess to not knowing a lot about all this script stuff so be gently. This idea is a wild one but may spawn some ideas. If Kodi YouTube works add free, then have a similar Kodi YouTube app running on the pi. Then any mobile or PC YouTube request get forwarded to the Pi YouTube (because of a DNS forward rule for YouTube.com requests back to the PI YouTube) Then when add free video is streaming, forward it back to the original YouTube client? In short, make Pi YouTube look like YouTube.com server. I could be talking total garbage. Yes, pi would take a hammering as all YouTube traffic would be flowing through it. Use another pi like a pi4 as a dedicated youtube server?

How to regex block for these domains on youtube?
r4---sn-5hne6n7s.googlevideo.com
r1---sn-5hne6nsd.googlevideo.com

I tried this regex r\d.*\.googlevideo\.com in the regex tester here : https://regex101.com/ and it seems that it's working perfect based on the list provided here : https://raw.githubusercontent.com/anudeepND/youtubeadsblacklist/master/domainlist.txt
I'm going to try add the regex in my pi-hole to see how it goes.
Does anyone else have better ideas , suggestions ?

Regards,
Dimostin

how did this test fair on pi-hole live?

Hi all,

I have tested using the regex above and on the regex test website was working 100% perfect, but in real live test it's a total fail.
Does anyone here have any idea what we are doing wrong ?
I'm looking forward to hear from anyone.

Regards,
Dimostin

I have also found from another forum a regex which I haven't tested yet but the author said it's working but unfortunately it's blocking the video together with the video add.
So youtube it's not playing any video if you use the regex bellow, no more ads but also no more yt video:

regex : ^r.+sn.+(.googlevideo.com)$
source : Trying to regex YouTube ads with pihole - Stack Overflow

I'm still waiting for other people's opinion

Regards,
Dimostin

I managed to come up with a regex for the googlevideo ones that will capture across all of the variants in the urls. listed below. It's not the prettiest thing in the world, but it gets the job done without being too greedy and without lookaheads or lookbehinds. Shouldn't be too aggressive as far as evaluating regex. feel free to correct me though. You can test in regex101 if you feel like tooling around with it. let me know if you come up with a better one.

regex:

PCRE (may work. ymmv)

^(r\d{1,2})(---|\.)(sn-)(([[:alnum:]]{1,8})|([[:alnum:]]{1,7}-[[:alnum:]]{1,4}))(\.googlevideo\.com)$
POSIX ERE (confirmed working but will block all googlevideo streaming sources per EDIT 2)

^r([0-9]{1,2})(---|\.)(sn-)(([A-Za-z0-9]{1,8})|([A-Za-z0-9]{1,7}\-[A-Za-z0-9]{1,4}))(\.googlevideo\.com)$

sample entries:

r1---sn-8xgp1vo-p5qe.googlevideo.com
r4---sn-p5qlsnz6.googlevideo.com
r4---sn-vgqs7nez.googlevideo.com
r4.sn-vgqs7nez.googlevideo.com
r17---sn-vgqsenes.googlevideo.com
r2---sn-vgqs7n7k.googlevideo.com
r1---sn-vgqsen7z.googlevideo.com
r1.sn-vgqsen7z.googlevideo.com
r20---sn-vgqs7ne7.googlevideo.com
r20.sn-vgqs7ne7.googlevideo.com

Edit: didn't read the docs and used PCRE regex instead of posix ere. Will update when fixed.

Edit 2: fixed the regex for POSIX ERE, but found out that youtube hosts the ads at the same domains as the videos themselves. basically they route your request to one of the googlevideo servers and the ad is injected directly into video stream as PART of the video. uBlock and pihole ftw on this one. I'm going to try doing some wireshark'ing on my home network to see if I can narrow it down further.

1 Like

That regex didn't block anything for me. Streaming in Japan.

Sorry about the regex issue. So I included part of my second edit below.

It looks like there really is no way to block the ads directly right now unless you want to slowly remove all of the available googlevideo streaming domains from being able to send you videos. I'd recommend using something like uBlock Origin as a second layer of ad removal on your browser. If you're on mobile, check out Blokada.

Hi
I not have any knowledge about regex
How I block this kind of subdomains?

r2---sn-ivuoxu-ua86.googlevideo.com
r3---sn-ivuoxu-ua86.googlevideo.com
r1---sn-ivuoxu-ua8z.googlevideo.com
r4---sn-ivuoxu-ua86.googlevideo.com

Update thiatwhat I test
r[0-9]+---sn-ivuoxu-+ua[0-9]+[\w].googlevideo.com

It's doesn't block with my regex.
Any ideas how do it right?

^r[0-9]---sn-ivuoxu-ua[a-zA-Z0-9][a-zA-Z0-9].googlevideo.com will block those and and variations but I have been down this road if you block all the iterations weird things start happening to youtube thumbnails stop loading and if you block the wrong ones even the videos stop loading or take a long time to load as google tries to find an add but there is a happy balance just be carefull adding too wide of a regex.

ps thats three - - - for some reason webpage shows it as one long dash