#!/usr/bin/env bash set -e OUTPUT="audiobook.m4b" FILES="files.txt" CHAPTERS="chapters.txt" # list of MP3 files in filename order find . -maxdepth 1 -name '*.mp3' -print0 | sort -z | while IFS= read -r -d '' file; do file="${file#./}" printf "file '%s'\n" "$file" done > "$FILES" # start chapter metadata file { echo ";FFMETADATA1" } > "$CHAPTERS" START=0 while IFS= read -r line; do FILE="${line#file \'}" FILE="${FILE%\'}" # duration in milliseconds DURATION=$(ffprobe \ -v error \ -show_entries format=duration \ -of csv=p=0 \ "$FILE") DURATION_MS=$(awk "BEGIN {printf \"%.0f\", $DURATION * 1000}") END=$((START + DURATION_MS)) # filename as the chapter title TITLE="${FILE%.mp3}" cat >> "$CHAPTERS" <