How do I block ads on YouTube?

^r[0-9]---sn-[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9].googlevideo.com this will stop every one of the r?---sn????????.googlevideo.com but it also stop videos from loading also so i dont think this is the correct path to be going down i've heard people say the last part of the url is a fingerprint and certain ones are add fingerprints but I have been tracking it for awhile and there dosnt seem to be any consistent pattern

Has anyone tried seeing what YouTube’s “paid service” changes in regards to ads? I’ll be working on this too soon with my Pi 4 in the next month.

1 Like

Using data from the Chrome plugin Adblock for Youtube, the code shows these primary regexes which is effective (thus far) for blocking Youtube ads in streaming video:

const YOUTUBE_REGEX = /^https?:\/\/(\w*.)?youtube.com/i

const YOUTUBE_AD_REGEX = /(doubleclick\.net)|(adservice\.google\.)|(youtube\.com\/api\/stats\/ads)|(&ad_type=)|(&adurl=)|(-pagead-id.)|(doubleclick\.com)|(\/ad_status.)|(\/api\/ads\/)|(\/googleads)|(\/pagead\/gen_)|(\/pagead\/lvz?)|(\/pubads.)|(\/pubads_)|(\/securepubads)|(=adunit&)|(googlesyndication\.com)|(innovid\.com)|(tubemogul\.com)|(youtube\.com\/pagead\/)|(google\.com\/pagead\/)|(flashtalking\.com)|(googleadservices\.com)|(s0\.2mdn\.net\/ads)|(www\.youtube\.com\/ptracking)|(www\.youtube\.com\/pagead)|(www\.youtube\.com\/get_midroll_)|(www\.youtube\.com\/api\/stats)/

const YOUTUBE_ANNOTATIONS_REGEX = /^https?:\/\/(\w*.)?youtube\.com\/annotations_invideo\?/

I don't know how to assemble this for PiHole, but I'm going to guess this might work, if it could be formatted properly. Anyone?

_F

1 Like

I added all of the domains and no luck. You can take what's there already and remove the ().
Adding those domains only resulted in more blocks from doubleclick.com/net. Some don't work with pi-hole since they're not domains and are meant for adblockers like (&ad_type=).
Unfortunately, it seems we'll still need an adblocker on our computers for youtube.

It's entirely possible I missed something (logic etc) from the JS code in the Adblock for Youtube addon (Chrome). The code is there, I probably don't know how to follow it all. But, I can tell you that as far as the in-browser experience is concerned, it works. I just imagine there's a way to take that logic and make it work for Pi-Hole, too.

There is not. Pi-Hole sees the domain request only, and none of the traffic that follows. The browser blocker sees the entire URL and all the html and Javascript that loads, and can act based on that.

Sounds like this may be a job for a proxy, such as Squid or similar

Good evening, please tell why do not add domains s.youtube.com and www.googleadservices.com and pagead.l.doubleclick.net for full block ads in application youtube for android and for web version? Thx.

That worked years ago on mobile

On web version youtube working why do not add domains s.youtube.com www.googleadservices.com www.googleadservices.com pagead.l.doubleclick.net pagead.l.doubleclick.net?

And by the way, in the mobile version, happiness also works.

Not for me on desktop. Are you sure your adblocker is disabled?

On Android there is one thing that works: https://vanced.app/

Frequent advertising in the mobile application is gone.)

Hi All

I have not checked this thread for over two years.

Has anyone found a solution to block YouTube ads on TV Apps for Apple TV, Fire TV etc?

Are the following block list URLs still any good?

https://raw.githubusercontent.com/HenningVanRaumle/pihole-ytadblock/master/ytadblock.txt 1.3k
https://raw.githubusercontent.com/anudeepND/youtubeadsblacklist/master/domainlist.txt

Thanks

Hi,

We didn't find reliable method based on DNS request (Pi-hole). For the moment, the only way is to have a web browser that can detect ads from html/url/javascrit.
So you will be able to remove ads if you can install this type of application on the Fire TV, otherwise no.

If you have web browser on the FireTV (I haven't it, I don't know), maybe this post can help you.

1 Like

After I configured Pi-Hole, for a week, I saw ads that are un-skippable 15-seconds only. I did not see 5-seconds skip ads for a whole week. And the ads were more frequent. Its like, Google knows you are trying to block and it was like a punishment.

