Hi,
due to ongoing issues of playing back recorded .ts files in Kodi (17.4, 17.5, 17.6 - playback quite often just freezes, you need to hit stop and start playing again) I've changed my post processing script to convert the .ts file to .mkv after recording. MKV appears more stable when watching, also skipping seems to be much better.
Some may wonder why I'm not directly recording in .mkv format: Here watching the show from the start while it's still being recorded (timeshifted viewing) does not work with .mkv. And that's what we quite often do.
So I'm converting right after the recording has finished:
* I copy the file from my NAS (where it's recorded) to the TVH server
* Here the file xxx.ts is converted to xxx.ts.tmp. The target is matroska format
* Once this is ready I copy back the file xxx.ts.tmp to the NAS as original file name (xxx.ts), overwriting the original. This is to prevent that the file is "gone" for a while (where TVH would move the DB entry to "deleted recordings" with the comment "file is missing". Unfortunately TVH does not revert this change when the file is back again).
* Finally I'm just renaming the file from xxx.ts to xxx.mkv on the NAS. Usually TVH tracks this change and changed the DB entry accordingly.
When we're still watching that means a short break once the overwrite of the file starts, we just need to wait 2-3 minutes and can continue watching.
But there is ONE problem:
I can run the script from shell as hts user as often as I want for any available recorded .ts files. Works flawlessly and TVH always keeps track of the name change from .ts to .mkv without tagging even a single recording as "file is missing". It works ALWAYS.
Running the same script as the post processing script in TVH, I'm losing around 40% of the recordings into "file deleted - missing". Of course the .mkv file is still there, but TVH seems to miss the rename sometimes and then thinks the file is gone.
I have no idea where's the difference between running the script from shell as hts user or when TVH runs the script as hts user automatically after recording.
It'll be interesting how TVH keeps track of file extension changes and why it's not reliably working when this is done in post processing. I already inserted a "sleep" after the overwrite has finished and before the renaming - but still it's losing track of some files.
Maybe my script is stupid, but as long as playback from recorded .ts files is so unreliable in TVH<->Kodi and originally in .mkv recorded files can't be watched while they are still being recorded I have no other idea.
I appreciate any ideas to improve this. ;-)
#!/bin/sh
# variables
TSVIDEO=$1 #Full path to recording /video/Recordings/%{ShowName}/%{FileName.ext}
error=$2 #error from tvheadend recorder "OK" if no error
#compactbase=$(echo "$basename" | sed "s/...$//") # Remove file ext (3 char)
filename=`basename $TSVIDEO` #Nur der Filename
filepath=`dirname $TSVIDEO` #Nur der Pfad
tempfile="/home/hts/convert/$filename" #Name des temp-files
targetfile="/home/hts/convert/${filename%.*}.mkv" #Nur der Name des Zielfeiles ohne Pfad
MKVIDEO="$filepath/${filename%.*}.mkv" #Kompletter Pfad des neuen Files
if [ ! -d "/home/hts/convert" ]; then
mkdir "/home/hts/convert"
fi
# exit if not ok
if [ $error != "OK" ]; then
echo " Not OK"
exit 1
fi
# Kopiert in lokales tmp-Directory, Konvertiert dort in MKV und kopiert dann auf TVH zurück.
nice -20 cp -f "$TSVIDEO" "$tempfile.tmp" &&
nice -20 /usr/bin/ffmpeg -i "$tempfile.tmp" -sn -vcodec copy -acodec copy -flags +global_header -f matroska "$tempfile" &&
rm -f "$tempfile.tmp" &&
nice -20 cp -f "$tempfile" "$TSVIDEO" && #File erst mit falscher Endung (.ts) rüberkopieren, damit TVH die Database nicht löscht
sleep 5 && # Wenn man hier nicht pausiert, wird das File ggfs. vom TVH als missing markiert
mv -f "$TSVIDEO" "$MKVIDEO" && # Final umbenennen
rm -f "$tempfile" # Und letztes Temp-File löschen