Archived
docs: add programmatic FFmpeg workflow example
Shows complete process: submit job, poll status, get progress, handle completion. Includes example bash script. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ee38f1ebaa
commit
c31029b2b3
1 file changed
+152
+152
@@ -2,6 +2,158 @@
|
|||||||
|
|
||||||
This guide covers FFmpeg-specific behavior when running jobs with Barn.
|
This guide covers FFmpeg-specific behavior when running jobs with Barn.
|
||||||
|
|
||||||
|
## Programmatic Workflow
|
||||||
|
|
||||||
|
This section shows the complete process for running FFmpeg jobs programmatically (e.g., from a script or application over SSH).
|
||||||
|
|
||||||
|
### Step 1: Submit the Job
|
||||||
|
|
||||||
|
```bash
|
||||||
|
barn run --output=json -- ffmpeg -y -progress pipe:1 -threads 0 -i input.mkv -c:v libx264 output.mp4
|
||||||
|
```
|
||||||
|
|
||||||
|
Response:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "job-a1b2c3d4",
|
||||||
|
"state": "queued",
|
||||||
|
"command": ["ffmpeg", "-y", "-progress", "pipe:1", "-threads", "0", "-i", "input.mkv", "-c:v", "libx264", "output.mp4"],
|
||||||
|
"createdAt": "2026-01-22T10:00:00.000Z",
|
||||||
|
"pid": null,
|
||||||
|
"exitCode": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Save the `id` field for subsequent commands.
|
||||||
|
|
||||||
|
### Step 2: Poll for Status
|
||||||
|
|
||||||
|
Check if the job is still running:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
barn describe job-a1b2c3d4 --output=json
|
||||||
|
```
|
||||||
|
|
||||||
|
Response while running:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "job-a1b2c3d4",
|
||||||
|
"state": "running",
|
||||||
|
"pid": 12345,
|
||||||
|
"exitCode": null,
|
||||||
|
"startedAt": "2026-01-22T10:00:01.000Z",
|
||||||
|
"finishedAt": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Response when complete:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "job-a1b2c3d4",
|
||||||
|
"state": "succeeded",
|
||||||
|
"pid": 12345,
|
||||||
|
"exitCode": 0,
|
||||||
|
"startedAt": "2026-01-22T10:00:01.000Z",
|
||||||
|
"finishedAt": "2026-01-22T10:05:30.000Z"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Step 3: Get Progress (While Running)
|
||||||
|
|
||||||
|
Use `--logs` to include stdout/stderr in the response:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
barn describe job-a1b2c3d4 --logs --output=json
|
||||||
|
```
|
||||||
|
|
||||||
|
Response:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "job-a1b2c3d4",
|
||||||
|
"state": "running",
|
||||||
|
"logs": {
|
||||||
|
"stdout": "frame=5000\nfps=120.5\nspeed=2.5x\nprogress=continue\n",
|
||||||
|
"stderr": "ffmpeg version 7.0..."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Parse the `logs.stdout` field to extract progress. Look for:
|
||||||
|
- `frame=N` - current frame number
|
||||||
|
- `fps=N` - encoding speed in frames per second
|
||||||
|
- `speed=Nx` - encoding speed relative to realtime
|
||||||
|
- `out_time=HH:MM:SS` - current output timestamp
|
||||||
|
- `progress=continue` or `progress=end`
|
||||||
|
|
||||||
|
### Step 4: Handle Completion
|
||||||
|
|
||||||
|
Check `state` and `exitCode`:
|
||||||
|
|
||||||
|
| State | exitCode | Meaning |
|
||||||
|
|-------|----------|---------|
|
||||||
|
| `succeeded` | 0 | Job completed successfully |
|
||||||
|
| `failed` | non-zero | Job failed (check `error` and `logs.stderr`) |
|
||||||
|
| `killed` | null | Job was killed by user |
|
||||||
|
|
||||||
|
Example error response:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"id": "job-a1b2c3d4",
|
||||||
|
"state": "failed",
|
||||||
|
"exitCode": 1,
|
||||||
|
"error": "Process exited with code 1",
|
||||||
|
"logs": {
|
||||||
|
"stdout": "",
|
||||||
|
"stderr": "input.mkv: No such file or directory"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Complete Example Script
|
||||||
|
|
||||||
|
```bash
|
||||||
|
#!/bin/bash
|
||||||
|
set -e
|
||||||
|
|
||||||
|
INPUT="/path/to/input.mkv"
|
||||||
|
OUTPUT="/path/to/output.mp4"
|
||||||
|
|
||||||
|
# Submit job
|
||||||
|
RESPONSE=$(barn run --output=json -- ffmpeg -y -progress pipe:1 -threads 0 -i "$INPUT" -c:v libx264 "$OUTPUT")
|
||||||
|
JOB_ID=$(echo "$RESPONSE" | jq -r '.id')
|
||||||
|
echo "Started job: $JOB_ID"
|
||||||
|
|
||||||
|
# Poll until complete
|
||||||
|
while true; do
|
||||||
|
STATUS=$(barn describe "$JOB_ID" --output=json)
|
||||||
|
STATE=$(echo "$STATUS" | jq -r '.state')
|
||||||
|
|
||||||
|
case "$STATE" in
|
||||||
|
"queued"|"running")
|
||||||
|
# Get progress
|
||||||
|
PROGRESS=$(barn describe "$JOB_ID" --logs --output=json | jq -r '.logs.stdout' | grep -o 'speed=[0-9.]*x' | tail -1)
|
||||||
|
echo "Status: $STATE $PROGRESS"
|
||||||
|
sleep 2
|
||||||
|
;;
|
||||||
|
"succeeded")
|
||||||
|
echo "Job completed successfully"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
"failed")
|
||||||
|
ERROR=$(echo "$STATUS" | jq -r '.error')
|
||||||
|
echo "Job failed: $ERROR"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Job ended with state: $STATE"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Recommended FFmpeg Command
|
## Recommended FFmpeg Command
|
||||||
|
|
||||||
Use `-progress pipe:1` to output machine-readable progress to stdout:
|
Use `-progress pipe:1` to output machine-readable progress to stdout:
|
||||||
|
|||||||
Reference in new issue
Block a user