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.
- I wrote following lines in a file ~/scripts/mplayer_helper.sh
#!/bin/bashThe 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.
fname20=`echo $1 | sed 's/ /\\ /g'`
fname=`echo $1 | sed 's/ /_/g'`
mv "$fname20" "$fname"
- Then I made it executable using
chmod +x ~/scripts/mplayer_helper.sh
- Then I created a soft link to this executable inside /usr/bin/ as follows:
sudo ln -s ~/scripts/mplayer_helper.h sudomplayer
- 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".
- 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.
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.
#!/bin/bashThe 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! ;)
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`
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