Batch crop a folder of videos?

# Postmortem: Batch Video Cropping Failure via FFmpeg Script

## Summary
An automation attempt to crop multiple videos using FFmpeg in a Windows batch script failed due to unresolved placeholders in filter parameters and improper variable handling. The script executed without error detection but produced incorrect outputs.

## Root Cause
- **Undefined crop parameters**: Placeholders `x` and `y` in `crop=960:ih:x:y` were never replaced with actual coordinates.
- **Lack of testing**: No validation of the cropping command on individual videos before batch execution.
- **Silent failure propagation**: Script executed without checking FFmpeg's exit codes or output validity.
- **Filename handling**: Potential issues with spaces in filenames due to insufficient quoting in loop structures.

## Why This Happens in Real Systems
- Rapid automation tasks prioritize "getting it running" over robustness.
- Placeholder values (e.g., `x`, `y`) are forgotten during script parameterization.
- Batch scripting nuances (variable expansion, special characters) are easily overlooked.
- Assumptions that FFmpeg commands failing individually will "fail loud" in batches.
- Documentation examples often omit contingency checks for production environments.

## Real-World Impact
- **Wasted compute resources**: 40 videos processed incorrectly.
- **Time loss**: Doubled processing time (original processing + rework).
- **Pipeline blockage**: Delays downstream processing stages reliant on cropped videos.
- **Data inconsistency**: Incorrectly cropped videos required manual intervention.

## Example or Code
**Faulty Implementation**:
```batch
@echo off
for %%i in (*.mp4) do (
    ffmpeg -i %%i -filter:v "crop=960:ih:x:y" output_%%i
)

Corrected Implementation:

@echo off
set CROP_X=150  // Actual horizontal start point
set CROP_Y=300  // Actual vertical start point

for %%i in (*.mp4) do (
    ffmpeg -i "%%i" -filter:v "crop=960:ih:%CROP_X%:%CROP_Y%" "cropped_%%i"
    if errorlevel 1 (
        echo ERROR processing "%%i" >> crop_errors.log
    )
)

How Senior Engineers Fix It

  1. Validate before scaling: Test command logic on 1-2 files manually first.
  2. Parameterize configs: Separate crop coordinates into variables or config files.
  3. Implement error handling: Check FFmpeg exit codes and log failures.
  4. Defensive quoting: Wrap all file paths in double quotes.
  5. Dry-run capability: Add echo flags to preview commands before execution.
  6. Output sanitization: Use distinct output directories to avoid overwriting sources.
  7. Metadata verification: Add post-processing checks (e.g., verify output dimensions).

Why Juniors Miss It

  • Placeholder blindness: Focus on macro workflow, overlook placeholder parameters.
  • Assumption of simplicity: Underestimate batch scripting edge cases on Windows.
  • Lack of validation steps: Prioritize execution