backfill.sh (1581B)
1 #!/usr/bin/env bash 2 set -euo pipefail 3 4 upload_dir="uploads" 5 thumb_dir="$upload_dir/thumbnails" 6 7 mkdir -p "$thumb_dir" 8 9 target_file="${1:-}" # optional file to override 10 override_time="${2:-00:00:05}" # default timestamp 11 12 generate_thumbnail() { 13 local file="$1" 14 local thumb="$2" 15 local timestamp="$3" 16 17 echo "Generating thumbnail for $(basename "$file") at $timestamp as $thumb" 18 if ! ffmpeg -y -ss "$timestamp" -i "$file" -vframes 1 -vf scale=400:-1 "$thumb" 2>/dev/null; then 19 echo "Failed at $timestamp, retrying from start..." 20 ffmpeg -y -i "$file" -vframes 1 -vf scale=400:-1 "$thumb" 21 fi 22 } 23 24 normalize_path() { 25 local file="$1" 26 if [[ "$file" == "$upload_dir/"* ]]; then 27 echo "$file" 28 else 29 echo "$upload_dir/$file" 30 fi 31 } 32 33 if [[ -n "$target_file" ]]; then 34 file_path=$(normalize_path "$target_file") 35 filename=$(basename "$file_path") 36 thumb="$thumb_dir/${filename}.jpg" 37 38 if [[ ! -f "$file_path" ]]; then 39 echo "File $file_path not found" 40 exit 1 41 fi 42 43 generate_thumbnail "$file_path" "$thumb" "$override_time" 44 else 45 find "$upload_dir" -maxdepth 1 -type f \( -iname "*.mp4" -o -iname "*.webm" -o -iname "*.mov" -o -iname "*.avi" -o -iname "*.mkv" -o -iname "*.m4v" \) | while read -r file; do 46 filename=$(basename "$file") 47 thumb="$thumb_dir/${filename}.jpg" 48 49 if [[ -f "$thumb" ]]; then 50 echo "Skipping $filename as thumbnail already exists" 51 continue 52 fi 53 54 generate_thumbnail "$file" "$thumb" "$override_time" 55 done 56 fi