Created
March 1, 2023 04:12
-
-
Save Sonictherocketman/37487645fd9f321969a0a24f66d1d439 to your computer and use it in GitHub Desktop.
A script to turn large text documents into audiobooks with chapters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env zsh | |
# Given a text file, split it into chapters, then make audio for each | |
# chapter using a set of presets. Then join the audio together | |
# preserving each chapter in the metadata. | |
# This assumes mp4.sh is in /usr/local/bin | |
# https://gist.github.com/djotto/fca0b0e57c8d008575eb7f3648260910 | |
SOURCE_DIR="$(pwd)" | |
WORK_DIR="$(mktemp -d)" | |
MP4_SH="$(which mp4.sh)" | |
INPUT="${1:-/dev/stdin}" | |
OUTPUT="book.mp4" | |
SAY_ARGS=(-v Daniel) | |
CHAPTER_SPLIT="ˇ" | |
TEXT_CHAPTER_PREFIX="Chapter_" | |
log() { | |
echo "[$(date)] $@" | |
} | |
cleanup() { | |
log "Cleaning up..." | |
rm -r $WORK_DIR; | |
} | |
main() { | |
log "Using workspace: $WORK_DIR..." | |
cd $WORK_DIR; | |
cat $SOURCE_DIR/$INPUT | sed "s/\($CHAPTER_SPLIT\)/\n\1\n/g" > fulltext.txt | |
log "Splitting chapters..." | |
gcsplit --digits 2 --quiet \ | |
--prefix=$TEXT_CHAPTER_PREFIX fulltext.txt \ | |
"/$CHAPTER_SPLIT/+1" "{*}" | |
log "Sanitizing..." | |
for i in $TEXT_CHAPTER_PREFIX*; do | |
sed -i '' -e '$ d' $i; | |
done | |
CHAPTER_COUNT="$(ls $TEXT_CHAPTER_PREFIX* |wc -l)" | |
log "Found $CHAPTER_COUNT chapters." | |
ls $TEXT_CHAPTER_PREFIX* | while read CHAPTER; do | |
N=$(basename $CHAPTER) | |
NAME=${N%.*} | |
SAYFILE="${NAME}.aiff" | |
log "Generating $NAME..."; | |
say $SAY_ARGS -o $SAYFILE -f $CHAPTER; | |
done; | |
log "Converting..." | |
for i in *.aiff; do ffmpeg -i "$i" "${i%.*}.mp4"; done | |
log "Merging..." | |
$MP4_SH merge_as_chapter $TEXT_CHAPTER_PREFIX*.mp4 combined.mp4 | |
mv combined.mp4 $SOURCE_DIR/$OUTPUT | |
} | |
#trap SIGINT | |
main | |
cleanup | |
log "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment