As a Kindle owner, I regularly have to convert epub files to mobi files. There’s an application called Kindlegen for that. Unfortunately, it doesn’t always work, because some epub files have settings that Kindlegen doesn’t understand, or the file ends up having a strange title.
With some knowledge of epubs you can fix these errors yourself. I’ll look at two problems I ran into:
- The author name or book title is not correct.
- The language isn’t set.
The nuts and bolts of epub files
Epub files are actually quite easy to edit, if you know how. That’s because an epub file is really a zip file with some HTML files and extra information in it. Any program that can open a zip can open an epub, for instance winzip and 7zip. You can also rename the file from name.epub to name.zip. Your OS should automatically recognize it.
Note, always make sure to backup any file before editing it. And be aware that there could be DRM (Digital Rights Management) embedded in your file, in that case editing the file might not work or make your file unreadable. That’s what the backup’s for.
When you open the file, you should see something like this:
Finding the meta data
The basic information on how an epub is organized is in the container.xml. Open the META-INF folder in the epub, and then use an editor to open the file container.xml in there. This file contains information about location the root meta-data file.
The container.xml should contain a line similar to this:
<rootfile full-path=”OPS/content.opf” media-type=”application/oebps-package+xml”/>
The quoted text behind the ‘full-path’ attribute is the name of the meta-data file, in this case it’s inside the OPS folder and called content.opf. Once you’ve found the meta data, you can change it to fit your needs.
Changing the author name
Annoying, isn’t it, if you add two epubs by the same writer to your Kindle, and they are in different places because one is filed under “Stellinga, Martin”, and the other under “Martin Stellinga”.
You can change that. Open the meta data file (content.opf) and find a line similar to this:
<dc:creator file-as=”Stellinga, Martin” role=”aut”>Martin Stellinga</dc:creator>
or
<dc:creator ops:role=”aut”>Martin Stellinga</dc:creator>
You could even just search for the author name. Change the name to the version you like. Note that the ‘file-as’ attribute is ignored by Kindle (at least, my Kindle).
The same can be done for the book title, which can be found under:
<dc:title>The Thousand Sails of Elysium</dc:title>
I often change titles for trilogies so they are listed together.
Make sure the changes you made are saved to the file and placed inside the zip file. If you changed the filename from name.epub to name.zip, change it back to name.epub.
Convert to mobi using Kindlegen, and voila.
Editing the language
As stated, Kindlegen conversion will fail if there is a problem with the language. If the language of the book is not set, or set to UND (undefined), Kindlegen will give an error (“Error(prcgen):E23006: Language not recognized in metadata. The dc:Language field is mandatory. Aborting.”) and stop conversion.
Open the meta data file (content.opf) and find a line similar to this:
<dc:language>UND</dc:language>
The “dc” part could be a different abbreviation, but that doesn’t matter, as long as the language part is there.
You need to change the language to a valid language code, for instance English:
<dc:language>en</dc:language>
If the entire line is missing, you need to add it between the <metadata> and the </metadata> texts, preferably exactly before the </metadata>.
Make sure the changes you made are saved to the file and placed inside the zip file. If you changed the filename from name.epub to name.zip, change it back to name.epub.
And now Kindlegen should be able to convert it fine.
Conclusion
Kindlegen isn’t perfect, but with some tweaking, you can make it do what you want. I hope this helps with that process.