Pages

Monday, February 20, 2017

Rename & Combine Audio Book files into one audio book.

I like to listen to audio books and I get them them from places such as Audible, books on CD, the library or LibriVox. The issue is that all these places present the files to you in different ways. You can get one big file or a lot of small files. They all use different naming conventions which can make organizing your books difficult. To play my audio books I use the iBooks app from Apple and the Audible app form Audible, on my iPod Touch. Apples iBooks app works well but is missing some features that the Audible app has such as the bookmarking feature. The Audible app is really bad at playing books that are broken up into several files. The app will play the files out of order or show each file as a separate book.

So to fix the issues described above I recommend that you rename and/or combine all the files from one book into one file. Below I show the BASH script I wrote to fix this issue. I wrote and tested this script on a Mac. This script will also work on Linux and UNIX operating systems. After the files are combined the finder didn't show the right length for the audio book but when I imported the file into iTunes everything displayed right and the file worked fine.

The script below shows how to combine several MP3 files into one file. I put a comment after each command explaining what it is doing. If you have any questions about the script below ask it in the the comment section below.

script-book
Put contents of files here
#!/bin/bash
# This script was created on 20170216
# This script was created to combine MP3 files form audio books into one file.
# usage ./script-book bookname
#
if [ -z "$1" ]
  then
    echo -e "Please rerun the script with desired file name at the end \n 
              Example: ./script-book bookname"
    exit 1
fi
# The if statement checks for $1 variable. 
# If no variable is present then the gives error message and exits 

for s in $(ls |grep .mp3|egrep -v '(png|jpg)'|awk '{print $NF}')
# egrep removes pictures
# $NF gives the last column in the file name. This removes the spaces in the name.
do mv *$s $1$s
# This renames the files
cat *$s >> $1.mp3
# Cat combines the files
rm *$s
# Removes old files
done
ls -lh

In order to make the script work, copy it into the same directory the audio books files are located in. In the example below the script is called script-book and the ls command shows the script in the same directory as the audio book files.

man@earth> ls
The Hot Gate 001.mp3    The Hot Gate 021.mp3    The Hot Gate 041.mp3
The Hot Gate 002.mp3    The Hot Gate 022.mp3    The Hot Gate 042.mp3
The Hot Gate 003.mp3    The Hot Gate 023.mp3    The Hot Gate 043.mp3
The Hot Gate 004.mp3    The Hot Gate 024.mp3    The Hot Gate 044.mp3
The Hot Gate 005.mp3    The Hot Gate 025.mp3    The Hot Gate 045.mp3
The Hot Gate 006.mp3    The Hot Gate 026.mp3    The Hot Gate 046.mp3
The Hot Gate 007.mp3    The Hot Gate 027.mp3    The Hot Gate 047.mp3
The Hot Gate 008.mp3    The Hot Gate 028.mp3    The Hot Gate 048.mp3
The Hot Gate 009.mp3    The Hot Gate 029.mp3    The Hot Gate 049.mp3
The Hot Gate 010.mp3    The Hot Gate 030.mp3    The Hot Gate 050.mp3
The Hot Gate 011.mp3    The Hot Gate 031.mp3    The Hot Gate 051.mp3
The Hot Gate 012.mp3    The Hot Gate 032.mp3    The Hot Gate 052.mp3
The Hot Gate 013.mp3    The Hot Gate 033.mp3    The Hot Gate 053.mp3
The Hot Gate 014.mp3    The Hot Gate 034.mp3    The Hot Gate 054.mp3
The Hot Gate 015.mp3    The Hot Gate 035.mp3    The Hot Gate 055.mp3
The Hot Gate 016.mp3    The Hot Gate 036.mp3    The Hot Gate 056.mp3
The Hot Gate 017.mp3    The Hot Gate 037.mp3    The Hot Gate 057.mp3
The Hot Gate 018.mp3    The Hot Gate 038.mp3    The Hot Gate 058.mp3
The Hot Gate 019.mp3    The Hot Gate 039.mp3    The Hot Gate 059.mp3
The Hot Gate 020.mp3    The Hot Gate 040.mp3    script-book

Note- Make sure the script is executable before you run the command as shown below. Alternately you can also run the script by bash before the command if you don't know how to make the script executable. Example: bash ./script-book bookname

In the example below the I show how to execute the script and show example output. This shows that the script combined the files listed above and named the file TheHotGate and removed all the old unneeded files.

man@earth> ./script-book TheHotGate
total 447744
-rw-r--r--  1  arich   staff    219M  Feb 20 11:34    TheHotGate.mp3
-rw-r--r--  1  arich   staff    624B   Feb 20 11:33    script-book


I hope this helps anyone who is having a similar issue.


Links to places to get audio books.


LibriVox

             Audible