Skip to content

Instantly share code, notes, and snippets.

@oleasteo
Created January 8, 2024 00:22
Show Gist options
  • Select an option

  • Save oleasteo/18224566cab496d7ec91732be7ab6b00 to your computer and use it in GitHub Desktop.

Select an option

Save oleasteo/18224566cab496d7ec91732be7ab6b00 to your computer and use it in GitHub Desktop.
Split m4b files by chapter
#!/bin/bash
###
# Splits an m4b file by chapter with the `mkvmerge` tool. Then, the parts
# are converted back into m4b containers with `ffmpeg`.
#
# Usage: m4b-split-chapters [FILE]
###
FILE="$1"
[ -f "$FILE" ] || (echo 'Specify input file.'; exit 1)
DIR="$(dirname "$(realpath "$FILE")")"
tmpdir="$(mktemp -d)"
mkvmerge -o "$tmpdir/$(basename "$FILE" .m4b).mka" --split chapters:all "$FILE"
for f in "$tmpdir/"*.mka; do
ffmpeg -i "$f" -c:a copy -vn -f mp4 "$tmpdir/$(basename "$f" .mka).m4b"
done
mv "$tmpdir/"*.m4b "$DIR"
rm -rf "$tmpdir"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment