Kludging an MP3 Bookmark

Suppose you, a run of the mill Linux user, just downloaded a very long mp3 – for example a 10 hour mp3 of Cory Doctorow reading The Hacker Crackdown. Naturally you’re not going listen to this in one sitting; but, you cannot find a media player that implements bookmarking. What do you do?

You remember that hitting ^C (control-C) while mpg123 is playing a file will cause it to output something like

[56:36] Decoding of sterling_podcast.mp3 finished.

and then quit. What’s more, the -k option to mpg123 takes an integer n and starts playing the file it was passed n frames in. (For those of you unfamiliar with the structure of mp3 files, they store the audio data in segmented frames containing 26ms of audio data.) A quick invocation of Google tells you that 1 second of playback takes about 38.461 frames. So if you had a way to store mpg123’s output, you could use “mpg123 -k n /path/to/file” as a commandline mp3 player with rudimentary bookmarking.

But how do you store the output?

One of the great things about the commandline environment is its ability to pass output data around. One of the important elements of this flexibility is the “Standard Output,” which I’ll refer to stdout. For the uninitiated, stdout is where all the commandline programs send their output; usually, (and this is crucial) but not always, this is simply your terminal screen. However, it is possible to redirect this output to a file using (in Bash at least) the construct &>. An example:

The command “ls /home &> contents.txt” will write a list of the contents of the /home to the file contents.txt.

So there’s the answer. You can use

“mpg123 -k n /path/to/file &> .bookmark”

to play your long file, hit ^C to stop at any point, and be secure in the knowledge that you’ve got a bookmark waiting there for next time. If you wanted to be really fancy you could even write a simple script to pull the stopping point out of .bookmark and, after computing what n should be, launch mpg123.

Et voilà!

Advertisements
This entry was posted in Computer Science, Off Topic. Bookmark the permalink.

2 Responses to Kludging an MP3 Bookmark

  1. Geoff says:

    Incidentally, if you decide to tack a & to the end of this command to make the file play in the background be *very* sure to bring mpg123 to the foreground and kill it with ^C rather than kill %jobID. If you use the latter, the time is not printed on shutdown. Which throws a wrench in the whole line.

  2. tunic says:

    Tunic says : I absolutely agree with this !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s