Tuesday, October 16, 2012

Create Playlist for Bible MP3's

I bought a copy of the English Standard Version Bible in mp3 form. Unfortunately, once you load it onto you MP3 player or smartphone, you have a hard time skipping around. I like to read a chapter from the Old Testament, a chapter from the New Testament, a chapter from the "wisdom literature", and a chapter from Psalms. Doing this, skipping around, and remembering which chapter you're on is painful.

That is... until you can create your MP3 play lists! To do the following, you have to have cygwin, Mac OS terminal, or a PC with Linux installed.

  1. Then, with file names like:
    Bible - ESV 0934.mp3
    Bible - ESV 0935.mp3
    Bible - ESV 0936.mp3
    Bible - ESV 0937.mp3
    Bible - ESV 0938.mp3
    Bible - ESV 0939.mp3
    Bible - ESV 0940.mp3
    Bible - ESV 0941.mp3
    You determine which ranges of files you want for your custom playlist. For me, that was
    • Old Testament 1-441,688-941 (without "wisdom literature" or Psalms)
    • New Testament 942-1205
    • Wisdom Literature 442-483,637-687
    • Psalms 484-636
  2. Now I create the M3U header for a given range. Let's say we're doing the Old Testament range:
    echo "#EXTM3U" > old_testament.m3u
  3. Construct a sed command for each range like:
    Then, concatenate all these sed commands together.

    e.g., for the Old Testament range above,you would create these sedcommands:

    /0001/,/0441/p; /0688/,/0941/p;
  4. Now, stick that filter into the following command after navigating to the ESV Study Bible MP3 directory:
    find . -type f | \
        sort -u | \
        sed -n -e 's/^\.\///; YOUR_SED_COMMANDS_HERE'\
        >> old_testament.m3u
    e.g. For the Old Testament example:
    find . -type f | \
        sort -u | \
        sed -n -e 's/^\.\///; /0001/,/0441/p; /0688/,/0941/p;'\
        >> old_testament.m3u
Stick that .m3u onto your MP3 player or smartphone and you're off to the races.

For Android install MortPlayer and it will help you keep track of where you are in each of the playlists.

Creating one List

If you want to combine all the lists into one mega list that automatically switches between your subranges, use a python script like the following:
class RangeItr:
  def __init__(self,ranges):
    self.ranges_ = ranges
    self.outer_range_idx_ = 0
    self.inner_range_val_ = ranges[0][0]-1
  def __iter__(self):
    return self
  def next(self):
    cur_range = self.ranges_[self.outer_range_idx_]
    if self.inner_range_val_ > cur_range[1]:
      self.outer_range_idx_= (self.outer_range_idx_+1)%len(self.ranges_)
      cur_range = self.ranges_[self.outer_range_idx_] 
      self.inner_range_val_ = cur_range[0]
    return self.inner_range_val_

ranges =( RangeItr( ( (1,441),(688,941) ) ),
          RangeItr( ( (942,1205) ,) ),
          RangeItr( ( (442,483),(637,687) ) ),
          RangeItr( ( (484,636), ) )
r = 0
f_num = 0
f = None
for i in range(0,10000):
    if i %200 == 0:
        f_num += 1
        f = open('bible_%02d.m3u'%f_num,'w')
    f.write("Bible - ESV %04d.mp3\n"%ranges[r].next())
    r = (r + 1 ) % len(ranges)