pk - music taste tracker

pk is a shell script I wrote to keep track of how my tastes in music change. It is a wrapper around your music player of choice (in my case, Mplayer). It generally works for video and any type of file as well.

Rationale

All of the media players I have looked at have had problems. Either they are inconvenient to use (when possible I like to use the keyboard exclusively), or they don't allow me to organize my music how I would like (I name songs from musicals and songs from anime and songs from regular bands differently), or like 99% of players, they don't keep track of how often I listen to songs! So I spent a night and a bit in Emacs, and now I have a system that does all of the above: Bash and <insert favourite media player here>.

Setting Up

Download the pk shell script at the bottom of this page, save it as pk in a directory in your path, then create symbolic links to it from all of these names:

pk.ls pk.ls.world pk.mv pk.mv.test

The default player associations are:

Program File types
mplayer mp3, ogg, mpg, mpeg, avi, mov, mkv, ogm, mp4, wma, wmv, flv
timidity mid, midi

If this is fine, then skip to the next section to learn how to use the script. If you want to change this, change the code on the lines after case "$EXT" in.

Playing Media

There is a single shell script, called pk, that is used to play a single file. It is symlinked to other files for other functions. The complete set of files and links are listed below. Note that for pk to do anything with a file, it must compute the file's md5sum; this could take time if a file is absolutely massive (hundreds of megs), but for smaller files, the file is already cached in memory by the time playback starts, so slowdowns in playback may be reduced.

Note that file extensions are case-sensitive. Completely lower-case extensions and completely upper-case extensions are recognized.

Command Description
pk file.ext Play the file with the configured player. If the player returns with exit code 0, pk increments the play count for the file.
pk.ls file.ext Prints file statistics for an individual file.
$ pk.ls 'AFI - Miss Murder.mp3' Overall: 13 plays Today: 1 play (7.69231%) $ pk.ls 'Angelus - Shimatani Hitomi (6th Opening).mp3' Overall: 51 plays Last played on 2007 08 11
pk.ls.world Prints all of the media in the database with play counts, sorted by play count in descending order.
pk.mv srcfile.ext destfile.ext Renames a media file, keeping the play counts up to date.
pk.mv.test srcfile.ext destfile.ext Pretends to rename the file; displays the files that would be renamed.

How the Play Count Works (and Limitations)

You most likely want to read this section, so that you don't accidentally mess up your play counts.

pk creates a hidden directory, called .play_history, in your home directory and keeps a file for each media file played. For a media file named file.ext, there is a stats file is named file.ext.index. Because you may have more than one file with the same name, an index is assigned to distinct files. This is so that you can have hard links or symlinks and as long as they have the same name, pk will still see it as the same file. Unfortunately, two files with different names but some contents are not currently seen as the same file.

The first line of a stats file is the md5sum of the corresponding media file. The second line is the total number of plays of the file. Lines after the second are in the format yyyy mm dd plays, with one line for every day the song was played. These files can be hand-edited at will. pk only writes to them when the media player exits successfully.

pk.mv should always be used to rename files in your media collection, so that the stats files get renamed too.

Version 2

The code in pk is not pretty — the unnecessary regexes and the crap necessary to add one — so I intend to eventually rewrite it a proper language. Maybe this one. Then I'll add support for changing a file's metadata and being able to update the md5sum in the stats file too. In the mean time, do it with your favourite text editor.

Version 2 should also include more statistics and graphs and useful stuff which is the entire point of collecting play counts.

pk?

Originally I called the script pl (short for "play" or something, now there's creativity), but then I took a Principles of Computer Science course, in which we used SWI-Prolog, whose binary is conveniently named pl. So I renamed my script. Now it's slightly easier to type (on qwerty keyboards at least). Play kounter?

AttachmentSize
pk - music taste tracker4.72 KB