Skip to content

Instantly share code, notes, and snippets.

@aklinker1
Last active September 12, 2022 02:20
Show Gist options
  • Save aklinker1/bb7f75841dd97c93f0c18be640479460 to your computer and use it in GitHub Desktop.
Save aklinker1/bb7f75841dd97c93f0c18be640479460 to your computer and use it in GitHub Desktop.
Script to fix EPUBs when uploading to Google Play Books and you get the "Processing Failed" error
#!/bin/bash
# Usage:
#
# bash play-books-epub-fixer.sh "./path/to/book.epub"
#
# This will rename the original epub to "./path/to/book_original.epub". You must have calibre installed for this to work:
#
# apt get calibre
#
# The EPUB is fixed in 3 steps:
#
# 1. Convert the EPUB to the MOBI format
# 2. Convert the MOBI back to an EPUB
# 3. Upgrade to EPUB version to 3
#
# WARNING: I read somewhere that the MOBI file format is more limitted in features than an EPUB. So when we convert to
# WARNING: and from the format, we lose some things. Images become lower quality and some other various things. I'm not
# WARNING: an expert, google the difference. That said, I care about listening to EPUBs, and this does the job with no
# WARNING: issues.
EPUB_PATH="$1" # ./path/to/book.epub
MOBI_PATH="${EPUB_PATH/.epub/.mobi}" # ./path/to/book.mobi
EPUB_CONVERTED="${EPUB_PATH/.epub/_converted.epub}" # ./path/to/book_converted.epub
FINAL_PATH="${EPUB_PATH/.epub/_playbooks.epub}" # ./path/to/book_playbooks.epub
function step {
echo -e "\x1b[1m\x1b[94m━━━━━━━━━━\x1b[0m"
echo -e "$1"
}
set -e
step "Converting \x1b[96m\x1b[1m$EPUB_PATH\x1b[0m to \x1b[96m\x1b[1m$MOBI_PATH\x1b[0m..."
ebook-convert "$EPUB_PATH" "$MOBI_PATH"
step "Converting \x1b[96m\x1b[1m$MOBI_PATH\x1b[0m back to \x1b[96m\x1b[1m$EPUB_CONVERTED\x1b[0m..."
ebook-convert "$MOBI_PATH" "$EPUB_CONVERTED"
step "Upgrading \x1b[96m\x1b[1m$EPUB_CONVERTED\x1b[0m to v3 if possible..."
ebook-polish -U "$EPUB_CONVERTED" "$FINAL_PATH"
step "Removing temp files..."
rm "$MOBI_PATH" "$EPUB_CONVERTED"
echo -e "\nFinal output: \x1b[96m\x1b[1m$FINAL_PATH\x1b[0m\n"
@aklinker1
Copy link
Author

Here's what the output looks like:

Example output

TBH still not sure what was wrong with the original EPUB, maybe the toc.ncx missing file was the problem? 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment