leotaku / mobi

This Go package implements writing KF8-style formatted MOBI and AZW3 books.
https://pkg.go.dev/github.com/leotaku/mobi
MIT License
33 stars 4 forks source link

Kindle Previewer - Older version of kindlegen or a third party #3

Open celogeek opened 1 year ago

celogeek commented 1 year ago

I try the example, and then try to open it with kindle previewer.

I receive this message:

Kindle Previewer does not support this file, which has either been created using an older version of KindleGen or a third party application. We recommend using EPUB or DOCX format directly for previewing and publishing your book on Kindle.: test.azw3

Do you think it's possible to update the version of the generated awz3 ?

Side question, how can I use this, to convert an ePub to an awz3 ? any example ? (specially for image)

leotaku commented 1 year ago

Hi! I do not think this would be as easy as just updating a version number. The error message suggests that the Kindle previewer app only supports e-books generated by Kindlegen. Kindlegen has many undocumented quirks meaning fully replicating its output is hard.

I would suggest you follow the advice given by the error message and simply use EPUB as your format. Newer versions of Kindle software all support EPUB, and generating compatible EPUB files is much easier than generating AWZ or MOBI files ever was. When I created this library this was not the case, and the library remains useful for Kindle readers running older firmware. I have misunderstood how Kindle supports EPUB, apparently files are still just converted to AZW3.

As I've outlined, I nowadays would not recommend generating AZW/MOBI files from EPUB files. However if you still want to attempt this e.g. for targeting devices with older firmware, it should definitely be possible, as long as you also have a library for reading EPUB files.

  1. Extract all relevant metadata from the EPUB files and fill the slots of the Mobi builder struct with it.
  2. Extract the XML content from the EPUB and fill the correct Chapter structs with their content. To ensure good performance, you should also split long content into Chunks with reasonable length. XML markup will often be compatible betweem EPUB and MOBI, but some things, like including images, will have to be adapted.
  3. Add all images you want to include to the Images field of the Mobi struct. You can then reference them using the syntax <img src="kindle:embed:0001?mime=image/jpeg></img>" where the number is the index of the image in the Images array, starting at 1, encoded as base 32.
celogeek commented 1 year ago

Hi, thanks. I've got this https://github.com/celogeek/go-comic-converter.

It generate ePub from comics (cbz/cbr/pdf/zip/rar). I was thinking of creating the AWZ3 files along with the ePub, in a very fast way, without reducing the image size like kindlegen do.

kindlegen limited image size to 1920 width or height and I've got the kindle scribe which has a higher resolution. also using amazon website result in the same files, as they probably just run their tools remotely.

In my case, I won't have to even parse the ePub, as I build it in my tools. I can directly create the AWZ3 from the image.

The goal is to try to have the latest feature and not go back in a very old version.

Too bad it's not documented. The kindlegen contains hidden parameters (I do an hexdump on it), like "--preserve_img", but when I try to use it, it's no more valid. I guess they forget to remove part of the code that reference the old option.

leotaku commented 1 year ago

Yeah, the fact that MOBI/AZW3 is largely undocumented and so tied to kindlegen is very annoying. In any case, have you attempted opening a ebook generated using my library on your Kindle Scribe? I don't own a Scribe myself, but I test this library on my Kindle Oasis with very recent firmware, which can open the generated ebooks without any issue. Kindle Previewer seems to be much stricter when it comes to not allowing ebooks generated using third-party tools.

Also, the Kindle Scribe is a very recent Kindle model which, as far as I am aware, does support EPUB, right? Are there still defects with Kindle support for EPUB, or why do you want to generate AZW3 files? I have misunderstood how Kindle supports EPUB, apparently files are still just converted to AZW3.

celogeek commented 1 year ago

My tools will convert CBZ to ePub. A bit like KCC. Then I upload the ePub to sendtomykindle website, and receive the amazon version on my kindle. It works nice, except the limitation of the image resolution. This epub to kindle conversion will limit width/height to 1920. It is the case using the website or using kindlegen locally.

I wonder if that possible to unlock this limit by generating the image myself.

For now, I've create a Standard Resolution profile which limit the image size to 1920 (width/height), and the kindlegen conversion (or online one), is very fast, as it don't need to reduce the image size.

If it's possible to write directly a AWZ3 files, with high resolution image, and sideload it to the kindle, that would be wonderful. I don't know if that will change anything on the result we see, it depends if the kindle will handle that, if the generated file will allow that ...

I will try with few image and a simple layout first, just to check that's working, then will go futher if it's ok.

leotaku commented 1 year ago

If you want to test out a simple e-book with images I would recommend generating a simple manga e-book using my tool at https://github.com/leotaku/kojirou. That way you can confirm everything works before having to write your own code.

Also, I apologize for the earlier confusion I thought Kindle readers now natively supported EPUB. I was not aware that you still had to do the conversion, which, as you have pointed out, has some shortcomings.

celogeek commented 1 year ago

Wow. thanks ! yes, indeed I will test that. Also, if that's working fine with higher resolution, I will integrate the logic to my tools using my css and images ;) I will also reference this tools in my readme ;) and the code.

celogeek commented 1 year ago

I've test it using the local disk. The image is converted from my png to JPG, with original resolution (1920x3031), and in color. I guess you are using the default quality.

It works, I can copy the file on device.

I wonder what's the size limit I can have here.

Also the file is a Book instead of a Document. I use this to distinguish what I buy vs what I put myself. I see we can set this.

In any case, I will have to take a look carefully to see if I can spot quality difference between the one from sendtokindle and this one. Even on scribe.

leotaku commented 1 year ago

Yeah, the library reencodes images to JPEG for compatibility reasons. Currently the default encoding quality is always used.

You should be able to have your e-books displayed as documents by setting. DocType to "PDOC".

axu2 commented 1 year ago

Wait, is this a kindlegen replacement?

leotaku commented 1 year ago

@axu2 Not directly, no. It is a library that can output Kindle-compatible ebooks. So you could use it to implement a tool similar to kindlegen.

axu2 commented 1 year ago

In that case is there a python equivalent library anywhere?

leotaku commented 1 year ago

@axu2 The calibre source contains python code for generating Kindle ebooks. You could use that as a library.