Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.sh text eol=lf
*.py text eol=lf
*.md text eol=lf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.py[cod]
151 changes: 134 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,140 @@
This repository contains a script that was painstakingly developed to automate and batch process ffmpeg jobs across various servers. There are many ways to formulate an ffmpeg command, this example rescales a letterboxed image captured in a 1920x1080 frame to 640x480. The script first defines all of the functions, then calls them all in sequence. This is needed later to repeat a run if for some reason the process misses some files.
# FFmpeg batch processing

The following assumptions are made about this script:
Transcode videos locally or across SSH workers sharing storage. The default
preset preserves this project's original use case: crop a specific letterboxed
1920×1080 capture to `1584:1072:158:6`, then scale to 640×480. Outputs use H.264
(`libx264`, medium, CRF 23) and AAC (48 kHz, stereo, 128 kb/s) in MP4 containers.
For other source dimensions, choose a suitable `--filter`.

* There are 3 servers available over SSH
* All SSH sessions are using the same username and passwordless auth (pubkey in my case)
* Those servers all have the working directory mounted in the same place. I used a network share.
* Each server has r/w access to the working directory
* One of the processing servers is localhost
* All servers are of the same capability, can handle the same concurrency level (10 is used here, each of my servers has 56 threads)
* All servers have ffmpeg installed and available

## Requirements

The script wil create:
* Temprary files in the working directory. Read the code for more info, all of them clean themselves up
* error_log.txt (used to collect ffmpeg errors/failures to read file)

- Bash 4.4 or newer, GNU core utilities and GNU find, including `realpath`,
`sha256sum`, `mktemp`, and `ln -T`.
- FFmpeg with the `libx264` and AAC encoders on every worker.
- An output filesystem supporting hard links. This allows atomic publication
without replacing an existing destination. Preflight fails if hard links are
unavailable; there is no overwrite fallback.
- Remote workers need SSH public-key authentication, a POSIX login shell, and
the **same absolute input/output paths on shared storage**. Preflight checks a
unique shared run token and write access. The script is sent over SSH stdin;
no remote script installation is required.

Don't worry about:
The offline suite has been exercised with Git Bash 5.2 on Windows. GNU/Linux is
the intended deployment environment; check actual SSH/shared-storage behavior
on a small sample before scheduling a large batch.

* wait: no such job (means processing exited some time before wait was called. Job is successful though
* Evenly splitting files: The script splits on number of files, not size of files. This can mean that things are a teensy bit skewed towards one server or another. If you want everything to be exactly the same, feel free. It doesn't make much of a difference in my case and I was lazy so I didn't handle the file sizes and split that way.
## Start locally

```bash
# Inspect the plan: no writes, encodes, or SSH connections.
bash vidprocess.sh --input /media/captures --dry-run

# Default: localhost, two concurrent jobs, no automatic retries.
bash vidprocess.sh --input /media/captures

# Separate output root and one extra attempt per failed job.
bash vidprocess.sh --input /media/captures --output /media/converted \
--jobs 4 --retries 1

# Material that does not need the original crop.
bash vidprocess.sh --input /media/captures --filter 'scale=640:-2'
```

`--help` lists all options. Zero matching files is a successful no-op. Discovery
is recursive, case-insensitive for supported extensions, NUL-delimited, and does
not follow directory symlinks. The selected output tree is excluded. Launch the
script from any working directory.

Outputs preserve relative source paths and **append `.mp4` to the full filename**:

| Input relative path | Output relative path |
| --- | --- |
| `camera-a/clip.mov` | `camera-a/clip.mov.mp4` |
| `camera-b/clip.mov` | `camera-b/clip.mov.mp4` |
| `camera-a/clip.mp4` | `camera-a/clip.mp4.mp4` |

The default output is `INPUT/transcoded`. Input/output cannot be filesystem root,
and output cannot equal or contain input. `.vidprocess` is reserved at the output
root; source files beneath an input `.vidprocess` subtree are rejected because
their mapped destinations would collide with this state namespace.

## Shared-storage workers

```bash
bash vidprocess.sh --input /mnt/shared/captures \
--host localhost --host encode-a --host encode-b --user media --jobs 2
```

Each `--host` adds a worker with `--jobs` slots. This single list drives assignment,
including small batches. If no hosts are specified, only localhost is used.
Omit `--user` to use SSH's configured username. SSH uses batch mode and a
ten-second connection timeout. Concurrency limits FFmpeg processes, not their
internal threads. Scheduling uses bounded round-robin slots, not duration balancing.

## Results and recovery

Reports separate successful, verified-skipped, and failed jobs. Exit status is
`0` for a completed run without failures, `1` for failed jobs, `2` for configuration
or preflight errors, and `130` for handled interruption. No retry prompt requires
interactive input.

State is retained under `OUTPUT/.vidprocess`:

- `runs/run.*/files.nul`: exact input snapshot, NUL-delimited.
- `runs/run.*/manifest.tsv`: job ID, shell-escaped source/destination, and host.
- `runs/run.*/*.log`: stdout/stderr for each job attempt.
- `runs/run.*/*.result`: final job status.
- `runs/run.*/retry.nul`: failed paths after a completed run, if any.
- `receipts/`: source/preset and output-content verification hashes.
- `lock` and `locks/`: coordinator and individual-output reservations.

Resume by rerunning the same command. An output is skipped only when its receipt
matches source contents, filter/preset, and current output contents. Failed or
unfinished files are attempted again, with additional attempts controlled by
`--retries`. Logs and manifests remain available after the run. Remove old run
directories manually only when no workers use them; keep receipts for future
verified skipping.

FFmpeg runs with `-xerror`: reported decoding/demuxing errors fail the job before
publication or receipt creation. Receipts from the earlier permissive decoder
policy are not accepted; existing outputs are preserved as conflicts. Inspect
them and use a new output root or move them aside before retrying. This does not
detect missing content that the media container/decoder reports as valid.

Verification reads entire source and output files. Successful encoding also
checks the source again before publication to detect changes during processing.
This adds disk/network I/O. Keep source media stable while processing it.

**Existing output conflicts are preserved and reported as failures.** This
includes files without matching receipts, modified files, and files made with a
different filter. Choose a new output root or move a conflicting output aside
after inspecting it; there is no overwrite flag. A crash between output and
receipt publication can leave a complete file without a receipt. It is
deliberately treated as a conflict: inspect it and move it aside or choose a new
output root before retrying.

Unique partial files are created beside the final destination. Handled local
cancellation stops active encoders and removes owned partials/reservations while
retaining logs/manifests. SSH disconnection cannot guarantee remote process
termination. Confirm remote jobs have stopped before removing stale locks.
After an unhandled crash or machine failure, inspect worker processes and run
records before manually removing stale reservations or abandoned
`.vidprocess.*.mp4` partials. The script never recursively deletes the working
directory or clears previous run logs.

## Offline regression tests

```bash
python -m unittest discover -s tests -v
bash -n vidprocess.sh
```

Tests require Python 3.10+ and use temporary fixtures with local FFmpeg/SSH stubs;
they never contact remote hosts. Set `BASH_EXE` if needed (Windows defaults to the
standard Git Bash installation). Coverage includes zero-to-ten-file batches,
duplicate names, verified reruns, changed inputs/outputs, overwrite refusal,
bounded retries, empty/failed encodes, remote quoting/failures, cancellation,
and dry-run behavior. Real synthetic FFmpeg controls also cover truncated input,
retry after source repair, and valid media paths containing spaces/apostrophes
(explicitly converted for native Windows FFmpeg). Newline filenames are skipped on Windows because its
filesystem forbids them.
Loading