Post

Calibre 修复 EPUB 的乱序图片

漫画平台提供的 EPUB 格式文件,由于部分内容维护者操作的不严谨,很多漫画的图片文件名是乱序的。例如:第 1 页的图片起名为 008.jpg,第 2 页的图片起名为 003.jpg…,等等。如果用 EPUB 阅读器观看,不会感受到次序的问题。但若用漫画阅读器观看(为了获得满屏显示的最佳阅读体验),页面将会是乱序的。本文将会分析这个问题的原因,并探讨如何去解决。

两类阅读器加载内容的区别

为了方便理解问题,解压一个乱序 EPUB 文件,可以观察到部分的目录结构是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
.
├── html
│   ├── 1.html
│   ├── 2.html
│   ├── 3.html
│   ├── ...
|
├── image
│   ├── vol-003890.jpg
│   ├── vol-017993.jpg
│   ├── vol-028358.jpg
│   ├── ...
│
...

打开第 1 页的文件 1.html,内容是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html SYSTEM "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>第1页</title>
  <link rel="stylesheet" type="text/css" href="../css/style.css" />
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
  <div class="fs">
    <div>
      <img src="../image/vol-491489.jpg" alt="Comic Book Images" class="singlePage" />
    </div>
  </div>
</body>
</html>

在第 11 行的 <img> 定义了该页应该读取图片地址 ../image/vol-491489.jpg

EPUB 阅读器之所以能够获得正常的阅读顺序,是因为它是根据 HTML 文件来确定每页的图片,所以图片名乱序也不会影响 EPUB 阅读器。而漫画阅读器则是从文件夹 image 里按照图片命名排序来加载内容的,乱序之源就在于此。

修复思路

观察完 HTML 文件内容,不难找到解决的思路:根据每个 HTML 文件的次序来确定每个 JPG 文件的命名。例如:

  1. html/1.html 指向的图片 image/vol-491489.jpg,重命名为 image/001.jpg
  2. 修改 html/1.html<img>src 属性,即:<img src="../image/001.jpg" ... />

实施细节

使用 Calibre 可以编辑 EPUB 文件里面的内容,实现上述思路。

下列截图是 macOS 平台的 Calibre,版本号 6.11.0。不同平台或版本号的界面可能会有差异。

  1. Calibre 打开 EPUB,点击 Edit book 打开内嵌的 APP ebook-edit

  2. 滚动 File browser > Images > 选中图片文件(注意略过封面页 cover.jpg)> 右键菜单 > Bulk rename the selected files

    bulk rename

  3. 下一步弹出的窗口中勾选 Rename files according their book order :

    rename items

  4. 自动重命名完成后,<num>.html 文件内对应的图片路径也会自动被修改。

  5. 关闭 ebook-edit 返回到 Calibre 界面,点击 Save to disk 就完工了。

其他

Calibre 也支持对 HTML 文件的批量重命名,操作方式和 Image 文件类似。当遇到某些连 HTML 文件命名也是乱序的 EPUB 文件时,可以一并对 HTML 批量重命名。

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.