Skip to content

Instantly share code, notes, and snippets.

@fortinmike
Created March 5, 2019 14:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fortinmike/ba92c87ba3b639bafad22bf460b6f662 to your computer and use it in GitHub Desktop.
Save fortinmike/ba92c87ba3b639bafad22bf460b6f662 to your computer and use it in GitHub Desktop.
Convert EPUB files and packages to MOBI using the Calibre CLI (the app must be installed)
#!/bin/bash
DIRNAME=$(dirname -- "$1")
BASENAME=$(basename -- "$1")
if [ ! -d "$1" ]; then
# The EPUB is already a file, convert straight away
/Applications/calibre.app/Contents/MacOS/ebook-convert "$1" "$DIRNAME/$BASENAME.mobi"
else
# The EPUB is a directory, convert it to a file before conversion
TEMPDIR=$(mktemp -d -t "ConvertToMobi")
EPUBFILE="$TEMPDIR/$BASENAME"
# Convert epub directory (package) to an epub file
# by zipping the package's contents with the epub extension
$(cd "$1" && zip -r -X "$EPUBFILE" *)
# Convert the epub file to MOBI
/Applications/calibre.app/Contents/MacOS/ebook-convert "$EPUBFILE" "$DIRNAME/$BASENAME.mobi"
rm -rf $TEMPDIR
fi
@fortinmike
Copy link
Author

fortinmike commented Mar 5, 2019

calibre.app must be installed in /Applications for the script to work.

Manual Usage

./convert-epub-to-mobi-with-calibre.sh MyBook.epub

With Hazel

A nice way to run the script is using Hazel. Just create a new rule like below for your epub-containing folder and paste the script in the "Edit Script" box of the "Run Shell Script" step.

This way, any epub file added to the target folder will automatically get a .mobi file beside the original .epub. Conversion takes about 20 seconds per book on my 2012 rMBP.

screen shot 2019-03-05 at 9 11 43 am

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