Converting YouTube Videos to MP3 Files in Linux

Well, there are dozens of way to do this. And when I tell people about this, they simply say “Why bother? There are tons of websites that do this for you!” Yeah, that is true, it’s nice to do some stuff on your own.

First of all, I’m not gonna go into detail on how to GET the video from YouTube. There are lots of add-ons to do this for Firefox and Opera etc. I’m not sure if there is any for Chrome, probably the folks at Google don’t like the idea that much. Actually at this Yahoo! Answers question, the Official YouTube account has answered why: It’s against their terms of service.  Anyway, using anyone of these add-ons you can download the video.

If you wanna go extra lower-level and do it without any add-on, you have get the videos source code in HTML5 and parse the necessary parts. For different browsers, YouTube acts differently of course, so you have to handle it accordingly. There’s an old discussion about this on stackoverflow.

Well, let’s say you’ve downloaded the video. As you can guess, it can be easily done with avconv (or ffmpeg) like,

$ avconv -i “my video.flv” “my video.mp3”

And that’s it! But, if you’re downloading lots of files (which I sometimes do for preparing playlists for the bands I play at) it is easier to do it in a bash line. The line below converts every file with the flv extension, and removes the “- YouTube” part from the name (the Opera add-on adds it) then deletes the flv file.

for i in *.flv; do avconv -i "$i" "${i// - YouTube.flv}".mp3; rm -f "$i"; done

When dealing with videos on YouTube, it is important to select which source to download. Don’t forget that the quality of the video also effects the quality of the sound. So getting a low quality to convert to mp3 will most likely result in a low quality mp3. Well, getting the highest quality video won’t help go easy on your bandwidth either.  Also video on YouTube don’t support every file type. So you have to choose between various codecs. A detailed list of codecs available on YouTbe are presented here in this wikipedia page :

Now don’t worry, you don’t have to study that table to get the optimal output. I’ve done the homework for you guys. The table below is in such an order from high audio quality to low audio quality. If two of them are the same quality, it is ordered in a fashion that the latter one uses more bandwidth.

1) webm 720p (192k)
2) mp4 720p (192k)
3) webm 360p (128k)
4) flv 360p (128k)
5) flv 480p (128k)
6) webm 480p (128k)
7) mp4 270p (96k)
8) mp4 360p (96k)
9) flv 240p (64k)
10) flv 270p (64k)

So this means, downloading webm720p will cost less bandwidth than mp4 720p but will result in the same audio quality.

Unfortunately the one-liner above I’ve provided doesn’t always preserve the Audio quality on the output. This was discussed in a stackoverflow post earlier and I’ve provided my modified version of a bash script that uses ffmpeg to do the job.

#!/bin/env bash
for f in *.${ext}; do
    x=${f%.*} ;
    x=${x% - YouTube}; # I usually download some song covers from YouTube.
    bit=`ffmpeg -i "${f}" 2>&1 | grep Audio | awk -F", " '{print $5}' | cut -d' ' -f1`
    if [ -n "$bit" ]; then
        ffmpeg -i "$f" -ab ${bit}k "$x"
        ffmpeg -i "$f" "$x" # this is if we don't have an output from the grep line above, ffmpeg will keep the original quality, that is 192k for 192k

So this script gets the file extension you want to convert (flv ?) and greps the Audio quality of each of them and converts accordingly. Simply use it like this :

$ bash flv

Of course you’ll need the ffmpeg and libavcodec-extra-53 packages installed on Ubuntu.

$ sudo apt-get install ffmpeg libavcodec-extra-53


Installing WordPress on CentOS 6.x

Let’s say you’ve got your new CentOS 6.x server up and running, with a minimal install. It is very easy to install the necessary items (including Apache, MySQL etc.) alongside WordPress to fully function.

First, let’s install the LAMP installer for CentOS 6.x With this script, you’ll get everything you need to turn your Linux box into a web server. Details on how to use it is available on

After this, we can easily get the wordpress installation and simply extract it.

tar -xzvf latest.tar.gz

Now get into MySQL using the password you’ve specified to

mysql -u root -p

Below, we’ll create a new database and new user to affiliate with.

CREATE USER wordpressuser@localhost;
SET PASSWORD FOR wordpressusername@localhost= PASSWORD("password");
GRANT ALL PRIVILEGES ON wordpress.* TO wordpressusername@localhost IDENTIFIED BY 'password';

Of course, you’ll have to replace “wordpressuser” and “password” with yours.
After creating the necessary database, we tell wordpress itself about these.

cp ~/wordpress/wp-config-sample.php ~/wordpress/wp-config.php
vi ~/wordpress/wp-config.php

Open and edit the wp-config.php file according to change the database info as below (yet again, replace with yours)

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'password');

Save and exit.
Now we should move our files to the user folder we've created with and change the files' owner.
cp -r ~/wordpress/* /home/username/public_html/
chown -R username:username /home/username/public_html/
chmod -R 755 /home/username/public_html/

Now visit your domain, or IP address on your browser. It will redirect you to the WordPress onscreen installation instructions. If it doesn’t, add “wp-admin/install.php” to the end. (like

And you’re done!

Also, if you want to import an XML file from your old WordPress blog or something, you’ll need to install the php-xml package.

yum install php-xml

Adding Cronjob via Script

Here’s another snippet I don’t want to forget,
adding a cronjob via scripting.
Here’s the source for it, :

Don’t forget that it only works on BASH! Doesn’t work on sh.

command="$mypath/ > /dev/null 2>&1"
job="*/5 * * * * $command"
cat <(grep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -

UNIX Process Time Bomb

Here’s a simple script that kills a process if it lives longer than the time specified. It’s written in Python, and is available on github.

Usage : <process-name> <minutes>

Example : $ firefox-bin 20

Outcome : This will kill the process named firefox-bin if it has been running longer than 20 minutes.

Crontab : You should probably add this to your crontab!

Dependency : Standard UNIX tools : Python 2.4.x, pgrep, ps, kill etc.

# A Pythonic Time Bomb
# Kills Processes Living Longer than the specified time.
# Don't Forget to add it to your crontab!

import subprocess, sys
if len(sys.argv) != 3 :
    print "Usage : <process-name> <time-in-minutes>"
    print "Takes only and exactly 2 arguments."
    raise SystemExit
try : int(sys.argv[2])
except :
    print "%s is not an integer." % sys.argv[2]
    raise SystemExit
try :
    if a == '' :
        raise SystemExit
    else :
        procc = subprocess.Popen(["ps -o pid,bsdtime -p $(pgrep %s)"%(sys.argv)[1]],shell=True,stdout=subprocess.PIPE).communicate()[0]
except : raise SystemExit 
for lines in procc.split('\n') :
    if lines != '' :
        if l[0] == 'PID' : pass
        else :
            if int(l[1].split(':')[0]) >= int(sys.argv[2]) :
                try : killer = subprocess.Popen(["kill","-9",l[0]],stdout=subprocess.PIPE).communicate()[0]
                except : pass
            else : pass

My vimrc file

Here’s my vimrc file. Simple, not much, Just some basic stuff for coding.

" Setting the tab stop distance
set tabstop=4 shiftwidth=4
" Places "spaces" instead of "tabs"
set expandtab
" Show line numbers
set number

color torte

" Highlight the current line, also highlights only the focused window.
hi CursorLine cterm=NONE ctermbg=blue ctermfg=NONE
autocmd WinEnter * setlocal cursorline
autocmd WinLeave * setlocal nocursorline
set cursorline

" Encoding
set encoding=utf-8

" Highlight Search Matches
set hlsearch

edit : maybe you’d prefer this extraordinary tool!