Hi,
Thanks for testing my script. One of the things that happens when you write something like this is you tend not to use it incorrectly, & thus don't test the error handling as well as one should.
The script is called tvhimport.sh, but you can call it whatever you like.
At least one argument is required, being the full path to the file being imported, e.g.
tvhimport.sh /full/path/to/file.mkv
If the file is in the directory you're currently in, you can just use the filename without the path.
You would normally first copy or move the file into the pvr directory, this script doesn't copy the file,
it just tells TVH that it exists.
So, if you have Lewis-s01e05.mkv, you'd first put it in an appropriate pvr directory, creating the directory if it doesn't exist. Naturally, you should use the pvr directory appropriate for your system.
The script expects that the directory the file resides in is the name of the program, as it would be for something you've recorded.
mkdir /var/spool/pvr/Lewis/
mv Lewis.mkv /var/spool/pvr/Lewis/
The you'd import it:
tvhimport.sh /var/spool/pvr/Lewis/Lewis-s01e05.mkv
Here's an updated copy with better error handling.
#!/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.
#
# Not tested on files containing spaces, so make sure your filenames
# don't have any!
#
# Change the following as needed for your setup
USER=admin
PASS=admin
LOGPATH=~/.hts/tvheadend/dvr/log
JSONFILE="/tmp/import.json"
INFILE=$1
filetest ()
{
if [ ! -e $1 ] ; then
echo "$1 does not exist!"
exit 1
fi
}
logtest ()
{
if [ ! -e $1 ] ; then
echo "$1 does not exist!"
exit 1
fi
}
die ()
{
echo "ERROR: Cannot specify both -l and -d: Aborting." >&2
exit 1
}
usage ()
{
echo "Usage:"
echo " $0 -h Display this help message."
echo " $0 /path/to/file -l logfile|-d descfile"
echo " -l /path/to/logfile Import using logfile"
echo " -d /path/to/logfile Import using description from logfile"
exit 1
}
if [ $# -lt 1 ];then
usage
exit 1
fi
if [ "$INFILE" != "-h" ] ; then
shift
filetest $INFILE
fi
LOGFILE=""
DESCFILE=""
while getopts ":hl:d:" opt; do
case ${opt} in
h )
usage
;;
l )
[ $DESCFILE ] && die
LOGFILE=${OPTARG}
;;
d )
[ $LOGFILE ] && die
DESCFILE=${OPTARG}
;;
: )
echo 'missing argument!' >&2
exit 1
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ $LOGFILE ] ; then
if [ ! -e $LOGFILE ] ; then
LOGFILE=$LOGPATH/$LOGFILE
filetest $LOGFILE
fi
fi
if [ $DESCFILE ] ; then
if [ ! -e $DESCFILE ] ; then
DESCFILE=$LOGPATH/$DESCFILE
filetest $DESCFILE
fi
fi
echo $INFILE "Logfile: $LOGFILE Descfile: $DESCFILE"
duration ()
{
VALUE=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 $1 2> /dev/null | cut -d '.' -f 1)
if ! let $VALUE 2>/dev/null
then
# Duration from ffprobe wasn't valid
VALUE=3600
fi
echo $VALUE
}
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')
if [ $LOGFILE ] ; then
if [ "$LOGFILE" == "$JSONFILE" ] ; then
mv ${LOGFILE} /tmp/import.tmp
LOGFILE=/tmp/import.tmp
fi
echo -n "conf=" > "$JSONFILE"
egrep -wv 'config_name|autorec|channel' ${LOGFILE} >> "$JSONFILE"
sed -i "s@filename\": .*\"@filename\": \"${INFILE}\"@" "$JSONFILE"
sed -i "s/comment\": .*\"/comment\": \"Imported\"/" "$JSONFILE"
else
if [ "$DESCFILE" ]; then
CHANNEL=$(grep -w channelname "$DESCFILE" | cut -d ' ' -f 2 | sed -e "s@\"@@g" -e "s@,@@g" )
START=$(grep -w "start" $DESCFILE | head -1 | cut -d ' ' -f 2 | sed 's/,//')
STOP=$(grep -w "stop" $DESCFILE | head -1 | cut -d ' ' -f 2 | sed 's/,//')
else
CHANNEL="local file"
fi
cat << EOF > "$JSONFILE"
conf={
"enabled": true,
"start": $START,
"stop": $END,
"channelname": "$CHANNEL",
"duration" : $DUR,
"title": {
"eng": "$TITLE"
},
EOF
if [ "$DESCFILE" ]; then
grep -A 2 subtitle $DESCFILE >> "$JSONFILE"
grep -A 2 description $DESCFILE >> "$JSONFILE"
else
cat << EOF >> "$JSONFILE"
"subtitle": {
"eng": "filename: $FILE"
},
"description": {
"eng": ""
},
EOF
fi
cat << EOF >> "$JSONFILE"
"comment": "added by $(basename $0)",
"files": [
{
"filename": "$INFILE"
}
]
}
EOF
fi
echo "Sending create command for \"$TITLE\""
if curl -q --data @"$JSONFILE" "http://$USER:$PASS@localhost:9981/api/dvr/entry/create" ; then
echo "Imported \"$TITLE\"" >> /tmp/tvhimport.log
fi
echo
exit 0