Authorize mplayer (and others) with hidden password via prompt on command line

Alec Jacobson

August 03, 2009

weblog/

I love to use mplayer and occasionally mpg123 to stream audio and video from my web server or else where online. But if the file I'm streaming is in a secure folder that uses http authentication, then I have to include my username and password in my execution. On the command line in a terminal this looks like:
mplayer -user username -passwd password http://hostname/file
or
mplayer http://username:password@hostname/file
Both of these display my password in plain text in my terminal. Here's a quick bash script solution I saved in a file called authmplayer.sh:
#!/bin/bash
#
# save current stty settings
oldstty=`stty -g`
read -p "User: " -e user
# disable echoing
stty -echo
read -p "Password: " -e passwd
# restore previous stty settings
stty $oldstty
echo "
mplayer -user $user -passwd [hidden] $1"
mplayer -user $user -passwd $passwd $1
Adapt and add your favorite mplayer options as necessary.
Note: I've noticed that a lot of programs use this format (-user username -passwd password) so I don't think it would be much of a stretch to convert this code into a generalized authorization wrapper.