givecover (1012B)
1 #!/bin/bash 2 # Attach cover art to audio files using ffmpeg 3 4 process_file() { 5 local file="$1" 6 local COVER_NAMES=("cover.jpg" "Cover.jpg" "cover.png" "Cover.png") 7 local dir 8 dir=$(dirname "$file") 9 local cover="" 10 for name in "${COVER_NAMES[@]}"; do 11 if [ -f "$dir/$name" ]; then 12 cover="$dir/$name" 13 break 14 fi 15 done 16 17 if [ -z "$cover" ]; then 18 echo "[—] $file (no cover found in $dir)" 19 return 20 fi 21 22 local tmpfile="${file}.givecover.tmp.${file##*.}" 23 if ffmpeg -nostdin -y -i "$file" -i "$cover" -map 0:a -map 1:0 -c copy -disposition:v:0 attached_pic "$tmpfile" 2>/dev/null; then 24 mv "$tmpfile" "$file" 25 echo "[✓] $file" 26 else 27 rm -f "$tmpfile" 28 echo "[✗] $file" 29 fi 30 } 31 32 export -f process_file 33 34 EXTENSIONS=("flac" "mp3" "m4a" "ogg" "wav" "aif" "aiff") 35 36 find_args=() 37 for i in "${!EXTENSIONS[@]}"; do 38 if [ "$i" -gt 0 ]; then 39 find_args+=(-o) 40 fi 41 find_args+=(-iname "*.${EXTENSIONS[$i]}") 42 done 43 44 find "${1:-.}" -type f \( "${find_args[@]}" \) -print0 | 45 sort -z | 46 parallel -0 process_file {}