How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file?

Added by K Shea over 3 years ago

Here in North America there is a DVB-S2 network that sends their audio in a rather strange manner, spread out across multiple streams. Last night I saw a tweet about an article that offers a solution:

http://freetoairamerica.wordpress.com/2014/09/30/fixing-the-audio-on-recorded-programs-from-a-certain-network-which-shall-remain-nameless/

Which is to run this ffmpeg command on any program recorded from that network:

ffmpeg -i "original program filename.ts" -c:v copy -filter_complex '[0:1][0:2][0:3]amerge=inputs=3,pan=5.1|FL=c0|FR=c1|FC=c2|LFE=c3|BL=c4|BR=c5' -c:a mp2 "converted program filename.ts"

The article notes that this only works if you install a version of ffmpeg from ffmpeg.org (see the article for details) but with the correct version of ffmpeg installed, this does seem to work. I got to wondering if it would be possible to install a Post-Processor Command to automatically run that on any program recorded from that network, so as to automatically fix the sound (I am assuming it would not be possible to do something like this in real time, as the stream is being recorded or played). According to the Wiki page on Tvheadend post recording scripts, it appears I should be able to use this as a Post-Processor Command:

/usr/bin/ffmpeg -i %f -c:v copy -filter_complex '[0:1][0:2][0:3]amerge=inputs=3,pan=5.1|FL=c0|FR=c1|FC=c2|LFE=c3|BL=c4|BR=c5' -c:a mp2 "/home/hts/recordings/Converted-%b"

(assuming that this does not exceed some sort of length limit for a Post-Processor Command in TVHeadEnd)

