Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am contemplating using mpg123 as an audiobook player. I can't find any other good audiobook players for Linux, and I think mpg123 may be my best option.

My audiobooks are organized by directories and the track names are numbered (e.g., Track-01.mp3, Track-02.mp3, etc.).

What I am seeking is a way to save the last location played (the track and the position within the track) when I stop listening, and then be able to start mpg123 at that place in the audiobook the next time I listen.

It would be ideal to have this "last location" info saved in a text file in the directory. That way I could start each audiobook at the last location by using the text file stored in that audiobook's directory.

A similar bookmark feature would be nice too. It would be almost the same implementation, it seems. The "last location" info could be saved in a text file named e.g. "last" and each bookmark could be saved in a text file named bookmark.N (where N simply increments).

Is a trivial implementation possible, maybe as a simple bash script? I'm not a developer.

share|improve this question
add comment

3 Answers

You can utilize mplayer's screen output to produce an output file containing the times (they are ouput at a rate of about 10 per sec). To prevent large output files, this script toggles between 2 log files, suffixed with 0 or 1. They are alternately deleted or written to. Barring a crash, There should only be one log left after mplaye stops playing that particular audio.

This remaining log is then processed by the sed snippet which reduces it to just the last timestamp, which is in a decimalized seconds format, and writes it to a file, suffixed .last (and removes the log).

Here is the script

file=~/test.mp3
mplayer "$file" 2>&1 |tee <( 
  awk -v"RS=\r" -v"recs=500" -v"file=$file" '
  { prev = extn
    para = int(NR/recs)
    extn = para%2
    print $2 > file"."extn
    if( prev!=extn ) {close(file"."prev)
        system("rm -f " "\""file"."prev"\"") }}')

# On exiting mplayer, create the  `.last` file           
sed -n '/^[.0-9]\+/p' "$file".[01] |
sed -n '$p' > "$file".last
rm -f "$file".[01]

To start mplayer at the last indicated position, run the following command.

mplayer -ss $(cat "$file".last) "$file"
share|improve this answer
    
For listening to an audiobook, what I would need is a script that also calls the audio player for each file in the directory (in order) and only saves the position for the files that are not played completely to the end. The .last file would need to include the file name as well as position. And, using this approach, a list of files would have to be maintained so that the continued listening would play the remaining files. It seems doable, but it is also beyond my scripting skills. –  MountainX Apr 22 '12 at 0:24
add comment

Although I find @Peter.O's mplayer output parser quite sexy, may I suggest you try mpd, the music player daemon? It's a very capable music player and playlist backend (with no builtin frontend); it will by default remember the last playback state (volume, track, track-position, if the track was playing/paused, etc), so playback will be resumed once the daemon starts up, presumably during boot.

There are several console as well as GUI frontends - the mpc console client is very simplistic; ncmpcpp us quite advanced, and gmpc is a nice GTK2-based client.

Although it's bulkier than having a simple script, it does help you with playlists, favorites, etc whilst still providing ease of use and a variety of frontends.

share|improve this answer
add comment
up vote 0 down vote accepted

Thomas Orgis, a mpg123 developer and maintainer, just implemented this functionality in mpg123 (as a script called 'conplay') at my request.

His description is:

This little wrapper runs mpg123 on a given directory (hand in '.' for the current one), playing all *.mp[123] files therein in terminal control mode. The extra trick is that a playlist file (conplay.m3u) is read and updated (created) with the position you left playback at (via 'q' key), to return on next invokation.

The name stands for CONtinued PLAYback. What did you think?;-)

I think it is brilliant! It does exactly what I asked for in my question above. I've been using it all day and it works flawlessly. I could not be happier!

You can get it from http://mpg123.org/snapshot

Thanks Thomas!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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