I've done a bit more work on importing:
Extracting the start and end times from the filesystem won't give correct results if you're post-processing.
This version of tvhimport.sh calculates a duration from the file times, and if that's too short
it uses ffprobe to get the duration of the recording and calculates approximately correct start
and end times from that. The duration is now included in the import data.
Obviously you'll need to ensure you have ffprobe installed for this to be useful.
cat tvhimport.sh
#!/bin/bash
#
# Import recordings into TVHeadend
# Assumes "Make subdirectories per title" is enabled, - if this is not the
# case, the title will have to be extracted from the filename.
#
INFILE=$1
if [ $# -ne 1 ] || [ ! -e "$INFILE" ]; then
echo "Usage: $0 /full/path/to/file"
exit 1
fi
duration ()
{
echo $(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $1 2> /dev/null | cut -d '.' -f 1)
}
FILE=$(basename $INFILE)
RECPATH=$(dirname $INFILE)
START=$(stat --format=%X $INFILE)
END=$(stat --format=%Y $INFILE)
let DUR=END-START
# If duration is too short, probe the file
if [ $DUR -lt 1800 ]; then
DUR=$(duration $INFILE)
END=$START
let START=END-DUR
fi
# Extract title from last level of RECPATH
TITLE=$(basename $RECPATH | sed -e 's/-/ /g' -e 's/_/ /g')
cat << EOF > /tmp/import.json
conf={
"enabled": true,
"start": $START,
"stop": $END,
"channelname": "local file",
"duration" : $DUR,
"title": {
"eng": "$TITLE"
},
"subtitle": {
"eng": "filename: $FILE"
},
"description": {
"eng": ""
},
"comment": "added by $(basename $0)",
"files": [
{
"filename": "$INFILE"
}
]
}
EOF
echo "Sending create command for \"$TITLE\""
curl -q --data @/tmp/import.json 'http://admin:secret@localhost:9981/api/dvr/entry/create'
echo
exit 0
This script finds any .ts or .mkv files in your recording dir, and imports them into
tvheadend if they're not found in the tvh logs. If you run it with any arguments at all it
will just print out the files it finds.
cat tvh_import_missing.sh
#!/bin/bash
#
# Usage - call with any argument to display files not found in TVH logs
# tvh_import_missing test
#
# Call with no args to import the files into TVH
#
# Modify to suit
HOME=/home/pi/.hts
BIN=/usr/local/bin
RECORDINGS=/var/spool/pvr/
if [ $# -eq 0 ]; then
COMMAND=$BIN/tvhimport.sh
else
COMMAND=echo
echo "Running in test mode"
fi
echo "Scanning logs and recordings"
# Get list of filenames from existing log files
grep filename $HOME/tvheadend/dvr/log/* | cut -d: -f3 > /tmp/log_filenames.txt
# Get list of recordings in recording directory
find /var/spool/pvr/ -name "*.ts" -o -name "*.mkv" > /tmp/files.txt
echo "Checking for recordings missing from logs"
# Search for files in list of existing log files
while read -r file ; do
if ! grep -q "$file" /tmp/log_filenames.txt
then
$COMMAND "$file"
fi
done < /tmp/files.txt