targetTime="400"
fallbackTarget="200"
slowForSD="1"
logActionsOnly="1"
# Check for required packages
packagesFound="0"
if hash inuse 2>/dev/null;
then
if hash ffmpeg 2>/dev/null;
then
if [ "$1" == "STANDBY" ]
then
if hash ir 2>/dev/null;
then
packagesFound="1"
fi
else
packagesFound="1"
fi
fi
fi
# Exit if packages are missing
if [ $packagesFound == "0" ]
then
if [ "$1" == "STANDBY" ]
then
echo "--- ERROR Thumbnails requires packages inuse, ffmpeg and ir for batch mode ---"
else
echo "--- ERROR Thumbnails requires packages inuse and ffmpeg ---"
fi
exit 1
fi
# Change the process priority to be a low value
renice 19 -p $$
# For overnight mode run any pending auto tasks first
if [ "$1" == "STANDBY" ]
then
if [ $logActionsOnly == "0" ]
then
echo " "`date +"%d-%m %H:%M"`" Running auto task"
fi
/mod/webif/lib/bin/auto
fi
cd /
cd "media/My Video"
# Loop through all *.ts files in the My Video folder
find . -type f -name "*.ts" | while read fileTS
do
file=${fileTS#*/}
fileRoot=${file%.ts}
process="0"
case "$file" in
\[[^/]*\]/* )
if [ $logActionsOnly == "0" ]
then
echo " "`date +"%d-%m %H:%M"`" ignoring [folder] $file"
fi
;;
* )
# Check if the current file is in use (still recording, being watched etc)
inuse "$file"
if [ $? == "0" ]
then
if [ $logActionsOnly == "0" ]
then
echo "xx "`date +"%d-%m %H:%M"`" In use $file"
fi
else
if [ -f "$fileRoot.thm" ]
then
# Check if the fourth byte is altered
checkByte=`hexdump -n1 -s3 -e'"" 1/1 "%02x"' "$fileRoot.thm"`
fileSize=$(stat -c '%s' "$fileRoot.thm")
# Is this a Humax generated file (original size, original 4th byte)
if [ $checkByte == "ff" ] && [ $fileSize == "43680" ]
then
process="1"
fi
else
# No thumbnail exists
process="1"
fi
if [ $process == "1" ] && [ -f "$fileRoot.hmt" ]
then
flags=`hmt "$fileRoot.hmt" | grep -o 'Flags: .*$'`
markFile="1"
if [ "${flags/*ODEncrypted*}" == "" ]
then
if [ $logActionsOnly == "0" ]
then
echo "xx "`date +"%d-%m %H:%M"`" Encrypted $file"
fi
markFile="0"
else
videoID=`hmt "$fileRoot.hmt" | grep -o 'Video PID:.*$'`
videoID=${videoID#Video PID}
audioID=`hmt "$fileRoot.hmt" | grep -o 'Audio PID:.*$'`
audioID=${audioID#Audio PID}
if [ $audioID == $videoID ]
then
if [ $logActionsOnly == "0" ]
then
echo "xx "`date +"%d-%m %H:%M"`" $file is audio only"
fi
else
# Run the ffmpeg command to generate a thumbnail
cat /dev/null | ffmpeg -loglevel quiet -ss "$targetTime" -i "$file" -frames 1 -pix_fmt bgra -vf vflip -s 140x78 TempThumb.bmp > /dev/null 2>&1
attemptSlowCapture="0"
# Was the thumbnail generated
if [ ! -f TempThumb.bmp ]
then
attemptSlowCapture="1"
else
# Is the thumbnail all grey
picStart=`hexdump -s 54 -n 32 -v -e'"" 1/1 "%02x"' TempThumb.bmp`
if [ $picStart == "818181ff818181ff818181ff818181ff818181ff818181ff818181ff818181ff" ]
then
echo "xx "`date +"%d-%m %H:%M"`" $file thumb looks faulty"
attemptSlowCapture="1"
fi
fi
if [ $attemptSlowCapture == "1" ]
then
echo "xx "`date +"%d-%m %H:%M"`" $file requires fallback method"
if [ "${flags/*SD*}" == "" ] && [ $slowForSD == "1" ]
then
echo "-- "`date +"%d-%m %H:%M"`" Attempting slow capture $file"
cat /dev/null | ffmpeg -loglevel quiet -i "$file" -r 25 -ss "$targetTime" -frames 1 -an -pix_fmt bgra -vf vflip -s 140x78 TempThumb.bmp > /dev/null 2>&1
else
echo "-- "`date +"%d-%m %H:%M"`" Attempting fallback thumbnail $file"
cat /dev/null | ffmpeg -loglevel quiet -ss "$fallbackTarget" -i "$file" -r 25 -frames 1 -an -pix_fmt bgra -vf vflip -s 140x78 TempThumb.bmp > /dev/null 2>&1
fi
else
echo "++ "`date +"%d-%m %H:%M"`" Thumbnail created $file"
fi
fi
if [ -f TempThumb.bmp ]
then
# Trim the bitmap header from the start of the file
dd bs=54 skip=1 if=TempThumb.bmp of="$fileRoot.thm" > /dev/null 2>&1
rm TempThumb.bmp
fi
if [ -f "$fileRoot.thm" ] && [ $markFile == "1" ]
then
# Alter the 4th byte of the thumbnail to show this script has generated it
echo -ne \\xFE | dd conv=notrunc bs=1 count=1 seek=3 of="$fileRoot.thm" > /dev/null 2>&1
fi
fi
else
if [ $logActionsOnly == "0" ]
then
if [ -f "$fileRoot.hmt" ]
then
echo "-- "`date +"%d-%m %H:%M"`" Already processed $file"
else
echo "-- "`date +"%d-%m %H:%M"`" No media information for $file"
fi
fi
fi
fi
;;
esac
done
if [ "$1" == "STANDBY" ]
then
if [ $logActionsOnly == "0" ]
then
echo " "`date +"%d-%m %H:%M"`" Shutting down"
fi
ir STANDBY >> /dev/null
fi