How do I block ads on YouTube?

Thank you. The script works, but Pihole blocks too much and some videos cannot be played

I'm not 100% sure, but I believe this was one of the videos, that didn't play for me, with the Adlist active.

I had the same issue, the code block PandaWithPants posted seems to result in some issues, when directly copy/pasted. The error you noted is due to a linebreak in the line "curl "http://api.wolframalpha.com/", where the URL is split with a new line.

1 Like

Thanks for noting about the line break. Yeah copying/pasting sometimes provokes those line breaks issues, I've directly copied the code from my Cygwin terminal window.

As for the video, I was able to play it both in the app/browser. I'd suggest though again not a 100% automatic solution, that you go to your web admin interface and look at the most recent logs in the Query log. Then you'd see an r#---sn-... entry in color red in there.

You should whitelist the topmost entry of those and every time another video wouldn't load.

As far as the method is too aggresive I would like to try other methods as well but as this works for casual use for mostly everyone is what it is the best right now.

Let me know if you need any other help!
Thanks

1 Like

Thanks for the response and your insight!

Oddly enough the videos only fail to play sometimes, sometimes it works just fine. But as I said before, ads keep coming up for me with the filter active. As stated before I'm very new to Pi-Hole and never occupied myself with how Youtube delivers content/ads.

I've resorted to an alternative solution for me uBlock on the desktop works fine and I use a modded Youtube app on Android which seems to work perfectly fine without ads (I'm not sure if external links are welcome here, but I can PM anyone the link).

Yup, right now the best resort (for browsers mostly) is to use an adblocker add-on, as there is no 100% reliable solution on Pi-hole at the moment. The script blocks YT servers that serve both ads and video, so that's why sometimes a video breaks because the script blocked a "video-only server". And the other videos that are plagued with ads & video are not playable since some YT servers serve both ads & video.

For devices that are not ad-blocking capable like: ChromeCast, Smart TVs, Apple TV, Amazon Fire this works 70-85% that's why many have resorted to Pi-hole for ad-blocking to these devices.

But since now YT is aware about people trying to block these r#--- servers, they actually punish you sending more ads when you're trying to block the connections from any r#---sn-gfjdgdkfgkd... server, so this script method could be useless to many.

There is another Reddit thread that is mentioned at the very beginning of this topic, where some said to have luck by doing this:

  1. nslookup manifest.googlevideo.com

  2. Check what IP throws in the result: Eg: 216.58.194.206

  3. nslookup 216.58.194.206

  4. If you see one server in your result it could mean that is the server geographically closest to you that serves ads (in theory).

So you would then have to create a custom hosts file according to this guide and add a line like this:

216.58.194.206 manifest.googlevideo.com

Again, the IP you see could change, so you would have to do the steps in sequence.

I would like to see if there is another solution in the future, but for the most part it is what it is right now, sadly.

1 Like

Hi guys, total noob here...working my way through this. One small question on editing crontab: do I edit it for the user "pi" or as "root" for it to work?

Depends on which user you used for installation. To determine that do a cd /etc/pihole and then ls -l

You will see which user created the files in that directory. If it's pi then just do a crontab with that user logged on, otherwise switch to root with su command and then run crontab -e

1 Like

Thanks for the tip. I wonder if Google recognizes if Pi Hole blocks an ad url and replaces with a brand new one resulting in an neverending game of cat and mouse.

Also I used to run YouTube on Brave Browser and if I have block ads enabled all ads would be blocked. I'm not a web whiz so not sure if this would work: could we can find out how Brave blocks those ads and incorporate that technique into Pi Hole?

I read today that appending a dot to YouTube.com
removes a lot of ads too.

e. g. https://youtube.com./{RESTOFURL}

how would I implement a url rewrite in that case?

or am I mistaken and it's already implemented?

thanks

Blocking ads by adding dot at the end of domain name

Not sure how this could fit, help or else, nor whether it be warranted. But I have few rules that is used to successfully blocked youtube ads with a 100% clean view. The problem, I have combined these regex from another interface... but throwing it in here, maybe could be adapted for pi-hole somehow, or based idea for pi-hole to successfully get rid of these ads...

The 'Hide Elements' are not applicable to Pi-hole.

right

So whats this latest method for blocking YouTube Ads on Amazon Fire TVs?

1 Like

Where are these rules coming from is it from an Android app? You could actually add these as regex entries as is in Pihole -> Blacklist -> Regex filter

Also do they work only on web browsers? Or do they work for Youtube app as well?

Just checking over the rules listed in those screenshots, they are URLs... Pi-hole can only block via DNS name. For example, it knows nothing about the http(s) part, or anything after the first / such as /get_midroll_

Adding them to Pi-hole will have no effect.

1 Like

Hi, so I'm new to PiHole (I set it up yesterday) and I scrolled through the entire forum, tested a lot of things, still some ads and videos stopped loading. Does anybody have any new developments?

EDIT: YouTube legit stopped working on my iPhone I had to go back to automatic DNS

The TL;DR of this entire thread (and answer to the question posed in the topic title) should probably be:

"Not with Pi-hole, sorry!"

Ultimately, youtube video ads are served from the same content domains as the videos themselves, so the only sure fire way to block ads on youtube is to block youtube all together :wink:

Personally (and I mean personally - this is often a controversial option for whatever reason) I pay for Youtube Premium family, primarily for Google Play Music/Youtube music for myself and 5 other family members - as an added bonus.. no ads on Youtube. However I do understand that paying for a service is not an option for some people.

3 Likes

Okay fair enough. I’m using a adblocker on my browser on my laptop. On my phone, I don’t get a lot of unskippable ads (but they still exist). So, this was kinda a waste to read through.

EDIT: I could also jailbreak my phone because there is a tweak to remove ads from YouTube. Paired with PiHole, I could have no ads on my phone at all.