2019-09-07 19:15:54 +00:00
|
|
|
#!/bin/bash
|
|
|
|
IFS=$'\n\t'
|
|
|
|
|
|
|
|
# Author: http://crunchbang.org/forums/viewtopic.php?id=38748#p414992
|
|
|
|
# m4bronto
|
|
|
|
# https://gist.github.com/dcondrey/469e2850e7f88ac198e8c3ff111bda7c
|
|
|
|
# Modified to support a chapters.txt file with sequential list of chapter names
|
|
|
|
# Run as split-audio-by-chapters file.m4a [chapters.txt]
|
|
|
|
# Output files are sequential and generated
|
|
|
|
|
|
|
|
|
|
|
|
# https://stackoverflow.com/a/3352015
|
|
|
|
trim() {
|
|
|
|
local var="$*"
|
|
|
|
# remove leading whitespace characters
|
|
|
|
var="${var#"${var%%[![:space:]]*}"}"
|
|
|
|
# remove trailing whitespace characters
|
2020-05-02 13:38:46 +00:00
|
|
|
var="${var%"${var##*[![:space:]]}"}"
|
2019-09-07 19:15:54 +00:00
|
|
|
echo -n "$var"
|
|
|
|
}
|
|
|
|
|
|
|
|
CHAPTER_INDEX=0
|
|
|
|
|
|
|
|
if [ -f "$2" ]; then
|
2020-02-03 08:47:20 +00:00
|
|
|
CHAPTERS_FILE_PASSED=1
|
2019-09-07 19:15:54 +00:00
|
|
|
echo "Reading chapters from $2"
|
|
|
|
readarray CHAPTERS < "$2"
|
|
|
|
IFS=$'\n\r'
|
|
|
|
IFS=$'\n\t'
|
2020-02-03 08:47:20 +00:00
|
|
|
else
|
|
|
|
CHAPTERS_FILE_PASSED=0
|
2019-09-07 19:15:54 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
ffmpeg -i "$1" 2> /tmp/ffmpeg.txt
|
|
|
|
|
|
|
|
while read -r first _ _ start _ end; do
|
|
|
|
unset IFS
|
|
|
|
if [[ "$first" = "Chapter" ]]; then
|
2020-05-02 13:38:46 +00:00
|
|
|
read -r
|
|
|
|
read -r _ _ CHAPTER_TITLE
|
2019-09-07 19:15:54 +00:00
|
|
|
|
2020-02-03 08:47:20 +00:00
|
|
|
REAL_CHAPTER_INDEX=$((CHAPTER_INDEX+1))
|
|
|
|
CHAPTER_INDEX_PADDED=$(printf "%03d" "${REAL_CHAPTER_INDEX}")
|
2019-09-07 19:15:54 +00:00
|
|
|
|
|
|
|
# If we are using a chapters.txt that is valid
|
2020-02-03 08:47:20 +00:00
|
|
|
if ((CHAPTERS_FILE_PASSED=="1")) ; then
|
2019-09-07 19:15:54 +00:00
|
|
|
CHAPTER_TITLE=${CHAPTERS[$CHAPTER_INDEX]}
|
|
|
|
fi
|
|
|
|
|
|
|
|
FILENAME="${CHAPTER_INDEX_PADDED} - $(trim "$CHAPTER_TITLE").m4a"
|
|
|
|
|
2020-05-01 03:17:49 +00:00
|
|
|
# -vsync 2 = Frames are passed through with their timestamp or dropped so as to prevent 2 frames from having the same timestamp.
|
|
|
|
# -sn = no subtitles
|
|
|
|
# -dn = no data stream
|
|
|
|
# -vn = no video stream
|
|
|
|
# -metadata title= put in a chapter title
|
|
|
|
# -acodec copy = copy the audio stream
|
|
|
|
ffmpeg -hide_banner -loglevel panic \
|
|
|
|
-vsync 2 -sn -dn -vn -i "$1" -ss "${start%?}" -to "$end" -metadata title="$CHAPTER_TITLE" -dn -vn -sn -acodec copy "$FILENAME" </dev/null
|
2019-09-07 19:15:54 +00:00
|
|
|
|
|
|
|
((CHAPTER_INDEX++))
|
|
|
|
fi
|
2020-05-02 13:38:46 +00:00
|
|
|
done </tmp/ffmpeg.txt
|