The idea is that this would keep the original recording (in case the conversion didn't go well for some reason) but also offer the converted version, with "Converted-" prepended to the file name. The only problem with that is that TVHeadEnd doesn't seem to "see" the resulting file and therefore doesn't offer it to the clients. Nor does the converted recording appear under the "Finished Recordings" tab in the web interface.

How do I get TVHeadEnd to recognize the converted file and make it available to clients such as XBMC? It's more important to have the converted file available than the original, though it would be nice to have both just in case there is some issue in the converted file, and for ease of deletion once the converted file has been watched.


Replies (8)

RE: How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file? - Added by Alex . over 3 years ago

Hi K Shea ,

Some quick remarks; I am not sure if there is a way to make tvheadend recongnise the resulting file.

(assuming that this does not exceed some sort of length limit for a Post-Processor Command in TVHeadEnd)

This can be solved ( as is also stated in the link you provide) to write a bash- script, and call the bash script from tvheadend, not the actual ffmpeg command ( using /path/to/script.sh %f %b as in the example)

It's more important to have the converted file available than the original, though it would be nice to have both
just in case there is some issue in the converted file, and for ease of deletion once the converted file has been
watched.

If you use a script you might try to swap the files so that the converted file has the same name as the original :
- Convert the file to zzz-convert
- move the original file to some folder 'original'.
- move the converted file zzz-convert to zzz

I am not sure if this works, but this way the converted file might be available in tvheadend.

Alex.

RE: How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file? - Added by K Shea over 3 years ago

Alex . wrote:

Hi K Shea ,

Some quick remarks; I am not sure if there is a way to make tvheadend recongnise the resulting file.

I really hope there is - if there is not, there should be. Maybe time to go make a feature request?

(assuming that this does not exceed some sort of length limit for a Post-Processor Command in TVHeadEnd)

This can be solved ( as is also stated in the link you provide) to write a bash- script, and call the bash script from tvheadend, not the actual ffmpeg command ( using /path/to/script.sh %f %b as in the example)

I thought about that, it's just that I'm not real comfortable writing bash scripts. I can usually do one if I really need to but it can take me a day to come up with something that an experienced bash user could knock out in five minutes, just because I do it so infrequently.

It's more important to have the converted file available than the original, though it would be nice to have both
just in case there is some issue in the converted file, and for ease of deletion once the converted file has been
watched.

If you use a script you might try to swap the files so that the converted file has the same name as the original :
- Convert the file to zzz-convert
- move the original file to some folder 'original'.
- move the converted file zzz-convert to zzz

I tried doing something like this manually and discovered that if you mess with the original file in any way other than moving a converted file over it, TVHeadEnd can lose track of it, and once that happens you are out of luck, for the same reason you can't get TVHeadEnd to recognize a converted file (as far as I know right now).

However, if you convert the file and then move the converted file on top of the original (which I did using Midnight Commander so I am not sure of the command line syntax, probably mv -f converted_filename original_filename but don't hold me to that) then it does seem to recognize the converted file.

I am not sure if this works, but this way the converted file might be available in tvheadend.

Well at least in the test I did earlier tonight, what I said in my previous paragraph seems to work. But still, I wish there was a way to make both files accessible, since I don't want to delete the original until I have watched the converted file, and having both shown in the recordings list would make it easy to delete both.

RE: How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file? - Added by Alex . over 3 years ago

K Shea wrote:

I thought about that, it's just that I'm not real comfortable writing bash scripts. I can usually do one if I

really need to but it can take me a day to come up with something that an experienced bash user could knock out in five minutes, just because I do it so infrequently.

Well, it could be a really easy script, along the lines:
( I think getting the ffmpeg command right is the hardest, but you need to do that anyway...)

ffmpeg <YOUR FFMPEG STUFF>
mv original_filename /path/to/original/files
mv converted_filename original_filename

If moving takes time ( for example if it is on another script) you just add a few seconds of pause in between.

RE: How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file? - Added by K Shea over 3 years ago

Just wanted to come back to say that I figured it out, but there is one wrinkle - if the file disappears from my recordings directory for even a second or two, TVHeadEnd loses track of it and then I have the same problem mentioned in my original post - there is no way to get TVHeadEnd to recognize the file again. So moving the original file is risky because in that brief moment when it is moved and the script hasn't yet started moving (actually renaming) the converted file, if TVHeadEnd happens to check the directory in that short interval it will think the file is MIA and lose track of it.

So what I wound up doing was this:

#!/bin/bash
cd /home/hts/recordings
cp "$1" /home/hts/backuprecordings
/usr/bin/ffmpeg -i "$1" -c:v copy -filter_complex '[0:1][0:2][0:3]amerge=inputs=3,pan=5.1|FL=c0|FR=c1|FC=c2|LFE=c3|BL=c4|BR=c5' -c:a mp2 "converted-$1" 
mv -f "converted-$1" "$1" 

In the profile, for the Post-Processor Command I use:

/home/hts/myscript.sh "%b" 

This is very simplistic and probably should have some kind of error trapping (to halt the process if the copy operation fails, for example) but I am not that great with bash scripts so for now this will be it. Thanks for the help! Still wish there was a way to get TVHeadEnd to recognize a recording that it didn't create, though.

RE: How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file? - Added by Alex . over 3 years ago

Good to hear that this works.

One other work-aroung may be to actually schedule and record the program twice, each from a different profile and with different post-processing (i.e. one without any postprocessing, and one to make the file smaller).
It should not take up much more resources ( apart from disk-writes) since both recordings will use the same stream.

Still a workaround and maybe not a perfect solution.

Alex.

RE: How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file? - Added by K Shea over 3 years ago

Well, I thought it worked, but further testing over a few days shows it doesn't work well enough to use, and I cannot understand why. The problem makes no sense.

If I run the bash script manually after a program has been recorded, it works fine and converts the entire file 100% of the time. Never any problem doing it that way.

BUT, if I set it up as a Post-Processor Command, it appears to work but in reality it only partially converts the file. For example, if the original file is an hour long, the converted file might only be 25 minutes (though I've seen as low as 5 minutes, and sometimes closer to 35 minutes on the high side). If I run the exact same script manually, then it works.

Something is definitely happening that causes ffmpeg to abort prematurely but I cannot understand why that would ONLY happen when it is running as a Post-Processor Command from TVHeadEnd.

RE: How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file? - Added by ullix tv over 3 years ago

TVH seems to notice any filename change (perhaps by inotify?) and correct its database accordingly. Therefore the following works to keep tvh in knowledge about a file:

- original record: /tvh/movie.ts
- transcode (with handbrake in my case) into new dir: /tvh/movie.ts --> /tvh/transcoded/movie.mkv
- copy new file movie.mkv over the original movie.ts file: cp /tvh/transcoded/movie.mkv /tvh/movie.ts , i.e. using the wrong extension .ts instead of .mkv!
- renaming /tvh/movie.ts into /tvh/movie.mkv

However, I am not using post-processing, but a python script timed by cron, but I think the results would be the same.

The post-processing did get in my way when I watch a just-recorded movie, and that movie file becomes overwritten with the transcoded file while I am still watching.

RE: How can I set up a Post-Processor Command and have TVHeadEnd recognize the resulting file? - Added by McNeal Jones 4 days ago

Besides cable, Charter Spectrum.net Communications additionally offers https://spectrumlogin.org once you've established your Spectrum.net username and password.

(1-8/8)