Thursday, October 31, 2013

Download YouTube Videos, Channels and Playlists with Youtube-DL

Today, I found a fantastic YouTube channel with loads of videos that interest me. Since I especially enjoy watching videos when I do not have an Internet connection, I tend to download videos from youtube to watch them later. However, this has always been a pain in the behind to do, especially if you are following a certain channel and would like to have the latest videos…

Well, if you are a Ubuntu user, it takes about 1 minutes to setup and start downloading.

First of all you need "youtube-dl", it's a simple command line tool to download videos from YouTube.

Get it via:
# apt-get install youtube-dl
or find your right download here:
http://rg3.github.com/youtube-dl/

Once installed, youtube-dl will download into the folder you are currently in, so I suggest creating folders first. I am giving some examples below on how I am using youtube-dl.

You could download a single youtube video by entering in the console:
# mkdir ~/YouTube/various
# cd ~/YouTube/various
# youtube-dl -citw http://www.youtube.com/watch?v=bV9L5Ht9LgY

To download the youtube channel http://www.youtube.com/user/celebrateubuntu by entering in the console:
# mkdir ~/YouTube/channels/celebrateubuntu
# cd ~/YouTube/channels/celebrateubuntu
# youtube-dl -citw ytuser:celebrateubuntu

You could download a youtube playlist like http://www.youtube.com/playlist?list=PL512E30EA478B12D9, use:
# mkdir ~/YouTube/playlists/celebrateubuntu
# cd ~/YouTube/playlists/celebrateubuntu
# youtube-dl -citw "http://www.youtube.com/view_play_list?p=PL512E30EA478B12D9"

You can download multiple videos from different URLs in batch mode! Simply put the links to the videos, channels, and/or playlists in a text file, and youtube-dl does the rest, just pass the text file on as a parameter using -a option.
# mkdir ~/YouTube/ubuntu-videos
# cd ~/YouTube/ubuntu-videos
# youtube-dl -citw -a "ubuntu-videos.txt"

——- Example of ubuntu-videos.txt ——--
http://www.youtube.com/watch?v=ZZIA0agcd_Y
http://www.youtube.com/playlist?list=PL512E30EA478B12D9
ytuser:celebrateubuntu
———————————————-
Once the download is completed, you could run the file again at any time, youtube-dl will only download (new) videos (from the playlists/channels) that have not been downloaded before. You can of course add new links to the file, run it again (in the same folder as before), and youtube-dl will only download new videos. It's a perfect way to keep an offline version of the channels/playlists you like, you could even schedule it to run/update automatically in the background.

Now comes the icing!
You can turn this into a podcast downloader for youtube videos :)
# mkdir ~/YouTube/videos-to-audio
# cd ~/YouTube/videos-to-audio
# youtube-dl -citwk -a "videos-to-audio.txt" --extract-audio --audio-format mp3
This would download the videos and extract the audio into a separate mp3 file. If you do not add the "k" option, the downloaded video will be deleted after the audio extraction. This is a bad idea when using the batch files to update when new videos are available in the channels/playlists, as it would re-download all videos since they are no longer present in the folder.

-citw stands for:
c -- continue interrupted download
i -- ignore errors
t -- use video title in file name
w -- do not overwrite existing files

To view all command/attributes:
# youtube-dl --help



Enjoy!

- See more at: http://www.webdesignblog.asia/software/download-youtube-videos-channels-and-playlists-with-youtube-dl/#sthash.DsXbXSzI.dpuf

Tuesday, October 29, 2013

Limiting your users use of disk space with quotas

When you run a multi-user system it's possible for a single user to unduly hog the system, by filling their home directory with a lot of files, and filling a disk so that other users have no space of their own. Quotas are a system of preventing this. It's possible to setup limits on the amount of space a single user, or a single group, can use.
Using quotas on Debian is very straight forward as the Debian kernel packages all have quota support compiled in.
There are two ways to use quotas:
  • Per user
  • Per group
When using per-user quotas you are effectively giving a limit on how much disk space the specific user may consume. In the case of per-group quotas you're giving a limit on the total disk usage of all members in that group combined.
Generally I find it much more useful to apply quotas on a per-user basis, as this way you dont have to work out which member of a group is consuming all the space.
To setup quota usage on your system you'll need to do three things:
  • Mount your filesystems with quota support
  • Install the quota software
  • Configure your limits
