Last active
January 28, 2024 05:14
-
-
Save senventise/cde9e87dbd6bc2a538879bc4b6e414e9 to your computer and use it in GitHub Desktop.
将 kepub 转为 cbz 格式。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/fish | |
argparse --name mox2cbz 'w/webp' -- $argv | |
or return | |
# input filename | |
set fn $argv | |
# output filename | |
set out_fn (basename $fn ".epub")".cbz" | |
set idx 0 | |
set tmp_files | |
# check output path | |
if test -e $out_fn | |
echo "\"$out_fn\" already exists." | |
exit 1 | |
end | |
# find pages from toc | |
for page in (unzip -p $fn "vol.opf" | string match -gr "href=\"(html/\S+html)\"") | |
set idx (math $idx + 1) | |
# find picture name and suffix | |
set temp (unzip -p $fn $page | string match -gr "\"\.\./(\S+(jpe?g|png))\"") | |
# picture path in zip | |
set img_path $temp[1] | |
# extracted filename | |
set img_name $out_fn"_MOX"(string pad --width 5 --char "0" $idx)"."$temp[2] | |
# continue if there is no picture | |
if test $status -eq 1 | |
continue | |
end | |
echo -e "Page: $page\nFile: $img_path => $img_name (index = $idx)\n" | |
# extract picture | |
unzip -p $fn $img_path > /tmp/$img_name | |
set tmp_files $tmp_files /tmp/$img_name | |
if set -q _flag_webp | |
cwebp -short /tmp/$img_name -o /tmp/$img_name".webp" | |
zip -q -u $out_fn /tmp/$img_name".webp" | |
else | |
zip -q -u $out_fn /tmp/$img_name | |
end | |
# remove tmp file | |
if test -e /tmp/$img_name | |
/bin/rm /tmp/$img_name | |
end | |
if test -e /tmp/$img_name".webp" | |
/bin/rm /tmp/$img_name".webp" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment