USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Convert your mp3 audiobooks to m4b(comments/suggestions plz)

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
Lord.DragonFly.of.Dawn
Advanced Member


Joined: 18 Jul 2024
Posts: 607
Location: South Portland, Maine, USA, Earth, Sol System

PostPosted: Sat Sep 06, 2024 8:14 pm    Post subject: Convert your mp3 audiobooks to m4b(comments/suggestions plz) Reply with quote

It works, but has some very significant parts that i am not at all happy with.

Namely: spaces in filenames and the not insignificant (10-30GB) free space requirement.

If anyone has any ideas as to how to fix these i'd love to know.

Code:
#!/bin/bash

# Filename:makem4b.sh
# Author:Lord.DragonFly.of.Dawn
# Date: 2024.09.06

# Define a few basic directories
# If this guy exists process files inside into one giant m4b
BUILD=".build";
# Backup files into this directory when done
BACKUP=".source";
# Temporary stuffgors here
TEMP=".tmp";

# Define a few temorary files relative to the temp dir
T_WAV="${TEMP}/temp.wav";
T_M4A="${TEMP}/temp.m4a";

# Define the normalize flag. If present try to normalize audio
F_NORMALIZE=".normalize";

# Detect the presence of certain required apps.
# fail now if they aren't present

# Detect mplayer:
if ! which mplayer > /dev/null 2>&1; then
    echo "FATAL ERROR: mplayer binary not detected.";
    echo " mplayer 1.0 or higher required for decoding";
    exit 1;
else
    P_MPLAYER="$(which mplayer)";
fi

# Detect faac:
if ! which faac > /dev/null 2>&1; then
    echo "FATAL ERROR: faac binary not detected.";
    echo "faac 1.26 or higher required for encoding.";
else
    P_FAAC="$(which faac) -b 48";
fi

# Detect awk:
if ! which awk > /dev/null 2>&1; then
    echo "FATAL ERROR: awk binary not detected.":
    echo "  awk 3.0 or higher required for processing.";
else
    P_AWK="$(which awk)";
fi

# Detect normalize:
if ! which normalize > /dev/null 2>&1; then
    echo "ERROR: normalize binary not detected.";
    echo "  Ignoring any normalize flags";
    D_NORMALIZE="";
else
    D_NORMALIZE="true"
    P_NORMALIZE="$(which normalize)";
fi

# Detect rename
if ! which rename > /dev/null 2>&1; then
    echo "FATAL ERROR: rename binary not detected. ";
    echo "  util-linux-ng 2.13 or higher required!";
    exit 1;
else
    P_RENAME="$(which rename)";
fi

# Detect tree
if ! which tree > /dev/null 2>&1; then
    echo "FATAL ERROR: tree binary not detected. ";
    echo "  tree 1.5 or higher required!";
    exit 1;
else
    P_TREE="$(which tree)";
fi

# Detect grep
if ! which egrep > /dev/null 2>&1; then
    echo "FATAL ERROR: egrep binary not detected. ";
    echo "  grep 2.2 or higher required!";
    exit 1;
else
    P_EGREP="$(which egrep)";
fi