Quota support is enabled as the filesystems are mounted, and so it must be specified in the options section of your /etc/fstab file.
Specifying the quota options is as simple as adding usrquota for per-user quota, or grpquota for per-group quotes in the options column.
For example this first entry without quotas:
/dev/hda1       /home       ext2    defaults           1       1
Would become the following with per-user quotas enabled:
/dev/hda1       /home       ext2    defaults,usrquota  1       1
Once this has been added you can remount the partition by rebooting, and install the software. This will setup the partitions to be mounted with quota support enabled, however you haven't enabled them for the running system.
To change the options run:
mount -o remount,usrquota /home
(Repeat for any other filesystems you wish to modify).
You will also need to load the quota kernel module, and cause that to be loaded at boot time. You can do that by running:
modprobe quota_v2
echo 'quota_v2' >> /etc/modules
The quota system has a notion of a soft limit and a hard limit. A soft limit is a limit which the user can live with for a while (by default 7 days). A hard limit is a limit which cannot be crossed ever.
These limits are recorded inside special files which we need to create for each filesystem which is being used with quotas.
Continuing with our example of /home we would create one file for group quotas and one file for user quotas:
touch /home/aquota.user
touch /home/aquota.group
chmod 600 /home/aquota.user /home/aquota.group
Now we're ready to install the software.
apt-get install quota quotatool
As you installed the software you will have been prompted to see if you wish to email your users when their quotas are exceeded, this is their soft limit.
If you don't reboot you'll need to turn on the quota system by running:
quotacheck -vagum
To set the soft limits to 200Mb and the hard limit to 250Mb for a user called skx you would run:
quotatool -u skx -bq 200M -l '250 Mb' /home
A cronjob will run every day to check and warn them if they exceed their soft quota (if you said 'Yes' to the email notification question when the packages were installed), and the user will be informed immediately if they exceed their hard quota:
skx@undecided:~/Articles$ mkdir f
mkdir: cannot create directory `f': Disk quota exceeded
skx@undecided:~/Articles$ 
The system administrator can see what the disk and quota usage is like by invoking:
repquota /home
If ever you wish to remove a quota for a user simply set their hard and soft limits to '0'.

Sunday, October 27, 2013

Ubuntu Apache2 : Change default DocumentRoot /var/www

By default the document root folder for apache2 in Ubuntu is /var/www.
This is where you can store your site documents.
In order to change the default site location to a different one, /opt/mysite use the following method.
A detailed steps to install LAMP on ubuntu is given here.
To do this, we must create a new site and then enable it in Apache2.

To create a new site:

Copy the default website as a starting point.

 - sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysite

Edit the new configuration file in a text editor “sudo nano” on the command line or “gksudo gedit”, for example:

 - gksudo gedit /etc/apache2/sites-available/mysite

Change DocumentRoot /var/www to  DocumentRoot /opt/mysite

Change the Directory directive, replace <Directory /var/www/> to <Directory /opt/mysite/>

You can also set separate logs for each site. To do this, change the ErrorLog and CustomLog directives. This is optional, but handy if you have many sites

Save the file

Now, we must deactivate the old site, and activate our new one. Ubuntu provides two small utilities that take care of this: a2ensite (apache2enable site) and a2dissite (apache2disable site).

 - sudo a2dissite default && sudo a2ensite mysite

Finally, we restart Apache2:

 - sudo service apache2 restart

FOR UBUNTU 13.10

Previously, files in /etc/apache2/sites-available had no extension. For example /etc/apache2/sites-available/default. Now a .conf extension is required.

Example:
If you had /etc/apache2/sites-available/some-site, in 13.04 you can just enable it using sudo a2ensite some-site. Now it will give you an error saying

ERROR: Site some-site does not exist!

To fix this, append a .conf to all your config files in sites-available. You can do the same in sites-enabled, or you can delete all the files and re-enable them each manually.
I recommend doing them manually since you probably need to fix each VHost (next step).

sudo find /etc/apache2/sites-available/ ! -iname '*.conf' -type f -exec mv '{}' '{}'.conf \;
if you decided to do them manually:

sudo rm /etc/apache2/sites-enabled/*
sudo a2ensite your-site-name