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/bash
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.
fname20=`echo $1 | sed 's/ /\\ /g'`
fname=`echo $1 | sed 's/ /_/g'`
mv "$fname20" "$fname"
mplayer $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:
cd /usr/bin
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
i=2
token=`echo $1 | cut -d"/" -f$i`
more=`echo $1 | cut -d"/" -f$i | grep -i ^$ | wc -l`
currDir=""
while [[ $more -eq 0 ]]
do
currDir=$currDir/
token=`echo $1 | cut -d"/" -f$i`
name20=`echo $token | sed 's/ /\\ /g'`
dir20=$currDir$name20
name=`echo $token | sed 's/ /_/g'`
dir=$currDir$name
if [[ $dir20 != $dir ]]
then
if [[ -a $dir ]]
then
xmessage -center -nearmouse $dir File exists! Aborting!!
exit
fi
mv "$dir20" "$dir"
fi
currDir=$dir
i=`expr $i + 1`
more=`echo $1 | cut -d"/" -f$i | grep ^$ | wc -l`
done
mplayer $currDir
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
4 comments:
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."
http://www.gap-system.org/~history/Biographies/Hopper.html
thanks Rose. Will definitely read that.
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
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.
Post a Comment