I am using this command to play music in the background (without GUI):

mplayer -shuffle -playlist /home/geppettvs/NAS/Musica/playlist.m3u </dev/null >/dev/null 2>&1 &

Which results in music being gathered from the Music folder in a NAS, which is played in the background.

The playlist itself is just a playlist file made with Rhythmbox or any other music player software with the ability to manage playlists.

So, mplayer starts with a random song after which it will continue uninterruptedly playing music.

What I need is to somehow query mplayer and ask it for the song name which is currently playing in order to place it somewhere, let's say: write it in a text file for later usage. (Just the filename without path)

Right now once mplayer starts playing music from the playlist, some efforts to query what is mplayer playing will result in "playlist.m3u" as the title name instead of the actual mp3 filename.


I am open for any alternatives, just as long as there is a better way of doing this without a GUI. Just let me know and I'll try it. Any ideas are welcomed.


Something like this crazy script should work:

lsof -c mplayer | fgrep ".mp3" | awk -F"/" '{ print $NF; }' | cut -d'.' -f1

Basically it works this way:

  • lsof part looks for all open files that are opened because of mplayer
  • fgrep grabs the .mp3 assuming it is an mp3. I used fgrep because I need the dot symbol to also be taken into consideration.
  • awk comes in and basically delimits everything based on the slashes from where the files are located and then the $NF takes the last occurrence of this.
  • Since you wanted the file without the mp3 part I put a basic cut there for it, but this could also be done with the awk.

So now you have a way of grabbing the songs from a mplayer.

Note that the mplayer needs to be running for this to work since lsof checks open files.

| improve this answer | |
  • 1
    Brilliant solution. Been looking for this for a long time. – richrosa Aug 19 '16 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.