#!/bin/bash

# A tool to scan the current directory for supported media files
# and to verify whether any of them are corrupt using ffmpeg or
# imagemagick accordingly, and bash >= 4.0 syntax.

# Define an image scanner
graphics=("gif" "bmp" "jpg" "png" "jpeg" "ico")
scan_image() {
	for t in ${graphics[*]}; do
		if [ "${1##*.}" = "$t" ]; then
			echo "scanning $1"
			local o=$(gm identify "$1")
			[ $? -eq 0 ] && echo "$o"
		fi
	done
}

# Define a video scanner
av=("mkv" "mp3" "rm" "rmvp" "mp4" "m4v" "avi" "wmv" "flv" "mov" "mpg" "mpeg" "ogg" "ogm" "webm" "xvid" "wpl")
scan_video() {
	for t in ${av[*]}; do
		if [ "${1##*.}" = "$t" ]; then
			echo "scanning $1"
			local o=$(ffmpeg -v error -i "$1" -f null - 2>&1)
			[ $? -ne 0 ] && [ -n "$o" ] && echo "$o"
		fi
	done
}

# Recursively send all files and their extensions to defined scanners
find "$PWD" -type f -print0 | while IFS= read -r -d $'\0' f; do
	scan_image "$f"
	scan_video "$f"
done
echo "scan complete"
