mpd

How to continuously poll mpd for currently played song and write result to a file?


The only thing i need to extract from mpd is the currently played song/track. I have to ensure this always is up to date in the output file.


Solution

  • If you install mpc then you can do the following:

    mpc idle player # block until the player changes songs
    mpc current     # outputs "Artist Name - Song Name" onto stdout
    

    Do those in a loop, and output the result of current into a file, and you're done!

    #!/bin/sh
    while true
    do
      mpc current > current_song.txt
      mpc idle player
    done
    

    The full list of what you can idle for is on the MPD command reference:
    http://www.musicpd.org/doc/protocol/command_reference.html#status_commands