Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Last active March 8, 2021 17:49
Show Gist options
  • Save ddrscott/bf786c589d8feeaf5dfc9546008de1ad to your computer and use it in GitHub Desktop.
Save ddrscott/bf786c589d8feeaf5dfc9546008de1ad to your computer and use it in GitHub Desktop.
Convert cbr files to PDF files
src_dir = sources
dist_dir = dist
cbr_files = $(wildcard $(src_dir)/*.cbr)
pdf_files = $(cbr_files:$(src_dir)/%.cbr=$(dist_dir)/%.pdf)
image_dir = .images-$<
all: ${pdf_files} | $(dist_dir)
@echo Done
clean:
rm -rf $(dist_dir)
$(dist_dir):
mkdir -p $@
$(dist_dir)/%.pdf : $(src_dir)/%.cbr | $(dist_dir)
@# use @ to prevent `make` from printing the command it is executing
@echo Building $< into $@
@# ^ ^
@# | |
@# | |
@# | +- foo.pdf
@# |
@# +- foo.cbr
@#
rar x $< ${image_dir}/
@# ^ ^ ^
@# | | |
@# | | +- unpack to destination directory
@# | |
@# | +- foo.cbr
@# |
@# +- extract
@#
find ${image_dir}/ -size 0 -ls -delete
@# ^ ^ ^ ^
@# | | | |
@# | | | +- delete the file
@# | | |
@# | | +- print `ls -l` of the file
@# | |
@# | +- only files with zero size
@# +- find in image directory
@#
magick convert ${image_dir}/* $@
@# ^ ^
@# | |
@# | +- foo.pdf
@# |
@# +- all files in image directory
@#
rm -rf ${image_dir}
@# ^
@# |
@# +- delete entire image director. We don't need it anymore.
@#
@echo $@ is ready
@ddrscott
Copy link
Author

ddrscott commented Feb 24, 2021

Uses rar, make and convert from imagemagick to unpack cbr files into .images/ and convert them to a pdf.

The Makefile is only a few lines longer than a bash script would be and has the following benefits because of make:

  • automatic looping because of wildcard
  • simple file extension mapping at #2
  • stops on errors without remembering bash flag -e
  • make will only create new and changed cbr files
  • make -j 4 provides parallel processing! Change 4 to the number of cores minus 1.
cd /path/to/cbr_files
curl -O https://gist.githubusercontent.com/ddrscott/bf786c589d8feeaf5dfc9546008de1ad/raw/c578ec960c7514dbd895dde5eabe2c0e11407f5c/MakefileMakefile
make

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment