Updating TVHeadEnd after moving files using API?
Added by Nic Butcher about 7 years ago
Hello,
I'm just up and running with tvheadend - very impressed (I even got the WebGrab++ TVguide and Picons working!).
I'm running it on RPi and then transferring the files to my NAS following recording using rsync. I'm using rsync rather than writing to the NAS directly so that recordings are tolerant to network and NAS unavailabilities. The only problem is that tvheadend then treats the recordings as 'removed'.
This is the correct behaviour of tvheadend... but I'm hoping that I can fix this with a script. I'm pretty sure I'll be able to generate the source and destination of the moved files from the rsync log.
Can anybody tell me how to make API calls to tvheadend to update it with the new file locations? Or is there some other way of telling tvheadend to look elsewhere for a file once it has been moved?
Thanks
Replies (8)
RE: Updating TVHeadEnd after moving files using API? - Added by ullix tv about 7 years ago
Look into the */dvr/log folder of hts (in my system: /home/hts/.hts/tvheadend/dvr/log )
There you find files with random names like "0a9cc43d27b3ae13180e8184652e679a". Each holds info on a single recording. At the last entry there is a "files" section, as shown below, in which you find the filename.
I haven't tried it myself, but I guess you can process those files with a script to modify the filenames. After that you probably have to restart tvheadend, as I believe these files are processed only once on startup.
The start and stop times appear to be "seconds since the epoch" , and can be converted by e.g. by Python with command
time.gmtime(1504655956) --> time.struct_time(tm_year=2017, tm_mon=9, tm_mday=5, tm_hour=23, tm_min=56, tm_sec=16, tm_wday=1, tm_yday=248, tm_isdst=0)
"start": 1504655956,
...
"content_type": 3,
"broadcast": 0,
"comment": "Auto recording: Created from EPG query",
"files": [
{
"filename": "/home/videos/xyz.ts",
"info": [
{
"type": "H264",
"width": 1280,
RE: Updating TVHeadEnd after moving files using API? - Added by Nic Butcher about 7 years ago
Thanks for your reply - it was really helpful.
I was a bit wary of changing the files directly but I have tested it and it all works ok. As you thought it is necessary to restart TVHeadEnd which is a bit of a pain because my script could end up restarting TVHeadEnd mid-recording (although it is unlikely as I run the script overnight).
Does anybody know of a way of getting TVHeadEnd to re-process the /dvr/log files without restart?
RE: Updating TVHeadEnd after moving files using API? - Added by ullix tv about 7 years ago
It would be great if there were a startup option like "tvheadend --status" which does nothing but report the current status and the minutes to the next recording, and exit.
I find myself checking the web interface for status and calculating the time slot until next recording simply to determine if I can do a reboot of the computer hosting tvheadend or do some tests/update on tvh. tvh is pretty busy in our house ;-)
RE: Updating TVHeadEnd after moving files using API? - Added by Nic Butcher about 7 years ago
Here is a script that will tell you when the next recording is (without having to go to the web interface), although you'll need a terminal so not sure that makes things much easier!
(Thanks to this page http://tvheadend.org/boards/4/topics/27066 )
NextSec=`curl -s http://user:pass@localhost:9981/api/dvr/entry/grid_upcoming | \ sed 's/,/\n/g' | \ grep start_real | \ sed "s/.*start_real.:\([0-9]*\).*/\1/" | \ sort -n | \ head -1 ` NextDateTime=` date -d @$NextSec -Iseconds ` echo "Next Recording = $NextDateTime" gap=$(($NextSec-`date +%s`)) echo "Gap (seconds) = $gap"
Note that you have to replace the user:pass with valid credentials for your TVH server.
RE: Updating TVHeadEnd after moving files using API? - Added by ullix tv about 7 years ago
Wonderful! And works here too.
Now, I am not so comfortable with shell scripts and therefore made me a Python2 script based on your script.
An ongoing recording has a start_real time in the past, hence a negative gap
#!/usr/bin/python # -*- coding: UTF-8 -*- import urllib import json import time response = urllib.urlopen('http://user:password@localhost:9981/api/dvr/entry/grid_upcoming') upcoming = response.read() parsed_json = json.loads(upcoming) entries = parsed_json['entries'] starts = [x['start_real'] for x in entries] nextrec = min(starts) gap = (nextrec - time.time()) / 60. # gap in minutes if gap < 0: print "Ongoing recording since {:0.1f} min!".format(abs(gap)) else: print "Next recording in {:0.1f} min at {}".format(gap, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(nextrec)))
Remember to provide the proper user:password.
RE: Updating TVHeadEnd after moving files using API? - Added by Nic Butcher about 7 years ago
I equally uncomfortable with Python but glad you got the above working ullix tv!
For future reference I also found a way to update the DVR/LOG using the API rather than processing the files in ../dvr/log and then restarting TVH
From a bash script using the curl command:
curl -s "http://admin:admin@localhost:9981/api/dvr/entry/filemoved" --data-urlencode "src=/storage/tvshows/xxxxx.ts" --data-urlencode "dst=/nas/tv/xxxxx.ts"
Note - you need to replace admin:admin with a suitable username and password for an admin account.
In this example TVH is configured to place recordings in /storage/tvshows/... and then I subsequently moved them to /nas/tv/...
Note - the above doesn't move the files (you must do this yourself - I recommend rsync) but just tells TVH where to find the file now.
RE: Updating TVHeadEnd after moving files using API? - Added by ullix tv about 7 years ago
... and works here as well. Though it had been a bit of a challenge to figure out that the new destination file must exist before you can refer to it; the api obviously checks for its presence and ignores the request if it is missing. And the api is not very verbose when it comes to error reporting :-/
After figuring out what curl does:
With a recording test1.ts and a copy of it test2.ts in the same directory /home/videos/tvheadend you can update the dvr/log files by:
http://user:password@localhost:9981/api/dvr/entry/filemoved?src=/home/videos/tvheadend/test1.ts&dst=/home/videos/tvheadend/test2.ts
The Python2 version then looks like this:
#!/usr/bin/python # -*- coding: UTF-8 -*- import urllib api_url = 'http://user:password@localhost:9981/api/dvr/entry/filemoved' file_from = '/home/videos/tvheadend/test1.ts' file_to = '/home/videos/tvheadend/test2.ts' post = urllib.urlencode({ 'src' : file_from, 'dst' : file_to}) print "post", post response = urllib.urlopen(api_url + "?" + post) upcoming = response.read() print upcoming
Indeed much simpler than fiddling with the log files! Looks like the solution to Nic Butcher's original question.
But, assuming you keep tvh running, if I am not mistaken you must first copy the file to the new location, then update via api, then delete the original file. Because otherwise tvh may declare the file as lost. Not sure how to get it back.
RE: Updating TVHeadEnd after moving files using API? - Added by ullix tv about 7 years ago
I noticed a problem in determining ongoing recordings.
When you get a negative gap (see my script above) you know to have an ongoing recording, which started gap minutes ago. Unfortunately, an ongoing recording is not always listed among the "upcoming", or it may be taken out of that list after x minutes.
But it is also NOT listed under '/api/dvr/entry/grid', or '/api/dvr/entry/grid_finished', or '/api/dvr/entry/grid_failed', and with '*grid_current', '*grid_ongoing' the server gives a 404 answer.
But on the web page those entries are correctly identified as being currently recorded.
There must be another way of identifying those. Anyone knows?