Last active
November 26, 2025 11:59
-
-
Save karolba/dedbe8d98955704f56c4524fdde18c92 to your computer and use it in GitHub Desktop.
Convert comics (.rar, .zip, .cbz, .cbx) to pdfs based on their internal file structure
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
| # remember to not use the old builtin gnu make on macos | |
| ifeq ($(word 1,$(subst ., ,$(MAKE_VERSION))),3) | |
| _ := $(error use a newer gnu make) | |
| endif | |
| SHELL := bash | |
| manga_dir := $(HOME)/Documents/manga | |
| escape = '$(subst ','\'',$1)' | |
| img2pdf_opts := --rotation=ifvalid --engine=internal | |
| find_image_files = \ | |
| find $1 -type f -exec file --mime-type --no-pad --separator / -- '{}' '+' \ | |
| | sed -n 's|/ image/[^ ]*$$||p' \ | |
| | grep -vF /__MACOSX/ \ | |
| | grep -vF /.DS_Store/ | |
| define pdfize | |
| mkdir -p $(call escape,$@)/_unpack | |
| cd $(call escape,$@)/_unpack && \ | |
| $1 $(call escape,$^) | |
| cd $(call escape,$@) && \ | |
| $(call find_image_files,.) \ | |
| | sed 's|/[^/]*$$||' \ | |
| | sort --version-sort \ | |
| | uniq \ | |
| | while read -r comic_dir; do \ | |
| echo "Processing $$comic_dir"; \ | |
| $(call find_image_files,"$$comic_dir" -maxdepth 1) \ | |
| | sort --version-sort \ | |
| | tr '\n' '\0' \ | |
| | img2pdf \ | |
| --from-file - \ | |
| $(img2pdf_opts) \ | |
| -o ./"$$(echo "$$comic_dir" | tr / - | sed 's|^\.-_unpack-||')".pdf -- "$${files[@]}"; \ | |
| done | |
| rm -r $(call escape,$@)/_unpack | |
| endef | |
| .PHONY: all | |
| all: | |
| shopt -s nullglob \ | |
| && pushd $(call escape,$(manga_dir)) \ | |
| && all=( \ | |
| *.zip */*.zip \ | |
| *.rar */*.rar \ | |
| *.cbz */*.cbz \ | |
| *.cbr */*.cbr \ | |
| ) \ | |
| && popd \ | |
| && $(MAKE) "$${all[@]/%//}" | |
| %.rar/: $(manga_dir)/%.rar; $(call pdfize,unrar x) | |
| %.cbr/: $(manga_dir)/%.cbr; $(call pdfize,unrar x) | |
| %.zip/: $(manga_dir)/%.zip; $(call pdfize,unar) | |
| %.cbz/: $(manga_dir)/%.cbz; $(call pdfize,unar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment