Monday, April 9, 2012

Unpacking a Kindle/Mobi file

Converting an ebook of whatever format to a Kindle/mobi file adds a level of abstraction that always leaves me with this gnawing uncertainty: did anything change under there? Is this what I really want? This may be due to general OCD, but I think it started with Microsoft Word's early attempts to guess what I wanted and automatically change my text for me, causing all manner of consternation and mistrust, as its guesses were often incorrect. Course it doesn't help that Kindle devices don't all display an ebook the same way.

As you might've guessed, I don't trust Kindle Previewer (or the KindleGen that it invokes) either... who knows what it does during conversion!? Indeed, when a feature in an ebook I'm creating doesn't come out right, knowing exactly what KindleGen is doing with my files can be very helpful for cross-platform development.

Thankfully, there are a bunch of folks over at MobileRead who have developed an open source tool for unpacking Mobi files and looking at their innards. It's called Mobi Unpack and the latest version (047) can be found there.

But what do you do with it once you've found it? Double-clicking didn't work at first for me:

doubleclick MU

And I had no idea what application I should associate it with. I used a rather convoluted system starting with Terminal, navigating to the MU folder, then typing python Mobi_Unpack_v047.pyw, which works, but it seemed like there should be a better way.

One of the Mobi Unpack developers told me I needed a thing called Python Launcher. You can find it in /System/Library/Frameworks/Python.framework/versions/2.5/Resources. It comes with your Mac. Who knew? :)

I dragged Mobi_Unpack_v047.pyw on top of the Python Launcher, and now it's permanently associated: when I double-click, it opens right up.

Mobi ebook Unpack Tool

Once Mobi Unpack is open, use the top field and Browse button to choose the Mobi file that you want to unpack. Use the second field and Browse button to choose the folder you want to unpack the Mobi file into.

I don't check any of the other options.

Then click Start to unpack your Mobi file.

Mobi Unpack creates a mobi8 folder, which presumably is the KF8 version as well as a mobi7 folder which presumably is what old Kindles get. You can explore the files inside (XHTML, CSS, content.opf, etc.) to see how KindleGen (via Kindle Previewer or whatever) is creating your Mobi file.

Unpacked Mobi

The mobi8 folder even contains an EPUB file. I thought this might be useful if you were starting with a Mobi file and wanted to convert to EPUB, but unfortunately, at least in my testing (in which I did not generate the Mobi file from an EPUB file), the EPUB did not validate.

13 comments:

  1. What a useful post! THANKS for the link to the unpacking tool...
    ...and for the detailed directions on how to use it.

    ReplyDelete
  2. I thought that one used to be able to unpack mobi files by unzipping them: just change the extension to .zip and use an unzip utility. Is the latest mobi format different?

    ReplyDelete
  3. But no abilty to edit and repack, right? Or can you feed the edited mobi or kf8 files back into Kindle Previewer?

    ReplyDelete
  4. Liz - you make me tired. How do you have the time to know so much about this thing? Are there really pigs in your life, too? When I read you, I feel this obligation to pay as much attention as you do and learn useful stuff and share it, but I know I'm not going to EVER know as much as you know about this. I write - but I write stories. Or cute blogs. Not informational things that are clear and helpful. Like you do. And right now, fighting a wearying cold, I feel like going back to bed and just quitting the twenty first century altogether. The learning curve is TOO STEEP. I'm trying to learn inDesign, e-book production, print-on-demand work flow - and this is just a side line. There are not enough days in a day. So thank you for teaching me this stuff.

    Once, I asked the same question that Opal has asked. But I know better now. I'm assuming she found out that it's epub that can be unzipped, not the .mobi.

    And I wonder along with Rick - unpacking does not indicate edit-ability and repacking, jah? Of course not.

    ReplyDelete
    Replies
    1. Thanks, I really needed that. I've been feeling like I don't get anything done... no books yet published this year!! Lots of research, lots of testing, but at some point, it's time to fish and cut bait, or however that goes. I appreciate the kind words. (And no pigs right now, just a couple of forlorn semi-abandoned sheep and some lovely chickens.) There is so so much to do. I find it wearying too. Ànims! (And I'm still working on the repacking end of the equation. Will report back.)

      Delete
  5. Anyone know how to repack the files back into one mobi after you've tinkered with the code?

    ReplyDelete
  6. Gosh, spent all day trying to work this out and I should have come here first! thank you so much. ana ;)

    ReplyDelete
  7. I can't find the Python Launcher on my Mac. I have system 10.5.8.

    ReplyDelete
  8. Python launcher is part of the python install, available here:
    http://www.python.org/download/

    But there's an applescript launcher, too. Drag and drop your mobi onto the script icon.
    http://www.mobileread.com/forums/showpost.php?p=774836&postcount=5

    ReplyDelete
  9. Wondering, is there a method to repack the directory so that it functions in kindle?

    ReplyDelete
  10. Does this method work on PRC files as well?

    ReplyDelete
  11. To repack, dragging and dropping the opf file from the unpacked mobi onto Kindle Previewer did it for me. That repackaged the whole folder containing all the mobi parts. (I use Mac OS).

    ReplyDelete
  12. Thanks Liz,
    this works beautifully on my Linux system too.
    Just one slightly worrying thing, I found an extra meta tag inserted into the .ncx files (both mobi7 & mobi8)
    meta content="mobiunpack.py" name="dtb:generator"/
    Maybe the programmer is just adding his little stamp, but it makes me wonder what else mobiunpack might be adding/changing?

    ReplyDelete

More of my books