Skip to content

Instantly share code, notes, and snippets.

@bkw777
Last active April 8, 2023 23:35
Show Gist options
  • Save bkw777/86e9adca433ca94e5dd63f07e6aa81fc to your computer and use it in GitHub Desktop.
Save bkw777/86e9adca433ca94e5dd63f07e6aa81fc to your computer and use it in GitHub Desktop.
Merge downpour.com m4b files
#!/bin/bash
# Merges downpour.com m4b files into a single m4b file per book.
# Directions:
# download m4b files from downpour.com
# cd into the directory with "Book Title ...[File # of #].m4b
# run with no arguments
# Multiple books may all be downloaded into the same directory.
# Previously combined books may still exist in the same directory.
# The filenames from the web site do not need to be changed or cleaned up.
# (they may contain () [] " ' & ! etc)
# Extracts the cover art from "...[File 1 of #].m4b"
# Combines all m4b files and re-embeds the extracted cover image
# Outputs "Book Title.m4b"
# Optionally moves the source m4b files to orig/ or deletes them
# true = move original files to ./orig/
# false = delete original files
keep_src=false
abrt () { printf '%s: %s' "$0" "$@" >&2 ; exit 1 ; }
mp4art --version || abrt "Please install mp4art (https://github.com/enzo1982/mp4v2)"
m4b-tool --version || abrt "Please install m4b-tool (https://github.com/sandreas/m4b-tool)"
${keep_src} && { [[ -d orig ]] || mkdir orig || abrt "failed mkdir orig" ; }
for f in *File\ 1\ of\ *.m4b ;do
title=${f%(*}
title=${title% *}
echo "\"$title\""
# FIXME don't rely on globbing for sort order
# FIXME don't proceed if any parts missing
infiles=("${title}"\ \(*File\ *\ of\ *.m4b)
outfile="${title}.m4b"
[[ ${infiles[0]} =~ '[File 1 of 1].m4b' ]] && {
mv -v "${infiles[0]}" "${outfile}"
:
} || {
mp4art --extract "${infiles[0]}" || abrt "failed mp4art --extract"
x=("${title}"*.art\[0\].jpg)
cover=${x[0]}
[[ -s ${cover} ]] || {
cover=${title}.jpg
printf 'No cover art found in "%s".\nPlease place "%s" in the current directory.\n' "${infiles[0]}" "${cover}"
read -p "Then press Enter when ready: "
[[ -s ${cover} ]] || abrt "Missing \"${cover}\""
}
m4b-tool merge --verbose --no-conversion --cover="${cover}" --output-file="${outfile}" -- "${infiles[@]}" || exit 1
[[ -s ${outfile} ]] || abrt "Failed \"${outfile}\""
rm "${cover}"
${keep_src} && { mv -v "${infiles[@]}" orig ; : ; } || rm -v "${infiles[@]}"
}
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment