Thursday, January 02, 2025

Bulk convert MOBI to ePub (or other eBook formats)

I've just switched from a Kindle to a Kobo book reader. I have a collection of old MOBI format books that I wanted to bring over the the Kobo. It does read MOBI books but I see some weird erros like this:


I've found that converting to ePub in Calibre avoids these problems but I have a folder full of files to convert. Calibre has a command line tool called ebook-convert that can convert a file and seems to work well. Based on past work by others I've created this script to convert a directory of ebooks over to ePub in a directory named "converted".

#!/bin/bash

# bulk-convert

# based on https://github.com/captaincouch/lame-bulk-convert

# Set the $SAVEIFS variable as the global Internal Field Separator character.

# This preserves the original global $IFS variable to change it back later.

SAVEIFS=$IFS

# Set the $IFS variable to a space character using substitution escape sequence 

# for the space character.

IFS=$(echo -en "\n\b")

# Create a new directory for the newly-converted files, if not already existing.

mkdir -p ./converted

# Loop and convert each file "i" to mp3. Quotation marks are used to account for

# spaces and illegal characters. The files are placed in ./converted-mp3/filename.mp3.

for i in $(ls)

do

# Set a variable "c" containing the filename with the ".ext" extension removed.

# For example, "sound.wav" would become "sound", and "sound.foo.wav" would

# become "sound.foo".


c="${i%.*}"

ebook-convert "$i" "./converted/$c.epub"

done

# Set the $IFS variable back to previous value, using the value stored in $SAVEIFS

# for safekeeping.

IFS=$SAVEIFS



No comments: