Skip to content

coroboros/agent-skills / skills/video-loop

video-loop

Verified

Prepare a background video loop for a website with optional crossfade, MP4/WebM encoding and poster extraction. Use for loop joins or web delivery of an existing clip.

FileKindSize
SKILL.mdskill md6.2 KB
scripts/video-loop.shexecutablescript6.5 KB

scripts/video-loop.sh

View on GitHub
#!/usr/bin/env bash
#
# video-loop.sh — crossfade and encode a background video loop (MP4 + WebM)
#
# Usage:
#   video-loop.sh <input> [-d <fade>] [-q <h264_crf>] [-w <vp9_crf>] [-o <out_dir>] [-p] [-C]
#
# Options:
#   -d <fade>       Crossfade duration in seconds (default: 1.5)
#   -q <h264_crf>   H.264 CRF — 18=high quality, 28=small file (default: 26)
#   -w <vp9_crf>    VP9 CRF (default: 32)
#   -o <out_dir>    Output directory (default: same as input)
#   -p              Also extract poster frame (first frame as JPEG)
#   -C              Disable crossfade — re-encode only (-n: deprecated alias)
#
# Pipeline:
#   probe → (optional crossfade → lossless intermediate) → encode MP4 → encode WebM → (optional poster)
#
# The crossfade is placed at the START of the output so the loop point is
# near the same source position: middle approaches A[D-F], then loops back
# to the xfade starting there. Verify motion continuity in actual playback.
#
# Emits a machine-readable summary on stdout prefixed with "RESULT:", one
# key=value per line. Consumers parse these to report file sizes and deltas.
#
# Exit codes:
#   0   success — output matches expected duration and codec
#   1   input / tool validation error, or `-d >= duration/2`
#   3   encoded successfully but post-condition check failed (output
#       duration off by more than 0.2s, or MP4 codec not H.264)

set -euo pipefail

FADE=1.5
H264_CRF=26
VP9_CRF=32
OUT_DIR=""
POSTER=0
NO_FADE=0

usage() {
  sed -n '2,22p' "$0" | sed 's/^# \{0,1\}//'
  exit 2
}

[[ $# -lt 1 ]] && usage
case "$1" in -h|--help) usage ;; esac

INPUT=$1
shift

while getopts ":d:q:w:o:pnC" opt; do
  case $opt in
    d) FADE=$OPTARG ;;
    q) H264_CRF=$OPTARG ;;
    w) VP9_CRF=$OPTARG ;;
    o) OUT_DIR=$OPTARG ;;
    p) POSTER=1 ;;
    C) NO_FADE=1 ;;
    n) NO_FADE=1 ;;  # deprecated alias of -C
    \?) echo "unknown flag: -$OPTARG" >&2; usage ;;
    :)  echo "flag -$OPTARG requires a value" >&2; usage ;;
  esac
done

[[ -f "$INPUT" ]] || { echo "error: input not found: $INPUT" >&2; exit 1; }
command -v ffmpeg  >/dev/null || { echo "error: ffmpeg not installed" >&2;  exit 1; }
command -v ffprobe >/dev/null || { echo "error: ffprobe not installed" >&2; exit 1; }

INPUT_ABS=$(cd "$(dirname "$INPUT")" && pwd)/$(basename "$INPUT")
BASENAME=$(basename "$INPUT")
STEM="${BASENAME%.*}"
[[ -z "$OUT_DIR" ]] && OUT_DIR=$(dirname "$INPUT_ABS")
mkdir -p "$OUT_DIR"
OUT_DIR=$(cd "$OUT_DIR" && pwd -P)

MP4="$OUT_DIR/${STEM}-opt.mp4"
WEBM="$OUT_DIR/${STEM}.webm"
POSTER_JPG="$OUT_DIR/${STEM}-poster.jpg"
LOOP_TMP="/tmp/vl-loop-$$.mkv"

TARGETS=("$MP4" "$WEBM")
[[ $POSTER -eq 1 ]] && TARGETS+=("$POSTER_JPG")
for target in "${TARGETS[@]}"; do
  if [[ "$INPUT_ABS" == "$target" || "$INPUT_ABS" -ef "$target" ]]; then
    echo "error: output would overwrite input ($target). Pass -o <dir> to write elsewhere." >&2
    exit 1
  fi
done

DURATION=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$INPUT_ABS")
# stat has different flags on macOS (-f%z) vs GNU/Linux (-c%s); try both.
INPUT_BYTES=$(stat -f%z "$INPUT_ABS" 2>/dev/null || stat -c%s "$INPUT_ABS")

trap 'rm -f "$LOOP_TMP"' EXIT

if [[ $NO_FADE -eq 1 ]]; then
  LOOP_SOURCE="$INPUT_ABS"
else
  # Split into three pieces — end_part ($FADE s tail), start_part ($FADE s head),
  # middle (remainder) — then xfade end_part→start_part and prepend to middle, so
  # the output loops back to the same frame it ends on.
  TRIM_END=$(awk -v d="$DURATION" -v f="$FADE" 'BEGIN { printf "%.6f", d - f }')
  HALF=$(awk -v d="$DURATION" 'BEGIN { printf "%.6f", d / 2 }')
  if awk -v f="$FADE" -v h="$HALF" 'BEGIN { exit !(f >= h) }'; then
    echo "error: fade ($FADE s) >= half of duration ($HALF s) — pick a shorter -d" >&2
    exit 1
  fi

  ffmpeg -y -loglevel warning -i "$INPUT_ABS" -i "$INPUT_ABS" -filter_complex "
    [0:v]trim=start=${TRIM_END},setpts=PTS-STARTPTS[end_part];
    [0:v]trim=0:${FADE},setpts=PTS-STARTPTS[start_part];
    [1:v]trim=${FADE}:${TRIM_END},setpts=PTS-STARTPTS[middle];
    [end_part][start_part]xfade=transition=fade:duration=${FADE}:offset=0[xfaded];
    [xfaded][middle]concat=n=2:v=1:a=0[out]
  " -map "[out]" -c:v libx264 -crf 0 -preset ultrafast -an "$LOOP_TMP"
  LOOP_SOURCE="$LOOP_TMP"
fi

ffmpeg -y -loglevel warning -i "$LOOP_SOURCE" \
  -c:v libx264 -crf "$H264_CRF" -preset slow \
  -an -movflags +faststart \
  "$MP4"

ffmpeg -y -loglevel warning -i "$LOOP_SOURCE" \
  -c:v libvpx-vp9 -crf "$VP9_CRF" -b:v 0 \
  -an \
  "$WEBM"

if [[ $POSTER -eq 1 ]]; then
  ffmpeg -y -loglevel warning -ss 0 -i "$MP4" -frames:v 1 -q:v 3 -update 1 "$POSTER_JPG"
fi

# Expected duration = input − FADE with a crossfade, else input. 0.2s tolerance
# absorbs codec rounding.
MP4_DURATION=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$MP4")
if [[ $NO_FADE -eq 1 ]]; then
  EXPECTED_DURATION=$DURATION
else
  EXPECTED_DURATION=$(awk -v d="$DURATION" -v f="$FADE" 'BEGIN { printf "%.6f", d - f }')
fi
DURATION_DELTA=$(awk -v a="$MP4_DURATION" -v e="$EXPECTED_DURATION" 'BEGIN { d = a - e; printf "%.3f", (d < 0 ? -d : d) }')

MP4_CODEC=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of csv=p=0 "$MP4")
WEBM_CODEC=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of csv=p=0 "$WEBM")

DURATION_OK=1
CODEC_OK=1
if awk -v d="$DURATION_DELTA" 'BEGIN { exit !(d > 0.2) }'; then
  DURATION_OK=0
fi
if [[ "$MP4_CODEC" != "h264" ]] || [[ "$WEBM_CODEC" != "vp9" ]]; then
  CODEC_OK=0
fi

MP4_BYTES=$(stat -f%z "$MP4" 2>/dev/null || stat -c%s "$MP4")
WEBM_BYTES=$(stat -f%z "$WEBM" 2>/dev/null || stat -c%s "$WEBM")

echo "RESULT: input_bytes=$INPUT_BYTES"
echo "RESULT: duration_seconds=$DURATION"
echo "RESULT: fade_seconds=$FADE"
echo "RESULT: no_fade=$NO_FADE"
echo "RESULT: mp4_path=$MP4"
echo "RESULT: mp4_bytes=$MP4_BYTES"
echo "RESULT: mp4_codec=$MP4_CODEC"
echo "RESULT: mp4_duration=$MP4_DURATION"
echo "RESULT: expected_duration=$EXPECTED_DURATION"
echo "RESULT: duration_delta=$DURATION_DELTA"
echo "RESULT: webm_path=$WEBM"
echo "RESULT: webm_bytes=$WEBM_BYTES"
echo "RESULT: webm_codec=$WEBM_CODEC"
if [[ $POSTER -eq 1 ]]; then
  POSTER_BYTES=$(stat -f%z "$POSTER_JPG" 2>/dev/null || stat -c%s "$POSTER_JPG")
  echo "RESULT: poster_path=$POSTER_JPG"
  echo "RESULT: poster_bytes=$POSTER_BYTES"
fi

if [[ "$DURATION_OK" -eq 0 ]]; then
  echo "RESULT: error=duration-mismatch actual=$MP4_DURATION expected=$EXPECTED_DURATION delta=$DURATION_DELTA"
  echo "RESULT: ok=false"
  exit 3
fi
if [[ "$CODEC_OK" -eq 0 ]]; then
  echo "RESULT: error=codec-mismatch mp4=$MP4_CODEC webm=$WEBM_CODEC expected=h264/vp9"
  echo "RESULT: ok=false"
  exit 3
fi

echo "RESULT: ok=true"
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 196kUpdated Claude CodeCodexVerifiedInstall