Project

General

Profile

Moving recorded files

Added by Hiro Protagonist 10 months ago

If you're recording with 'Make subdirectories per title' in your recording profile, sometimes things will end up in slightly different directories.
For example changes in case or punctuation from one series to the next, or a different channel changing the name subtly.
So you might end up with some files in My-Favourite-Program and some in My-favourite-Program.

I have written a script to simplify moving files into different TVHeadend directories. The script can move a single file to another directory,
or move all the files in a directory to another directory and remove the source directory.

A few caveats:
  • Update TVH_USER, TVH_PASS and TVH_IP to suit your setup
  • The script uses ln & thus does not copy the files. As noted this will only work
    on Linux filesystems and can only move files within a single filesystem.
    If you want to use it across filesystems or on filesystems that don't support ln,
    then replace 'ln "${INPATH}" "${DEST}"' with 'cp -n "${INPATH}" "${DEST}"', or you could
    use another method such as rsync.
  • The script will not move a file if the destination file already exists.
#!/bin/bash
 #
 # Move recorded TVHeadend files from one directory to another
 # WARNING!!! Only works on Linux filesystems
 #            Only moves files within a single filesystem
 #                                                                               
TVH_USER="user"
TVH_PASS="secret"
TVH_IP="127.0.0.1"
TVH_PORT="9981"
API_ENDPOINT="/api/dvr/entry/filemoved"
BIN_CURL="/usr/bin/curl" # Move a single file to a new directory & update TVHeadend # movefile /path/to/file /path to/dest/dir #
movefile() {
INPATH=$1
OUTDIR=$2
INFILE=$(basename ${INPATH})
DEST="${OUTDIR}"/"${INFILE}"
if [ -e "${INPATH}" ]; then
if [ ! -e "${OUTDIR}" ]; then
mkdir -p "${OUTDIR}"
fi
ln "${INPATH}" "${DEST}"
if [ $? -eq 0 -a -e ${DEST} ] ; then
echo "Notifying Tvheadend of the change from\n${INPATH} to \n${DEST}"
SUCCESS=0
REPEAT=0
while [ "$REPEAT" -lt 5 ]; do
if ${BIN_CURL} -s "http://${TVH_USER}:${TVH_PASS}@${TVH_IP}:${TVH_PORT}${API_ENDPOINT}" \
--data-urlencode "src=${INPATH}" \
--data-urlencode "dst=${DEST}" > /dev/null; then
echo "Notification successful!"
SUCCESS=1
break
else
let REPEAT++
echo "Notification attempt $REPEAT failed."
sleep 30
fi
done
if [ "$SUCCESS" -gt 0 ]; then
echo "All done, deleting ${INPATH}"
rm ${INPATH}
else
echo "Could not notify TVH about the change - deleting ${DEST} and leaving original file"
rm -f ${DEST}
fi
else
echo "Unable to link ${INPATH} to ${DEST}"
fi
fi
}
if [ $# -lt 2 ]; then
echo "Usage"
echo "$0 /path/to/source/dir /path/to/dest/dir"
echo "$0 /path/to/source/file /path/to/dest/dir"
else
SRC=$1
DST=$2
if [ -d "${SRC}" ]; then # Move all files in ${SRC} to ${DST}
for FILE in "${SRC}"/* ; do
movefile "${FILE}" "${DST}"
done
rmdir ${SRC}
else
movefile "${SRC}" "${DST}"
fi
fi