#!/usr/bin/env bash # batch-scan.sh — transcribe every PDF in a folder with scan-notes-md. # # usage: batch-scan.sh [scan-notes-md options...] # # PDFs that already have a .md are skipped, and an interrupted PDF # resumes from its last saved page, so the script can simply be rerun. A # failed PDF is reported and the batch moves on to the next one. Extra # options are passed to scan-notes-md; the default is --backend ollama, so # nothing is sent to a cloud service unless you ask for it. # # Long runs belong in tmux: tmux new -s scan 'batch-scan.sh ~/notebooks' set -uo pipefail if [[ $# -lt 1 || ! -d "$1" ]]; then echo "usage: $0 [scan-notes-md options...]" >&2 exit 64 fi dir="$1"; shift opts=("$@") [[ ${#opts[@]} -eq 0 ]] && opts=(--backend ollama) if command -v scan-notes-md >/dev/null 2>&1; then run() { scan-notes-md "$@"; } else : "${PDF_TOOLS_DIR:?set PDF_TOOLS_DIR or install scan-notes-md}" run() { (cd "$PDF_TOOLS_DIR" && uv run --quiet scan-notes-md "$@"); } fi pdfs=() while IFS= read -r -d '' f; do pdfs+=("$f"); done \ < <(find "$dir" -type f -iname '*.pdf' -not -name '.*' -print0 | sort -z) total=${#pdfs[@]} done_n=0; skipped=0; failed=() start=$(date +%s) for i in "${!pdfs[@]}"; do pdf="${pdfs[$i]}" md="${pdf%.*}.md" echo "=== [$((i + 1))/$total] ${pdf#"$dir"/} ($(date '+%H:%M'))" >&2 if [[ -e "$md" ]]; then echo "already transcribed; skipping" >&2 skipped=$((skipped + 1)) continue fi if run "${opts[@]}" "$pdf"; then done_n=$((done_n + 1)) else failed+=("${pdf#"$dir"/}") fi done mins=$(( ($(date +%s) - start) / 60 )) echo "=== finished in ${mins} min: $done_n transcribed, $skipped skipped, ${#failed[@]} failed" >&2 for f in ${failed[@]+"${failed[@]}"}; do echo "failed: $f" >&2; done [[ ${#failed[@]} -eq 0 ]]