Thursday, January 14, 2010

Automating repetitive tasks using Linux Shell programming

So, here is the scenario. Suppose you have a .wav file on your system and you want to convert it to .mp3. How do you do that?

In case, you are not already familiar with ffmpeg, well, you can always learn about it here. It can be used for many purposes but the main way in which i use it is for converting audio/video files from one format into another. The way to do that (after installing ffmpeg, of course) is to type in
    $ ffmpeg -i <input-File>.wav <output-File>.mp3
As simple as that !!!

Now, imagine that you have a folder that has plenty of .wav files and you want to convert all of them to .mp3 then, you could type in the name of each file and use the command as above which is really tiresome if you have plenty of .wav files. Or, you can use shell programming to automate the task as given in the code below.

Type in the code below and save it in some file, say

for arg in $( ls *.WAV)
    ffmpeg -i $arg -acodec libmp3lame $arg.mp3

In order to run the above program all you have to do is, type in the following in command line,
    $chmod u+x

There are many documentations available online for learning shell programming but you need not understand the complete thing to create small programs such as the one above. If you already know some programming then you need to know only the relevant stuff to write your own programs to automate whatever tasks that you are interested in.

Now, another similar scenario. Suppose I had plenty of .wav files and instead of converting I wanted to play them one after another with a gap of 3 seconds between any two .wav files. I just have to type the code below into a file and then run it in a similar fashion as the previous file.

for arg in $( ls *.WAV)
    mplayer $arg
    sleep 3

I hope that proves useful in automating the repetitive tasks for you on your system.


Post a Comment