Method 1. Use a djvused(1) script to save each page to a standalone file
You probably want this method.
In bash, you would do something like this:
input_file=inputfile.djvu
output_dir=outdir
mkdir "$output_dir"
page_count=$( djvused "$input_file" -e n )
for page_num in $( seq 1 "$page_count" ); do
printf 'select %d; save-page outdir/page-%04d.djvu\n' "$page_num" "$page_num"
done
| djvused "$input_file"
That would generate a djvused script like this:
select 1; save-page-with outdir/page-0001.djvu
select 2; save-page-with outdir/page-0002.djvu
select 3; save-page-with outdir/page-0003.djvu
select 4; save-page-with outdir/page-0004.djvu
select 5; save-page-with outdir/page-0005.djvu
select 6; save-page-with outdir/page-0006.djvu
which would produce a directory a directory of files, like this:
$ ls -l outdir/
total 400
-rw-r--r-- 1 dwon dwon 59145 Apr 26 17:30 page-0001.djvu
-rw-r--r-- 1 dwon dwon 69848 Apr 26 17:30 page-0002.djvu
-rw-r--r-- 1 dwon dwon 60037 Apr 26 17:30 page-0003.djvu
-rw-r--r-- 1 dwon dwon 68312 Apr 26 17:30 page-0004.djvu
-rw-r--r-- 1 dwon dwon 71849 Apr 26 17:30 page-0005.djvu
-rw-r--r-- 1 dwon dwon 68368 Apr 26 17:30 page-0006.djvu
Method 2. Convert the document to "indirect" storage format
The DjVu format allows documents to be stored as multiple files, with references between them. This is what is known as "indirect" format (as opposed to the usual "bundled" format, which stores everything together). It's possible to convert a document between bundled and indirect format.
The benefit of this method is that any complex document structure (e.g. shared dictionaries for JB2 compression, page repetition, metadata covering the whole file, etc) will be preserved. This makes it handy for adding information & metadata (e.g. OCR text) since it preserves the overall content structure.
The drawbacks are that you don't get any control over filenames, and some software might not be able to handle it. (e.g. Okular 21.12.3 chokes on this sample file when converted to indirect format, even though DjView4 works fine.)
The conversion can be done using the djvmcvt command, or by using the save-indirect
subcommand of djvused.
Using djvmcvt(1):
mkdir output_directory
djvmcvt -i doc_in.djvu output_directory index_filename.djvu
Using djvused(1), subcommand save-indirect
:
mkdir output_directory
djvused doc_in.djvu -e "save-indirect output_directory/index_filename.djvu"
These commands produce output like this:
$ mkdir outdir
$ djvmcvt -i sample2.djvu outdir index.djvu
$ ls -l outdir/
total 56
-rw-r--r-- 1 dwon dwon 22173 Apr 26 18:32 dict0084.iff
-rw-r--r-- 1 dwon dwon 71 Apr 26 18:32 index.djvu
-rw-r--r-- 1 dwon dwon 22653 Apr 26 18:32 p0001.djvu
-rw-r--r-- 1 dwon dwon 3688 Apr 26 18:32 p0002.djvu
$ djvudump outdir/index.djvu
FORM:DJVM [59]
DIRM [47] Document directory (indirect, 3 files 2 pages)
dict0084.iff -> dict0084.iff
p0001.djvu -> p0001.djvu
p0002.djvu -> p0002.djvu
$ djvudump outdir/dict0084.iff
FORM:DJVI [22161]
Djbz [22149] JB2 shared dictionary
$ djvudump outdir/p0001.djvu
FORM:DJVU [22641]
INFO [10] DjVu 3300x2550, v21, 300 dpi, gamma=2.2
INCL [12] Indirection chunk --> {dict0084.iff}
Sjbz [3054] JB2 bilevel data
FGbz [22] JB2 colors data, v0, 2 colors
BG44 [3774] IW4 data #1, 72 slices, v1.2 (color), 1100x850
BG44 [3400] IW4 data #2, 11 slices
BG44 [5270] IW4 data #3, 10 slices
BG44 [6635] IW4 data #4, 6 slices
TXTz [387] Hidden text (text, etc.)
$ djvudump outdir/p0002.djvu
FORM:DJVU [3676]
INFO [10] DjVu 3300x2550, v21, 300 dpi, gamma=2.2
INCL [12] Indirection chunk --> {dict0084.iff}
Sjbz [2792] JB2 bilevel data
FGbz [404] JB2 colors data, v0, 43 colors
BG44 [87] IW4 data #1, 97 slices, v1.2 (b&w), 275x213
TXTz [318] Hidden text (text, etc.)