Created
April 27, 2022 02:04
-
-
Save dlitz/273b228cf47109a5805cff7e5bacd8b0 to your computer and use it in GitHub Desktop.
Shell script to split .djvu files into pages, one per file
This file contains 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/bash | |
# dlitz 2022 | |
# SPDX-License-Identifier: CC0-1.0 | |
set -eu | |
usage() { | |
echo "usage: $0 [-v] inputfile.djvu output-dir" | |
echo "Split a DjVu document into multiple pages, one per file." | |
echo "Use djvm -c to reassemble the file." | |
echo "" | |
echo "Example: $0 foo.djvu pages" | |
} | |
if [ "$*" = "--help" ]; then | |
usage | |
exit 0 | |
elif [ "${1-}" = "-v" ]; then | |
verbose=-v | |
shift 1 | |
else | |
verbose= | |
fi | |
if [ "$#" -ne 2 ]; then | |
usage >&2 | |
exit 2 | |
fi | |
input_file="$(readlink -f "$1")" # absolute path | |
output_dir="$2" | |
page_count=$( djvused "$input_file" -e n ) | |
if ! test -d "$output_dir"; then | |
mkdir "$output_dir" | |
elif [ "$(find "$output_dir" -mindepth 1 -maxdepth 1 | wc -l)" -gt 0 ]; then | |
echo >&2 "$0: error: directory already exists and is not empty: $output_dir" | |
exit 1 | |
fi | |
cd "$output_dir" | |
generate_script() { | |
for page_num in $( seq 1 "$page_count" ); do | |
output_file=$( printf "page-%04d.djvu" "$page_num" ) | |
echo "select $page_num; save-page-with $output_file;" | |
done | |
} | |
if [ "${verbose:+1}" ]; then | |
# show the script being used | |
generate_script | |
fi | |
generate_script | djvused ${verbose} "$input_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment