Youtube-cache with Squid3 on Ubuntu using Nginx and Ruby1.8
I've been wanting to set up a youtube-cache for a while now, just to save me on some bandwidth. As I use youtube heavily for music. I have it running really well now, although I think it has some issues when videos are in 480p 720p and 1080p. Works fine with 360p.
I would suggest setting this up in a VirtualBox machine.
Ubuntu Server 12, Nginx, Squid3, Ruby1.8.
Most of the code is used from here https://code.google.com/p/youtube-cache/ with some modifications.
- Install nginx, ruby, squid
sudo apt-get install nginx ruby squid
- Backup your existing nginx and squid configuration
mv /etc/squid3/squid.conf /etc/squid3/squid.conf.backup
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
- Put in new squid and nginx config
wget -O /etc/squid3/squid.conf https://dl.dropbox.com/u/44518415/youtube/squid.conf
wget -O /etc/nginx/nginx.conf https://dl.dropbox.com/u/44518415/youtube/nginx.conf
- You also need a re-director script a ruby app that will do the youtube downloading for you
wget -O /etc/nginx/nginx.rb https://dl.dropbox.com/u/44518415/youtube/nginx.rb
- Create the cache directories
mkdir /usr/local/www/nginx_cache
mkdir /usr/local/www/nginx_cache/tmp
mkdir /usr/local/www/nginx_cache/files
- Set the appropriate permissions and ownerships
chmod 755 /etc/nginx/nginx.rb
chown www-data /usr/local/www/nginx_cache/files/ -Rf
chown proxy:proxy /cache1
- Make sure squid and nginx is not running
- Now generate the Squid cache directories
- Now start nginx and squid3
- Nginx will be running on port 8081 and Squid3 will be running on 8080 as transparent
- Change your browser proxy settings to point to your youtube cache server or squid server on port 8080 and load a youtube video. Make sure it is in 360p. You can experiment with 720p etc later.
- Recently youtube has made a change with their streaming. They send video data in chunks of 1.7 Mb. So, you should end up with lots of files like this in /usr/local/www/nginx_cache/files directory.
-rw------- 1 www-data www-data 1.7M Mar 15 2011 id=0e095c881c062079.itag=34.range=10690560-12472319.algo=throttle-factor
-rw------- 1 www-data www-data 1.7M Mar 15 2011 id=0e095c881c062079.itag=34.range=12472320-14254079.algo=throttle-factor
-rw------- 1 www-data www-data 1.7M Mar 15 2011 id=0e095c881c062079.itag=34.range=13-1781759.algo=throttle-factor
-rw------- 1 www-data www-data 291K Mar 15 2011 id=0e095c881c062079.itag=34.range=14254080-16035839.algo=throttle-factor
-rw------- 1 www-data www-data 1.7M Mar 15 2011 id=0e095c881c062079.itag=34.range=1781760-3563519.algo=throttle-factor
-rw------- 1 www-data www-data 1.7M Mar 15 2011 id=0e095c881c062079.itag=34.range=3563520-5345279.algo=throttle-factor
-rw------- 1 www-data www-data 1.7M Mar 15 2011 id=0e095c881c062079.itag=34.range=5345280-7127039.algo=throttle-factor