# BEGIN MAIN LOOP!
for AUTHOR_DIR in $(ls -v); do
    # Only process ${AUTHOR_DIR} if it really is a dir
    if [ -d "${AUTHOR_DIR}" ]; then
   # Extract author name for flags
   AUTHOR="$(echo ${AUTHOR_DIR}| tr _ ' ')";
   
   # Output progress info
   echo "Now Processing ${AUTHOR}";
   cd "${AUTHOR_DIR}"
   # BEGIN SECONDARY LOOP
   for TITLE_DIR in $(ls -v); do
       # Only process if build/rebuild is required
       if [ -d "${TITLE_DIR}/${BUILD}" ]; then
      cd "${TITLE_DIR}";
      # Extract more meta info
      ORDER="$(echo ${TITLE_DIR}|awk -F _-_ '{ print $2 }'|tr _ ' ')";
      BOOK="$(echo ${TITLE_DIR}|awk -F _-_ '{ print $1 }'|tr _ ' ')";
      TITLE="$(echo ${TITLE_DIR}|awk -F _-_ '{ print $3 }'|tr _ ' ')";
      if [ -z "${TITLE}" ]; then
          TITLE="${BOOK}";
          TARGET="$(echo ${ORDER} - ${TITLE}.m4b|tr ' ' _)";
      else
          TARGET="$(echo ${ORDER} - ${BOOK} - ${TITLE}.m4b|tr ' ' _)";
      fi;
      # Output more progress info
      echo "  Now Processing ${BOOK} (${ORDER})";
      
      # Rename files to remove spaces
      # TODO - make the rest of the script handle spaces
      #   gracefully so this rename is not needed.
      cd ${BUILD};
      ls | while read i; do
               mv "${i}" "$(echo ${i}|tr ' ' _)" 2> /dev/null;
           done;
                cd ..;

      # Give us us tempdir!
      mkdir -p ${TEMP};

      # Normalize routine. (not tested yet)
      # TODO - test this subroutine
      if [ ${D_NORMALIZE} -a -e ${F_NORMALIZE} ]; then
          echo "    Normalizing input";
          ${P_NORMALIZE} ${BUILD} >/dev/null 2>&1;
          rm ${F_NORMALIZE}
      fi;

      # remove leftovers from previous run(s)
      if [ -e ${T_WAV} ]; then
          rm ${T_WAV};
      fi
      
      # Collect filenames
      T_FILES="$(${P_TREE} -if ${BUILD}| ${P_EGREP} 'mp3$')";

      # Counters
      T_FILES_TOTAL="$(${P_TREE} -if ${BUILD}| ${P_EGREP} 'mp3$' | wc -l)";
      T_FILES_NUM="0";

      # Begin decoding files.
      # WARNING! This takes up between 10 and 20 Gig of HD space!
      # URGENT - fix this to use a named pipe to reduce the disk
      #   activity and remove the free-space requirement
      echo "    Decoding now..."
      for FILE in $T_FILES; do
          # Progress bar
          echo -ne "\r      ${T_FILES_NUM} of ${T_FILES_TOTAL} Completed";
          T_FILE="${TEMP}/${T_FILES_NUM}.wav";
          $P_MPLAYER -ao pcm:fast:file=${T_FILE} -vo null -vc null ${FILE} > /dev/null 2>&1 ;
          cat ${T_FILE} >> ${T_WAV};
          rm ${T_FILE};
          T_FILES_NUM=$(expr ${T_FILES_NUM} + 1);
      done;
      echo -e "\r      ${T_FILES_NUM} of ${T_FILES_TOTAL} Completed";

      # Encode files
      # TODO - Add support for embedding cover art
      echo "    Encoding now... (You might want to get some tea)";
      ${P_FAAC} --artist "${ARTIST}" \
          --writer "${ARTIST}" \
          --title "${TITLE}" \
          --genre Audiobook \
          --album "${BOOK}" \
          --track 1 \
          --disc "${ORDER}" \
          $COVER \
          -o "${T_M4A}" ${T_WAV};
      mv "${T_M4A}" "${TARGET}";
      
      # Do cleanup.
      mv ${BUILD} ${BACKUP};
      rm -r ${TEMP};
      cd ..;
       fi;
   done;
   cd ..;
    fi;
done;



_________________
ArchLinux x86_64 - Custom Built Desktop
ArchLinux x86_64 - Compaq CQ50 Laptop
ArchLinux i686 - Acer Aspire One Netbook
ArchLinux i686 - Dell Presario ze2000 (w/ shattered LCD)

PuppyLinux, CloneZilla, PartedMagic, DBAN - rescue thumbdrives
Windows 7 (x86_64 desktop alternate boot)
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2024-2009 - Usa Linux Users Group
This forum is powered by phpBB. © 2024-2009 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse