Skip to content

Instantly share code, notes, and snippets.

@nimatrueway
Forked from aularon/m4b_split.sh
Last active July 27, 2023 07:45
Show Gist options
  • Save nimatrueway/6ec71bb514e94c3ecbff4e2103b9bb76 to your computer and use it in GitHub Desktop.
Save nimatrueway/6ec71bb514e94c3ecbff4e2103b9bb76 to your computer and use it in GitHub Desktop.
Split an m4b into its chapters. No recoding is done, just splitting
#!/bin/bash
# Description: Split an m4b into its chapters. No recoding is done, just splitting
# Usage: m4b_split.sh $input_file $output_dir/
# Requires: ffmpeg, jq
# Author: Hasan Arous, Nima Taheri
# License: MIT
in="$1"
out="$2"
splits=""
ffprobe -i "$in" -print_format json -show_chapters > chapters.json
printf "\n\n\n\n"
jq -r '.chapters[] | .start_time + " " + .end_time + " " + (.tags.title | gsub("[/\\|?*<>:\" ]+"; "_"))' chapters.json
printf "\n\nEdit chapters \"$(pwd)/chapters.json\" if you like before proceeding.. [press Enter to continue]\n\n\n\n"
read
while read start end title; do
splits="$splits -c copy -map_chapters -1 -ss $start -to $end $out/$title.m4b"
done <<<$(cat chapters.json \
| jq -r '.chapters[] | .start_time + " " + .end_time + " " + (.tags.title | gsub("[/\\|?*<>:\" ]+"; "_"))')
ffmpeg -i "$in" $splits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment