26 September 2011

The Scriptures in Audiobook Form, and How to Do It Yourself

also called "How Shell Scripts Saved the Day".

As I'm sure many of you know, I'm a Mormon, and as a Mormon, we are taught to read and study the scriptures. The Church of Jesus Christ of Latter-day Saints has provided many ways to bring the scriptures to the people. In addition to the print edition of the scriptures, the Church provides them online, in mobile applications, and in audio form as a set of compact discs or a set of MP3 files which can be downloaded gratis and played on an audio player (e.g. an iPod).

The thing is, that they're provided as a set of MP3 files, with one MP3 file for each chapter of all the scriptures. This is fine if your MP3 player can only handle MP3 files, but if you have an iPod, iPhone, or iPad, your device supports audiobooks with the M4B extension, which are much nicer to manage, and they can remember your position as you listen to them. They can also be divided into chapters, but these chapters are in a single file, which is nice if you're listening through a book, so that the chapters play together in order as opposed to them playing in a random order. They also appear as audiobooks in iTunes, as opposed to music.

This is how I have it with my copy of the scriptures in audio form. I did this a few years ago, using a shareware app whose name I don't remember, on Macs in Queens College's computer labs (as I didn't have my own Mac at the time, and you could only make these audiobooks with chapters on a Mac). I was on these computers a few hours at a time, entering information by hand and the rather slow book-by-book process (and by "book" I mean the subdivisions found in the Bible, the Book of Mormon, and the Pearl of Great Price). After a couple of weeks of this, I had a complete set of the Standard Works in audiobook form, for my enjoyment.

Flash forward to a couple of weeks ago. My mom asks me if I can put a set of audio scriptures – in Spanish – on an iPod for someone. I figure, "Well, I might as well give it the same treatment."

There's a problem though. The software that Apple released to make these audio files with chapters, called ChapterTool, requires Rosetta, support for which was dropped in Lion.

After a bit of Google searching, I eventually found a utility which can make audiobooks like the ones I made a few years ago. It's called Audiobook Binder. It works on Lion, and it's also available on the Mac App Store. I found the GUI to be a little confusing, since dropping in a bunch of files doesn't automatically make each one a chapter. That has to be done manually. Fortunately, the developer of the app has a command-line version called abbinder, not available on the Mac App Store, but a lot more useful, since it could take a list of files, bind them together in an M4B file, with one MP3 file per chapter, and automatically set the title of each chapter using the title attribute of each of the MP3 files. However, with thirty-nine books in the Old Testament alone, I wondered if I could simplify and automate the process further.

That's when a light bulb clicked on: Shell scripts! I could use a shell script to generate the list of files to be bound into each book, then pass that list to abbinder which would churn out the audiobook file. After reading up about shell scripting, I managed to hammer out the following script:

#!/bin/bash
OT_DIR=~/Desktop/ScripturesSpanish/Source/OldTestament
NT_DIR=~/Desktop/ScripturesSpanish/Source/NewTestament
BOM_DIR=~/Desktop/ScripturesSpanish/Source/BookOfMormon
PGP_DIR=~/Desktop/ScripturesSpanish/Source/PGP

# NOTE: The BOM_DIR variable was not included in the original, but should work
# IF you follow my instructions.

# Do the Old Testament first
echo "Old Testament..."
cd $OT_DIR
for book in Genesis Exodus Leviticus Numbers Deuteronomy Joshua Judges Ruth 1Samuel 2Samuel 1Kings 2Kings 1Chronicles 2Chronicles Ezra Nehemiah Esther Job Psalms Proverbs Ecclesiastes SongofSolomon Isaiah Jeremiah Lamentations Ezekiel Daniel Hosea Joel Amos Obadiah Jonah Micah Nahum Habakkuk Zephaniah Haggai Zechariah Malachi
do
ARPASS="*_"$book"_*"
ls -1 $ARPASS > $book
../../abbinder -v -c 1 -r 32000 -i $book -E '%t' ../../OldTestament/$book.m4b
done

# Next, do the New Testament
echo "New Testament..."
cd $NT_DIR
for book in Matthew Mark Luke John Acts Romans 1Corinthians 2Corinthians Galatians Ephesians Philippians Colossians 1Thessalonians 2Thessalonians 1Timothy 2Timothy Titus Philemon Hebrews James 1Peter 2Peter 1John 2John 3John Jude Revelation
do
ARPASS="*_"$book"_*"
ls -1 $ARPASS > $book
../../abbinder -v -c 1 -r 32000 -i $book -E '%t' ../../NewTestament/$book.m4b
done

# Pearl of Great Price
echo "Pearl of Great Price..."
cd $PGP_DIR
for book in Moses Abraham Facsimile JosephSmith_Matthew JosephSmith_History ArticlesOfFaith
do
ARPASS="*_"$book"_*"
ls -1 $ARPASS > $book
done

cat Facsimile >> Abraham

for book in Moses Abraham JosephSmith_Matthew JosephSmith_History ArticlesOfFaith
do
../../abbinder -v -c 1 -r 32000 -i $book -E '%t' ../../PGP/$book.m4b
done

# Finally, do the Book of Mormon.
# This section was not done in my original, but it should work
# IF you follow my instructions below this code in the blog post.
echo "Book of Mormon..."
cd $BOM_DIR
for book in 1Nephi 2Nephi Jacob Enos Jarom Omni WordsOfMormon Mosiah Alma Helaman 3Nephi 4Nephi Mormon Ether Moroni
do
ARPASS="*_"$book"_*"
ls -1 $ARPASS > $book
../../abbinder -v -c 1 -r 32000 -i $book -E '%t' ../../BookOfMormon/$book.m4b
done

Making sure that all the folders referenced to in the script were in place (especially the ones that abbinder would write to, I ran it. I should note that I did The Book of Mormon manually, and the Doctrine and Covenants using a shell script, but since that is just a single volume with 138 sections and two Official Declarations, I manually edited a list, dividing the D&C into segments of at most 50 sections each, then ran a shell script for that, simply calling abbinder for each list. After about an hour and a half, the scripts were done, and I could import the M4B files into iTunes, where I edited the metadata so that the track numbers would match the order of the books in each volume, rename the files to the corresponding book names in Spanish, set the Album and Grouping to the volume name, and transferring the files to the iPod, and then setting up playlists for each volume. This was a LOT simpler than that shareware app, whose name I don't remember, as the whole process took less than a day total. That's why I declared shell scripts the Hero of the Day on Friday.

As for doing this yourself, here's what you need:
• a Mac
• the abbinder command-line utility (link above)
• the shell script above
• The audio downloads from http://lds.org/audio. Be sure you download the complete ZIP files for each volume.
Please note that these downloads will total between 3 and 4 gigabytes of data, so you will need about triple that on your hard disk for the original archives, the files contained therein, and the audiobook files generated by abbinder.

Now, here's what you do.
• Paste the text of the script into a new text file. Save it to your desktop. I will call it scriptureBind for this example.
• Create some folders on your desktop. Starting with one called ScripturesSpanish on the desktop itself. (NOTE: You can change the name of this folder if you want, but you'll need to change the names of the variables at the start of the script to match the name you give it). This will be called the Base Folder.
• In this folder, create five folders: OldTestament, NewTestament, BookOfMormon, PGP, and Source.
• Go into the Source folder you created and unzip each archive into its own folder here. Rename these OldTestament, NewTestament, BookOfMormon, and PGP, resprectively for each of the volumes.

• Open a Terminal window and type the following commands, one at a time:
cd ~/Desktop
chmod u +rx scriptureBind
./scriptureBind
The first line changes the Terminal's current working directory to your Desktop. The second line adds the read and execute permissions for your user to the file scriptureBind you created. The third line actually executes the script.

For the Doctrine and Covenants, as well as the introductory portion of the Old and New Testaments, the Book of Mormon, and the Pearl of Great Price, I had to edit these manually. Unzip the Doctrine and Covenants into your Source folder and call it DC. Then, go back to Terminal and type this: ls -1 > DC.txt

In Finder, find that file and make several copies of it. The exact number depends on how many segments of the D&C you prefer to have. I prefer fifty sections per segment, so I make four additional copies, but if you prefer 25 sections per segment, you'll need seven (1 through 25, 26 through 50, … 126 through 138, Official Declarations). Rename each file according to your preferred dividing of the sections (e.g. if you do 50 sections per segment, you'll name them Intro, 1-50, 51-100, 101-138, and OD). Edit these files in TextEdit and delete any lines not relevant to the segment the volume the file is supposed to be for. For example, if you're in a file that's supposed to include only sections 26 through 50, you delete the lines listing the introductory files and sections 1 through 25, as well as those listing sections 51 through 138. and the Official Declarations. Save the file and repeat for the remaining sections. Then make a DC folder in your Base Folder, then make another text file, and saving it to your Source/DC folder as dcBind.

#!/bin/bash
../../abbinder -v -c 1 -r 32000 -i Intro -E '%t' ../../DC/Intro.m4b
../../abbinder -v -c 1 -r 32000 -i 1-50 -E '%t' ../../DC/Sections001-050.m4b
../../abbinder -v -c 1 -r 32000 -i 51-100 -E '%t' ../../DC/Sections051-100.m4b
../../abbinder -v -c 1 -r 32000 -i 101-138 -E '%t' ../../DC/Sections101-138.m4b
../../abbinder -v -c 1 -r 32000 -i OD -E '%t' ../../DC/OfficialDeclarations.m4b

Once you've saved that file, go into Terminal, cd into the Source/DC folder inside your Base Folder, chmod the dcBind file and then run the script.

Once you have this dcBind file, you can copy it using the Finder to other folders to perform similar bindings to the introductory portions of the other volumes. Just make and edit a list as described above, and edit the script to refer to the lists you use and the desired output location.

Once the process is complete, you can add them to iTunes, edit their metadata, and add them to your iPod.

Happy listening!

No comments :