星期四, 三月 13, 2014

Split an m4b file into chapters

An audio book in m4b format usually contains meta information and is usually very large. It is not suitable to be played in a car audio system. To split it:

#!/usr/bin/env bash
# This script need below software to be installed
#  faad2 lame ffmpeg sox
# e.g., sudo yum install blah blah

if [ $# -ne 1 ]
then
    echo
    echo Usage: $0 file.m4b
    echo
    exit 1
fi
info=`mktemp -u -p .`
segs=`mktemp -u -p .`
twav=`mktemp -u -p .`.wav
faad "$1" -o $twav

ffprobe "$1" 2>$info

grep end $info | \
    sed 's/,//' | \
    gawk '{print $4, $6-$4}' \
     > $segs

nch=`wc -l $segs | gawk '{print $1}'`

for i in `seq -w $nch`
do
    echo
    echo
    echo Dealing with Chapter $i
    echo
    sox "$twav" $i.wav --show-progress trim `head -$i $segs | tail -1`
    lame $i.wav $i.mp3
    rm $i.wav
done
rm $info $segs $twav
Save this code in, e.g., m4bsplt.sh
chmod u+x m4bsplt.sh
pathto/m4bsplit.sh file.m4b
You can then see the mp3 files created.

没有评论: