How do I block ads on YouTube?

I know what you mean - until now I was not able to block the ads at the beginning of the video only with pihole :disappointed:

How can I use these commands? Or how can I actually implement them, I have no idea how to use it. And ADS are everywhere in YouTube.

I use that too works great whit youtube ads :smile:

Hi guys! Is there any additional software I can install on raspberry to help pihole to kill youtube ads?
Raspberry pi3 with raspbian

If you're using Android take a look at this thread on XDA, it's a modified version of YouTube app. Works well and has some other cool features too!

1 Like

Well i combined some ideas and small improvements found on this topic into a install script

Requirements: Pi-hole Version v3.2.1 Web Interface Version v3.2.1 FTL Version.

Default scriptcode: ubuntu / debian.
You can convert the script to other platforms by simple replace the scriptcode line:

"apt-get python-pip install"

Replace it with the install command required to install python pip on your distro.

create file as following:

  • sudo -i (and login)

  • nano /etc/pihole/youtube-adblock-install.sh

  • paste the scriptcode below:

  • exit nano with ctrl+x and save the install script

  • chmod +x /etc/pihole/youtube-adblock-install.sh

  • run /etc/pihole/youtube-adblock-install.sh

scriptcode:

echo "created for public use and use."
echo "Start with re-install cleanup."
rm -r /etc/dnsdumpster
rm /var/www/html/youtube-ads-list.txt
rm /etc/pihole/youtube-ads.sh
echo "cleanup done."
echo "installing python-pip and dnsdumpster."
apt-get install python-pip
pip install --upgrade pip
pip uninstall dnsdumpster
mkdir /etc/dnsdumpster
echo "" > /etc/dnsdumpster/youtube-domains.txt
echo "" > /etc/dnsdumpster/youtube-filtered.txt
echo "" > /etc/dnsdumpster/youtube-ads.txt
cd /etc/dnsdumpster
pip install https://github.com/PaulSec/API-dnsdumpster.com/archive/master.zip --target /etc/dnsdumpster/
pip install https://github.com/PaulSec/API-dnsdumpster.com/archive/master.zip --user
echo "copying dnsdumpster API_example.py script and modify it as ADS_youtube.py."
echo "replacing API_example.py domain 'tsebo.com' with 'googlevideo.com'."
sed 's/tsebo.com/googlevideo.com/g' /etc/dnsdumpster/dnsdumpster/API_example.py > /etc/dnsdumpster/dnsdumpster/ADS_youtube-temp.py
echo "remove some script lines from script to resolve XLS base64 errors as it is not required"
awk 'NR!~/^(35|36|37|38)$/' /etc/dnsdumpster/dnsdumpster/ADS_youtube-temp.py > /etc/dnsdumpster/dnsdumpster/ADS_youtube.py
echo "ADS_youtube.py script created."
echo "cleanup temp script file."
rm /etc/dnsdumpster/dnsdumpster/ADS_youtube-temp.py
echo "create update script /etc/pihole/youtube-ads.sh."
echo "youtube-ads.sh is used for maintaining youtube generated list youtube-ads-list.txt in pihole webroot for pihole update access."
echo "echo off" >> /etc/pihole/youtube-ads.sh
echo "rm /etc/dnsdumpster/youtube-domains.txt" >> /etc/pihole/youtube-ads.sh
echo "rm /etc/dnsdumpster/youtube-filtered.txt" >> /etc/pihole/youtube-ads.sh
echo "rm /etc/dnsdumpster/youtube-ads.txt" >> /etc/pihole/youtube-ads.sh
echo "python /etc/dnsdumpster/dnsdumpster/ADS_youtube.py > /etc/dnsdumpster/youtube-domains.txt" >> /etc/pihole/youtube-ads.sh
echo "grep ^r /etc/dnsdumpster/youtube-domains.txt >> /etc/dnsdumpster/youtube-filtered.txt" >> /etc/pihole/youtube-ads.sh
echo "sed 's/\s.*$//' /etc/dnsdumpster/youtube-filtered.txt >> /etc/dnsdumpster/youtube-ads.txt" >> /etc/pihole/youtube-ads.sh
echo "cat /etc/dnsdumpster/youtube-ads.txt > /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh
echo "#greps the log for youtube ads and appends to /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh
echo "grep r*.googlevideo.com /var/log/pihole.log | awk '{print $6}'| grep -v '^googlevideo.com\|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh
echo "#removes duplicate lines from /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh
echo "perl -i -ne 'print if ! $x{$_}++' /var/www/html/youtube-ads-list.txt" >> /etc/pihole/youtube-ads.sh
echo "#updates pihole blacklist/whitelist" >> /etc/pihole/youtube-ads.sh
echo "pihole -g" >> /etc/pihole/youtube-ads.sh
echo "youtube-ads.sh script created."
echo "chmod script to executable."
chmod +x /etc/pihole/youtube-ads.sh
echo "add youtube-ads.sh maintained list youtube-ads-list.txt from local webroot: http://localhost/youtube-ads-list.txt to pihole lists."
echo "http://localhost/youtube-ads-list.txt" >> /etc/pihole/adlists.list
echo "save current crontab to mycron.sav, add crontab job, to run youtube-ads.sh updater every 15 minutes."
crontab -l > mycron.sav
echo "*/15 * * * * /etc/pihole/youtube-ads.sh" > mycron
crontab mycron
rm mycron
echo "manual update first."
/etc/pihole/youtube-ads.sh
echo "done, now enjoy youtube ad free, thru your pihole."

Test or View generated youtube ads list in your webbrowser:

http://< pihole weburl >/youtube-ads-list.txt

Here you go: :popcorn::popcorn::popcorn:

use it free,it works for me :slight_smile:

3 Likes

@Joshua_joezoef_B_ZzO

Awesome work man. Can you please make it work on rasbian, too?

@Schrauber47
Have you tried it? If so, which command fails? Although I haven't tried it there seems to be nothing that's obvious why it shouldn't just work on Raspbian.

@DL6ER
Yes, I've tried it. The run command did not work proberly.

I was able to create the .sh install script, but the run command failed. I'm using Mac OS Terminal and connect to the Pi via ssh. This is, what it says:

"root@raspberrypi:~# chmod +x /etc/pihole/youtube-adblock-install.sh
root@raspberrypi:~# run /etc/pihole/youtube-adblock-install.sh
-bash: run: Kommando nicht gefunden."

The third line means "-bash: run: Did not find command."

Oh, that's not how you run a shell script. Just do:

./etc/pihole/youtube-adblock-install.sh

Well, but now it says:

root@raspberrypi:~# ./etc/pihole/youtube-adblock-install.sh
-bash: ./etc/pihole/youtube-adblock-install.sh: Datei oder Verzeichnis nicht gefunden

which means "file or directory not found".

Sorry for me being such a noob :smiley:

Edit: Haha okay, just need to leave out the first dot. Then it works just fine.

@Joshua_joezoef_B_ZzO

I followed your instruction and the pi hole seems to block almost any youtube ads now.

Only a few ads are getting through.

I had two error messages during the installation:

The first one is no error, just a note you can safely ignore.

About the second one ... what are the last lines of /etc/pihole/adlists.list ?

This is the content of the list:

GNU nano 2.7.4 Datei: /etc/pihole/adlists.list

##Zeustracker
https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist

##Disconnect.me Tracking
https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt

##Disconnect.me Ads
https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt

##Hosts-file.net
https://hosts-file.net/ad_servers.txt

www.youtube.com
#https://raw.githubusercontent.com/CamelCase11/UnifiedHosts/master/hosts.all
http://localhost/youtube-ads-list.txt

@Joshua_joezoef_B_ZzO

Awesome script, when I run it I get this error on the youtube-ads.sh

Blockquote
syntax error at -e line 1, at EOF
Missing right curly or square bracket at -e line 1, at end of line
Execution of -e aborted due to compilation errors.

you need to remove the line starting with perl -i -ne 'print if
in youtube-ads.sh

and replace it with

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

2 Likes

@Schrauber47 put a # in front of :this: entry and re-run pihole -g.

Hi and thanks for this script.
Right now I have about 150 domains in my list.

@itsme awesome, thank you for your help!

Is there a mistake in /etc/pihole/youtube-ads.sh ?
Line 10:
grep r*.googlevideo.com /var/log/pihole.log | awk '{print }'| grep -v '^googlevideo.com\|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt
Isn't anything missing after the awk'{print }'? Because it pastes the whole log there and not just the hostnames.
My idea was to just add the "$6" so it takes the hosts:
grep r*.googlevideo.com /var/log/pihole.log | awk '{print $6}'| grep -v '^googlevideo.com\|redirector' | sort -nr | uniq >> /var/www/html/youtube-ads-list.txt

Thank you for the great work and the idea!

Best Regards