Monitor-hdpvr.sh

From MythTV Official Wiki
Revision as of 13:43, 26 March 2016 by Cncook001 (talk | contribs) (Created page with "From this posting: http://www.gossamer-threads.com/lists/mythtv/users/594538 <pre> #!/bin/bash # Monitor a video file for "enter your pin" popup and send pin if required #Tu...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

From this posting: http://www.gossamer-threads.com/lists/mythtv/users/594538

#!/bin/bash
# Monitor a video file for "enter your pin" popup and send pin if required

#Turns out that this requires just a few lines of code with the right tools.
#The script is called from a Start recording event script I need anyway ( to fix hd-pvr recording parameters)
#
#Monitoring script is called like this in the event script:
#/usr/bin/nohup /usr/local/bin/monitor-hdpvr.sh $1 &
#
#Settins for the start recording event:
#+--------------------+---------------------------------------------------------+----------+
#| value | data | hostname |
#+--------------------+---------------------------------------------------------+----------+
#| EventCmdRecStarted | /usr/local/bin/fix-hdpvr.sh %FILE% %STARTTIME% %CARDID% | pvr-sw |
#+--------------------+---------------------------------------------------------+----------+
#
#The script monitors the given filename, grabs a screenshot every 10 seconds and compares it to a reference image. Imagemagick compare returns a result ~500 for the popup screen and ~ 12000 for normal content from movie recordings so accuracy of detection is quite good.

file="/var/lib/mythtv/recordings/$1"
pin="9071"
refimg="/var/lib/mythtv/recordings/schutz.jpg"
debug="1"
delay="10"

frame="${file%.*}.jpg"

function sendKey {
        key=$1
        /usr/bin/irsend send_once SKY $key
}
function sendString {
        s=$1
        for (( i=0; i<${#s}; i++ )); do
                key="KEY_${s:$i:1}"
                sendKey $key
                sleep 0.1
        done
}

[[ "$debug" ]] && echo "$(date): Monitoring $file" >> ~/pvrmon.log
# Keep monitoring while recording is active (last write less than 30 seconds ago
while [ $(($(date +%s)-$(stat -c %Y $file))) -lt 30 ]; do

        #extract first keyframe in last 500K of recording
        /usr/bin/tail -c 500000 $file | \
        /usr/bin/ffmpeg -i - -vf select="eq(pict_type\,I)" -vframes 1 -f image2 $frame >/dev/null 2>&1

        # compare to reference image
        sim=$(/usr/bin/compare -metric MAE $frame $refimg null: 2>&1 | cut -d " " -f1)

        [[ "$debug" ]] && echo "$(date): Compare returned $sim" >> ~/pvrmon.log

        if [ ${sim%.*} -lt "2000" ]; then
                # send pin if captured frame looks like reference image
                [[ "$debug" ]] && echo "$(date): found popup screen, send pin" >> ~/pvrmon.log
                sendString $pin
        fi
        sleep $delay
done
[[ "$debug" ]] && echo "$(date): Monitoring $file - no recent changes, done" >> ~/pvrmon.log