Now, the ads are high in number, but still skippable sometimes.

On a laptop browser, I don't care, as AbBlock is handling well enough. Only issue is with mobile devices like phones and iPads which we are a bit more frequent.

BTW, I noticed, I see less ads on my Samsung 43 and 65 Smart TV YouTube app.
I see ads on Apple TV -YouTube app on my Samsung 65".

1 Like

Please check similar to how vanced is managing as well.

I have to see if vanced is compatible with OnePlus Two. Thats the only android I have.

this two script are working for me

1º script /etc/pihole/temp.sh

#!/bin/sh
# This script will fetch the Googlevideo ad domains and append them to the Pi-hole block list.
# Run this script daily with a cron job (don't forget to chmod +x)
# More info here: https://discourse.pi-hole.net/t/how-do-i-block-ads-on-youtube/253/136

# File to store the YT ad domains
FILE=/etc/temp/temp.txt

# Wolfram Alfa AppID
APPID="Register at https://www.wolframalpha.com/ put your id here"

# Fetch the list of domains, remove the ip's and save them
curl 'https://api.hackertarget.com/hostsearch/?q=googlevideo.com' \
| awk -F, 'NR>1{print $1}' \
| grep -vE "redirector|manifest" > $FILE

# Replace r*.sn*.googlevideo.com URLs to r*---sn-*.googlevideo.com
# and add those to the list too
curl "http://api.wolframalpha.com/v2/query?input=googlevideo.com&appid=${APPID}&format=plaintext&podstate=WebSiteStatisticsPod:InternetData__Subdomains&podstate=WebSiteStatisticsPod:InternetData__Subdomains_More" \
| grep -Po "r\d+---sn-.+.googlevideo.com" >> $FILE

# Scan log file for previously accessed domains
grep r*.googlevideo.com /var/log/pihole.log \
| awk '{print $6}' \
| grep -v '^googlevideo.com\|redirector\|manifest' \
| sort | uniq >> $FILE

# Replace r*.sn*.googlevideo.com URLs to r*---sn-*.googlevideo.com
# and add those to the list too
cat $FILE | sed -i $FILE -re 's/(^r[[:digit:]]+)(\.)(sn)/\1---\3-/' >> $FILE

#delete duplicates
perl -i -ne 'print if ! $x{$_}++' /etc/temp/temp.txt

#demonstrative
cat $FILE >>  /var/www/html/temp.txt

2º script /etc/pihole/youtube-ads.sh

echo off

#copy file to blacklist
cat /etc/temp/temp.txt >> /var/www/html/youtube.txt

#delete duplicates
perl -i -ne 'print if ! $x{$_}++' /var/www/html/youtube.txt

#removes the temporary file on the raspberry pi
rm /var/www/html/temp.txt

#updates pihole blacklist/whitelist
pihole -g

crontab
/35 * * * * /etc/pihole/temp.sh
0
/3 * * * * /etc/pihole/youtube-ads.sh

add http://localhost/youtube.txt blacklist from local

block list:

r5---sn--n4v7sn76.googlevideo.com
r6---sn--n4v7sn76.googlevideo.com
r3---sn--30a7yn76.googlevideo.com
r1---sn--ab5szn76.googlevideo.com

sorry for my English

4 Likes

I noticed wolframalpha was not returning any *.googlevideo.com addresses and checked out whats going on. Turns out this api call:

http://api.wolframalpha.com/v2/query?input=googlevideo.com&appid=${APPID}&format=plaintext&podstate=WebSiteStatisticsPod:InternetData__Subdomains&podstate=WebSiteStatisticsPod:InternetData__Subdomains_More

returns

timedoutpods="Web statistics for all of googlevideo.com,HTML element hierarchy for googlevideo.com"

To solve this increased podtimeout in call

POD_TIMEOUT="20"
http://api.wolframalpha.com/v2/query?input=googlevideo.com&appid=${APPID}&format=plaintext&podtimeout=${POD_TIMEOUT}&podstate=WebSiteStatisticsPod:InternetData__Subdomains&podstate=WebSiteStatisticsPod:InternetData__Subdomains_More

Wolframalpha API documentation is here if someone is having other timeout issues Wolfram|Alpha Full Results API Reference