Friday, September 24, 2010

mplayer - not playing files with _space_ characters

Recently I upgraded all packages on Ubuntu and somehow since then I could not play video files with vlcplayer! Not just that, somehow Realplayer and default movie player lost the codecs they used to previously use! After this miracle, the only option left with me was to try mplayer. I had heard that it supports many common formats and so on. I tried that and faced another issue. It magically could not play files having space characters in it!!

Initially I used to manually rename the file from browser before double clicking it for mplayer. Later on I found this very hectic when following happened. I renamed a long file name replacing some 7-8 spaces by underscores (Note: spaces, it converts to %20 and cannot play such file, underscore works!) and soon realized that I wanted to see another episode (with similar long file name)!!! So, I got irritated a lot and said to my friend that I will soon write a script to rename all files such the spaces are replaced by underscores. Now, I realized that all main / important video files (movies, acad lectures and so on) were on portable hard disk and the list will get augmented day after day as I get something good from some source, I will have a copy. Thus, this method sounded a bit overkill - to rename all file names just like that!

I then did following.
  1. I wrote following lines in a file ~/scripts/
    fname20=`echo $1 | sed 's/ /\\ /g'`
    fname=`echo $1 | sed 's/ /_/g'`
    mv "$fname20" "$fname"
    mplayer $fname
    The idea here is simple. Just create a file name (do some trials on command line first as to how exactly "mv" expects the input.) replacing space by underscore and replace file using "mv" command. Once that is done, invoke mplayer on renamed file.
  2. Then I made it executable using
    chmod +x ~/scripts/
  3. Then I created a soft link to this executable inside /usr/bin/ as follows:
    cd /usr/bin
    sudo ln -s ~/scripts/mplayer_helper.h sudomplayer
  4. Now, when I want to open video from browser, I simply right-click it and say "Open with". Then, I expand the option "Use custom command" and write "/usr/bin/sudomplayer" and say "Open".
  5. This not only opens the video by run-time substituting spaces with underscores but also once opened, adds this sudo application into the list of applications. Additionally, sudomplayer will be the default application shown for that file format then onwards.
Easy! Isn't it!

But, there is a glitch, what if not only the file to be played but also the recursive folders it is contained in have spaces. mplayer fails then as well! But, the solution is simple! Modify the script to take care of that as follows.
xmessage -center -nearmouse Note that this tries to rename the files so this might fail if the file path does not have write permissions
token=`echo $1 | cut -d"/" -f$i`
more=`echo $1 | cut -d"/" -f$i | grep -i ^$ | wc -l`
while [[ $more -eq 0 ]]
        token=`echo $1 | cut -d"/" -f$i`
        name20=`echo $token | sed 's/ /\\ /g'`
        name=`echo $token | sed 's/ /_/g'`
        if [[ $dir20 != $dir ]]
                if [[ -a $dir ]]
                        xmessage -center -nearmouse $dir File exists! Aborting!!
                mv "$dir20" "$dir"
        i=`expr $i + 1`
        more=`echo $1 | cut -d"/" -f$i | grep ^$ | wc -l`
mplayer $currDir
The idea is here, to go one directory at a time. Try to rename it and then keep track of current directory such that now next level directory can be renamed. But, WARNING!! What if the current directory is inside "/home/xyz/abc/"? One might not have permission to replace "xyz" with "xyz" inside /home/. There are two ways to look at it. First, mostly the filenames with space character won't appear in such so_called __system_prohibited__ paths and even if they appear, it's better not to mess with it (at least automatically!) and thus, the initial "xmessage" serves the purpose. Another WARNING! is - what if we are trying to rename directory "dir with spaces" and there already exists a directory called "dir_with_spaces". To prevent such collisions, we will terminate the script once we find such case. Work around to this problem is to use some weird set of characters instead of "_", say like "_-_" or so; but, then if this script is run quite frequently, then your filesystem (portable hdd in my case) will be full of such weird names! ;)

Anyways, another (probably best?) way is - just Google around! Then will tell you some ways to get around to this problem of mplayer not playing files with space characters. But, then you will miss all the scripting_fun! :D :P


    Rose Catherine said...

    hey! nice one!
    well, when you told me that you will write a script to automatically convert the space to underscores because you were irritated, i didn't know it would happen this soon.. and that it would not be a 2 line script !

    you may be interested in reading about Grace Hopper, the developer of the first compiler, in 1952.

    "Hopper's reason for designing a compiler was, she wrote later, because she was lazy and hoped that the introduction of compilers would allow the computer programmer to return to being a mathematician."

    Megha said...

    thanks Rose. Will definitely read that.

    Megha said...

    I read it! :) I had heard of her in the context of Grace Hopper scholarship but did not know other things. Thanks!
    And, btw, I am sure that even the longer script can be written in say 3 lines ;) with more expertise in scripting! I e.g. am not very comfortable with awk - but I believe that will make life still easier! :D

    Bush said...

    I was just curious about something. I have Mplayer on my machine that runs Windows. Just how does one play files with spaces in their names with the Windows version of Mplayer? Thank you for your help.