Convert Comics into CBZ format
Posted by mazet on 12 Avr 2020 in Programmation, Bash
Shell
#!/bin/bash -f | |
# convert2cbz - a comics file convertor | |
# Copyright (C) 2020 Laurent Mazet | |
| |
# This program is free software; you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation; either version 2, or (at your option) | |
# any later version. | |
| |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
| |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software Foundation, | |
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
| |
# Changelog: | |
# - 1.0.1 04/04/2021 | |
# * bug fixes | |
# - 1.0.0 11/04/2020 | |
# * initial version | |
| |
#DEBUG=yes | |
QUALITY=75 | |
OUTPUT=output | |
| |
# formating functions | |
function title () { echo -e "\033[0;1m$*\033[0;0m"; } | |
function pass () { echo -e "\033[1;32m$*\033[0;0m"; } | |
function warn () { echo -e "\033[1;33m$*\033[0;0m"; } | |
function fail () { echo -e "\033[1;31m$*\033[0;0m"; } | |
function debug () { [ "$DEBUG" = yes ] && echo -e "\033[1;34m$*\033[0;0m"; } | |
| |
# check output directory | |
echo $OUTPUT | grep -q '^[~/]' || OUTPUT="$(pwd)/$OUTPUT" | |
debug "dirout: $OUTPUT" | |
| |
for file; do | |
title "Processing: $file" | |
| |
# get file | |
debug "input: $file" | |
echo $file | grep -q '^[~/]' || file="$(pwd)/$file" | |
[ -f "$file" ] || { warn "usage: $(basename $0) <file>"; exit 1; } | |
debug "file: $file" | |
| |
# check type | |
case $(file "$file") in | |
*"7-zip archive data"*) type="7Z";; | |
*"PDF document"*) type="PDF";; | |
*"RAR archive data"*) type="RAR";; | |
*"POSIX tar archive"*) type="TAR";; | |
*"Zip archive data"*) type="ZIP";; | |
*) fail "unkown archive type"; exit 1;; | |
esac | |
debug "type: $type" | |
| |
# create temporary directory | |
tmp=$(mktemp -d) | |
[ -d "$tmp" ] || { fail "unable to create temporary directory"; exit 1; } | |
debug "tmp: $tmp" | |
| |
# extract images | |
case $type in | |
7Z) (cd $tmp; 7z x "$file");; | |
PDF) (cd $tmp; pdfimages -j "$file" page >/dev/null);; | |
RAR) (cd $tmp; unrar -inul x "$file");; | |
TAR) (cd $tmp; tar xf "$file");; | |
ZIP) (cd $tmp; unzip -q "$file");; | |
esac | |
[ -d "$tmp/$(ls $tmp)" ] && [ ! x"$(ls $tmp)" = xbook ] && mv "$tmp/$(ls $tmp)" $tmp/book | |
chmod u+rw -R $tmp | |
list=$(cd $tmp; find -type f | sort -n | awk '{sub(/\.\//, ""); m=m" "$0} END{print m}') | |
debug "archive:$list" | |
| |
# recompress image | |
before=$(find $tmp -type f -print0 | xargs -0 wc -c | awk '$2=="total" {print $1}') | |
#find $tmp -iname *.jpg -o -iname *.jpeg -print0 | xargs -0 mogrify -quality $QUALITY | |
[ -z "$(find $tmp -iname '*.jpg' -o -iname '*.jpeg')" ] || \ | |
{ find $tmp -iname '*.jpg' -o -iname '*.jpeg' -print0 | xargs -0 jpegoptim -m$QUALITY -p -q -s; } | |
[ -z "$(find $tmp -iname '*.png')" ] || \ | |
{ find $tmp -iname '*.png' -print0 | xargs -0 optipng -o2 -quiet -preserve; } | |
after=$(find $tmp -type f -print0 | xargs -0 wc -c | awk '$2=="total" {print $1}') | |
debug "compression: $after/$before $(echo $after*100/$before | bc)%" | |
| |
# check process effyciency | |
[ $after -gt $before ] && warn "useless compressing $(echo $after*100/$before | bc)%" | |
| |
# create archive | |
archive=$(basename "$file"| sed 's/\.[^.]*$/.cbz/') | |
[ -d "$OUTPUT" ] || mkdir "$OUTPUT" | |
debug "output: $OUTPUT/$archive" | |
[ -f "$OUTPUT/$archive" ] && rm "$OUTPUT/$archive" | |
(cd $tmp; find . -type f -print | zip -9 --quiet "$OUTPUT/$archive" -@) | |
size=$(ls -sh "$OUTPUT/$archive" | awk '{print $1}') | |
debug "result: $size" | |
| |
# remove temporary directory | |
rm -rf $tmp | |
| |
pass "Produced: $archive $size" | |
done |